Concept explainers
Find the error in each of the following. (Note: There may be more than one error.)
- The following code should print whether a given integer is odd or even:
- The following code should input an integer and a character and print them. Assume the usere types as input 100 A.
- The following code should output the odd integers from 999 to 1:
- The following code should output the even integers from 2 to 100:
- The following code should sum the integers from 100 to 150 (assume total is initialized to 0):
(a)

To find and debug the error using proper syntax and logic in the given program.
for (int x = 100; x >= 1;--x) { printf ("%d\n", x); }
Explanation of Solution
Given information:
- For (x = 100, x >= 1, ++x) {
- printf ("%d%n", x);
- }
Explanation:
- Loop variable needs to be initialized as an intthat is int x.
- The syntax of a for loop is not followed as well as C is the case-sensitive language that is For and for are the different for C compiler.
- Therefore, For should be written as for and the commas need to be changed into the semi-colons (;) inside the for-loop parenthesis.
- The correct syntax of a for loop is:
- The logical error wasthat loop going to execute the infinite times as it’s variablestarts from 100 and is updating its value by incrementing 1 at every run. Therefore, at every update, the value of x is greater than 1. Thus, instead of updating x as ++x use -- x.
- The last error is that to print every integer in newline \n is used instead of %n .
for (Initializing variable; Testing condition; Updating variable){
//for block to write the statements
}
Output:
(b)

To find and debug the error using proper syntax and logic in the given program to print whether the given integer is odd or even.
switch (value % 2) { case 0: puts ("Even integer"); break; case 1: puts ("Odd integer"); break; }
Explanation of Solution
Given information:
switch (value % 2) { case 0: puts ("Even integer"); case 1: puts ("Odd integer"); }
Explanation:
The cases act as the start point for every switch and case block where the switch takes the data and matches it with the corresponding case. Therefore, if any case is matched then all the statements from that case will be executed till it terminates or breaks. Therefore, cases should have a break keyword to exit the switch and case block, once the case is matched.
(c)

To find and debug the error using proper syntax and logic in the given program to print an integer value and the character value A as 100 A.
scanf ("%d", &intVal);
scanf ("\n%c", &charVal);
printf ("Integer: %d\nCharacter: %c\n", intVal, charVal);
Explanation of Solution
Given information:
scanf("%d", &intVal);
charVal = getchar ();
printf("Integer: %d\nCharacter: %c\n", intVal, charVal);
Explanation:
To stop reading the variable charValblank character, the second statement need to skip the preceding blanks when the user enters the integer value and press return, to rectify this scanf must be used. That is getchar() reads the next line after pressing by inputing the integer value.
Output:
(d)

To find and debug the error using proper syntax and logic in the given program
Explanation of Solution
Given information:
for (x = .000001; x == .0001; x += .000001)
printf("%.7f\n", x);
Explanation:
The numbers are quite small to be compared and the for-loop must not compare the floating-point number using == as it causes impression which might cause the infinite loop. Thus, it is recommended to use integer values as int datatype in the for-loop.
(e)

To find and debug the error using proper syntax and logic in the given program to print odd number from 999 to 1.
for (int x = 999; x >= 1; x -= 2) if (x % 2 == 1) printf ("%d\n", x);
Explanation of Solution
Given information:
for(x = 999; x >= 1; x += 2)
printf("%d\n", x);
Explanation:
- Loop variable need to be initialized as intthat is int x.
- To print the odd values from 999 to 1 the loop variable should decrease by two, instead of increasing by 2 as we need to decrease the value.
- The correct syntax of a for loop is:
- If condition is used to check whether the variable x is odd or not.
for (Initializing variable; Testing condition; Updating variable) {
//for block to write a statement
}
Output:
(f)

To find and debug the error using proper syntax and logic in the given program to print even numbers between 2 to 100.
int counter = 2;
do{
if(counter % 2== 0)
printf("%u\n", counter);
counter += 2;
} while(counter <= 100);
Explanation of Solution
Given information:
counter = 2;
Do {
if (counter % 2== 0) {
printf ("%u\n", counter);
}
counter += 2;
} While (counter < 100);
Explanation:
- The variable need to be initialized as intthat is int counter.
- The programming language - C is the case-sensitive language that is Do and do are different for C compiler and the same goes for While and while.
- To print the even values to 100 inclusively whileloop needs to <= instead of <.
Output:
(g)

