
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 Plus Mylab Programming With Pearson Etext -- Access Card Package (8th 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
- Draw a context diagram for this scenario: You are developing a new customer relationship Management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in. the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary.arrow_forwardWrite a complete Java program named FindSumAndAverage that performs the following tasks in 2-D array: Main Method: a. The main() method asks the user to provide the dimension n for a square matrix. A square matrix has an equal number of rows and columns. b. The main() method receives the value of n and calls the matrixSetUp() method that creates a square matrix of size n and populates it randomly with integers between 1 and 9. c. The main method then calls another method named printMatrix() to display the matrix in a matrix format. d. The main method also calls a method named findSumAndAverage() which: • Receives the generated matrix as input. • Calculates the sum of all elements in the matrix. • Calculates the average value of the elements in the matrix. • Stores these values (sum and average) in a single-dimensional array and returns this array • e. The main method prints the sum and average based on the result returned from findSumAndAverage()). Enter the dimension n for the square…arrow_forwardThe partial sums remain the same no matter what indexing we done to s artial sum of each series onverges, * + s of each series to the series or show 12. (1)+(0)+(0)+(+1)+ 17, " (F) + (F) + (F)(F)(- 18. 19. 1 #20. (三)+(三)-(三)+(3) 20 (9)-(0)-(0)-- 10 +1 2.1+(男)+(男)+(罰)+(鄂 9 T29 x222-끝+1-23 + -.... Repeating Decimals 64 Express each of the numbers in Exercises 23-30 as the m integers. 23. 0.23 = 0.23 23 23... 24. 0.234 = 0.234 234 234. 25. 0.7 = 0.7777... 26. 0.d = 0.dddd... where d is a digit natio of own s converges or * 27. 0.06 = 0.06666.. 28. 1.4141.414 414 414... 29. 1.24123 = 1.24 123 123 123... 30. 3.142857 = 3.142857 142857. Using the ath-Term Test In Exercises 31-38, use the ath-Term Test for divergence to show that the series is divergent, or state that the test is inconclusive 8arrow_forward
- CPS 2231 Computer Programming Homework #3 Due Date: Posted on Canvas 1. Provide answers to the following Check Point Questions from our textbook (5 points): a. How do you define a class? How do you define a class in Eclipse? b. How do you declare an object's reference variable (Hint: object's reference variable is the name of that object)? c. How do you create an object? d. What are the differences between constructors and regular methods? e. Explain why we need classes and objects in Java programming. 2. Write the Account class. The UML diagram of the class is represented below (10 points): Account id: int = 0 - balance: double = 0 - annualInterestRate: double = 0.02 - dateCreated: java.util.Date + Account() + Account(id: int, balance: double) + getId(): int + setId(newId: int): void + getBalance(): double + setBalance(newBalance: double): void + getAnnualInterestRate(): double + setAnnualInterest Rate (newRate: double): void + toString(): String + getDataCreated(): java.util.Date +…arrow_forwardTHIS IS NOT A GRADING ASSIGNMENT: Please only do lab 2.2 (bottom part of the first picture) For that Lab 2.2 do: *Part 1 (do the CODE, that's super important I need it) *Part 2 *Part 3 I also attached Section 2.5.2 which is part of the step 1 so you can read what is it about. Thank you!arrow_forwardTHIS IS NOT A GRADING ASSIGNMENT: Please only do lab 2.2 (bottom part of the first picture) For that Lab 2.2 do: *Part 1 *Part 2 *Part 3 I also attached Section 2.5.2 which is part of the step 1 so you can read what is it about. Thank you!arrow_forward
- can you please give me: * the code (step 3) *the list file (step 5) *and answer step 6 Thank youarrow_forward# Find the error# Why will the following code not print out a list of contact namesphoneBook = {'Doe, Jane' : '843-000-0000' ,'Doe, John' : '843-111-1111' ,'Smith, Adam' : '843-222-2222' ,'Jobs, Steve' : '999-333-3333' ,}for contact in phoneBook.values():print(contact)arrow_forward# Find the error:# The following code creates an empty dictionary and attempts to add a record# Why will the following code not create a new dictionary entry as intended?phoneBook = {}phoneBook{'Jobs, Steve'} = '999-111-1111'arrow_forward
- Select all the possible polar representations of the vector that is obtained from rotating where by Zrot Ź x = 3e² T= 3п 8 Hint: Consider the negative angle that is equivalent to the positive angle of the rotated vector. 0arrow_forwardCharacter Analysis If you have downloaded the source code you will find a file named text.txt on the Chapter 08 folder. Write a program that reads the file's contents and determines the following: The number of uppercase letters in the file The number of lowercase letter in the file The number of digits in the file The number of whitespace characters in the filearrow_forwardWrite a program that reads the text file's contents and calculates and outputs the following in this order: • The number of words in the file • The number of lines in the file • The number of uppercase letters in the file • The number of lowercase letters in the file • The number of digits in the file • The number of letter H's in the file • The number of whitespace characters in the file NOTE: Your program should include at least one try-except error handling statement block. Your program should also validate any input that could cause your program to crash. I'm Henery The Eighth, I Am! Henery The Eighth, I Am, I am!I got married to the widow next door,She's been married seven times before.And ev'ryone was a Henery,She wouldn't have Willie or a Sam.I'm her eighth old man named Henery,Henery the Eighth, I Am!Second verse same as the first!I'm Henery The Eighth, I Am! Henery The Eighth, I Am, I am!I got married to the widow next door,She's been married seven times before.And…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




