C How to Program (8th Edition)
C How to Program (8th Edition)
8th Edition
ISBN: 9780133976892
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 4.5E

Find the error in each of the following. (Note: There may be more than one error.)

  1. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  1
  2. The following code should print whether a given integer is odd or even:
  3. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  2

  4. The following code should input an integer and a character and print them. Assume the usere types as input 100 A.
  5. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  3

    Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  4

  6. The following code should output the odd integers from 999 to 1:
  7. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  5

  8. The following code should output the even integers from 2 to 100:
  9. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  6

  10. The following code should sum the integers from 100 to 150 (assume total is initialized to 0):
  11. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  7

(a)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program.

Program Description Answer
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:

  1. Loop variable needs to be initialized as an intthat is int x.
  2. 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.
  3. Therefore, For should be written as for and the commas need to be changed into the semi-colons (;) inside the for-loop parenthesis.
  4. The correct syntax of a for loop is:
  5. for (Initializing variable; Testing condition; Updating variable){

    //for block to write the statements

    }

  6. 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.
  7. The last error is that to print every integer in newline \n is used instead of %n .

Output:

C How to Program (8th Edition), Chapter 4, Problem 4.5E , additional homework tip  1

(b)

Expert Solution
Check Mark
Program Plan Intro

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.

Program Description Answer
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)

Expert Solution
Check Mark
Program Plan Intro

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.

Program Description Answer

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:

C How to Program (8th Edition), Chapter 4, Problem 4.5E , additional homework tip  2

(d)

Expert Solution
Check Mark
Program Plan Intro

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)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program to print odd number from 999 to 1.

Program Description Answer
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:

  1. Loop variable need to be initialized as intthat is int x.
  2. 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.
  3. The correct syntax of a for loop is:
  4. for (Initializing variable; Testing condition; Updating variable) {

    //for block to write a statement

    }

  5. If condition is used to check whether the variable x is odd or not.

Output:

C How to Program (8th Edition), Chapter 4, Problem 4.5E , additional homework tip  3

(f)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program to print even numbers between 2 to 100.

Program Description Answer

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:

  1. The variable need to be initialized as intthat is int counter.
  2. 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.
  3. To print the even values to 100 inclusively whileloop needs to <= instead of <.

Output:

C How to Program (8th Edition), Chapter 4, Problem 4.5E , additional homework tip  4

(g)

Expert Solution
Check Mark
Program Plan Intro

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.

Program Description Answer
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:

  1. The declaration of the total variable is missing.
  2. Loop variable needs to be initialized as an intthat is int x.
  3. The syntax of a for loop is not followed as there should be no comma (,) after the parenthesis.
  4. The correct syntax of a for loop is:
  5. for (Initializing variable; Testing condition; Updating variable) {

    //for block to write statement

    }

  6. The logical error was that there is no print statement.

Output:

C How to Program (8th Edition), Chapter 4, Problem 4.5E , additional homework tip  5

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write true if the two statements are equivalent; otherwise write false. (p∨∼q) ∨q and p
This error occur when I run the code
Find the error. Write the correct statement

Chapter 4 Solutions

C How to Program (8th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Literals in Java Programming; Author: Sudhakar Atchala;https://www.youtube.com/watch?v=PuEU4S4B7JQ;License: Standard YouTube License, CC-BY
Type of literals in Python | Python Tutorial -6; Author: Lovejot Bhardwaj;https://www.youtube.com/watch?v=bwer3E9hj8Q;License: Standard Youtube License