To find and debug the error using proper syntax and logic in the given programto find the sum of the numbers between 100 to 150.
int total = 0; for(int x = 100; x <= 150; ++x){ total += x; } printf("%d",total);
Explanation of Solution
Given information:
for(x = 100; x <= 150; ++x);{
total += x;
}
Explanation:
- The declaration of the total variable is missing.
- Loop variable needs to be initialized as an intthat is int x.
- The syntax of a for loop is not followed as there should be no comma (,) after the parenthesis.
- The correct syntax of a for loop is:
- The logical error was that there is no print statement.
for (Initializing variable; Testing condition; Updating variable) {
//for block to write statement
}
Output:
Want to see more full solutions like this?
Chapter 4 Solutions
C How To Program, Global Edition
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Degarmo's Materials And Processes In Manufacturing
Mechanics of Materials (10th Edition)
Modern Database Management
- Assume you are a loyal member of Costco. You have been expecting your membership reward from the company, and you just received a text message on your mobile phone. The message appears to be from Costco: Costco - Your 3% return is ready: costco.wholesaledividend.com Before clicking the link in the message, you consider whether this is genuinely from Costco or a phishing attack on you. How can you tell? If it is a phishing attack, what mechanism was most likely used to send the message to you? What actions can you take if you are not sure whether it is a genuine message from Costco? What actions can you take if you are sure this is a phishing attack?arrow_forwardRSA and Diffie-Hellman are important algorithms in public-key cryptography. What are the differences between the two? Assume you intercept the ciphertext C = 105 sent to a user whose public key is e = 7, n = 403. Explain in detail how you will find the private key of the user and crack the ciphertext. What is the plaintext M?arrow_forwardCBC-Pad is a block cipher mode of operation commonly used in block ciphers. CBC-Pad handles plain text of any length. Padding is used to ensure that the plaintext input is a multiple of the block length. Hence, the ciphertext is longer than the plaintext by at most the size of a single block.Assume that the original plaintext is 556 bytes and the size of a cipher block is 28 bytes. What will be the padding? If the original plaintext is an integer multiple of the block size, will padding still be needed? Why or why not?arrow_forward
- Abstract classes & Interfaces (Ch13) 5. See the code below and solve the following. class Circle { protected double radius; // Default constructor public Circle() ( } this(1.0); // Construct circle with specified radius public Circle(double radius) { } this.radius radius; // Getter method for radius public double getRadius() { } return radius; // Setter method for radius public void setRadius(double radius) { } this.radius = radius; // Implement the findArea method defined in GeometricObject public double findArea() { } return radius* radius * Math. PI; // Implement the find Perimeter method defined in GeometricObject public double findPerimeter() { } return 2*radius*Math.PI; // Override the equals() method defined in the Object class public boolean equals(Circlel circle) { } return this.radius == circle.getRadius(); // Override the toString() method defined in the Object class public String toString() { } } return "[Circle] radius = " + radius; 5-1. Define a class name…arrow_forward6. What is Race condition? How to prevent it? [2 marks] 7. How many synchronization methods do you know and compare the differences. [2 marks] 8. Explain what are the “mutual exclusion”, “deadlock”, “livelock”, and “eventual entry”, with the traffic intersection as an example like dinning philosophy. [2 marks] 9. For memory allocation, what are the difference between internal fragmentation and external fragmentation. Explain with an example. [2 marks] 10. How can the virtual memory map to the physical memory. Explain with an example. [2 marks]arrow_forwardYour answers normally have 50 words. Less than 50 words will not get marks. 1. What is context switch between multiple processes? [2 marks] 2. Draw the memory layout for a C program. [2 marks] 3. How many states does a process has? [2 marks] 4. Compare the non-preemptitve scheduling and preemptive scheduling. [2 marks] 5. Given 4 process and their arrival times and next CPU burst times, what are the average times and average Turnaround time, for different scheduling algorithms including: a. First Come, First-Served (FCFS) Scheduling [2 marks] b. Shortest-Job-First (SJF) Scheduling [2 marks] c. Shortest-remaining-time-first [2 marks] d. Priority Scheduling [2 marks] e. Round Robin (RR) [2 marks] Process Arrival Time Burst Time P1 0 8 P2 1 9 P3 3 2 P4 5 4arrow_forward
- a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. [Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NF]arrow_forwarda database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. [Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NF]arrow_forwardIf a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type (ID or Non-ID, also Justify why)?arrow_forward
- Which one of the 4 Entities mention in the diagram can have a recursive relationship? Order, Product, store, customer.arrow_forwardInheritance & Polymorphism (Ch11) There are 6 classes including Person, Student, Employee, Faculty, and Staff. 4. Problem Description: • • Design a class named Person and its two subclasses named student and Employee. • Make Faculty and Staff subclasses of Employee. • A person has a name, address, phone number, and e-mail address. • • • A person has a class status (freshman, sophomore, junior and senior). Define the status as a constant. An employee has an office, salary, and date hired. A faculty member has office hours and a rank. A staff member has a title. Override the toString() method in each class to display the class name and the person's name. 4-1. Explain on how you would code this program. (1 point) 4-2. Implement the program. (2 point) 4-3. Explain your code. (2 point)arrow_forwardSuppose you buy an electronic device that you operate continuously. The device costs you $300 and carries a one-year warranty. The warranty states that if the device fails during its first year of use, you get a new device for no cost, and this new device carries exactly the same warranty. However, if it fails after the first year of use, the warranty is of no value. You plan to use this device for the next six years. Therefore, any time the device fails outside its warranty period, you will pay $300 for another device of the same kind. (We assume the price does not increase during the six-year period.) The time until failure for a device is gamma distributed with parameters α = 2 and β = 0.5. (This implies a mean of one year.) Use @RISK to simulate the six-year period. Include as outputs (1) your total cost, (2) the number of failures during the warranty period, and (3) the number of devices you own during the six-year period. Your expected total cost to the nearest $100 is _________,…arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE LEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT




