Find the error in each of the following. (Note: There may be more than one error
ID: 3835731 • Letter: F
Question
Find the error in each of the following. (Note: There may be more than one error.)
a) For (x = 100, x >= 1, ++x) { printf("%d ", x); }
b) The following code should print whether a given integer is odd or even: switch (value % 2) { case 0: puts("Even integer"); case 1: puts("Odd integer"); }
c) The following code should input an integer and a character and print them. Assume the user types as input 100 A. scanf("%d", &intVal); charVal = getchar(); printf("Integer: %d Character: %c ", intVal, charVal);
d) for (x = .000001; x == .0001; x += .000001) { printf("%.7f ", x); }
e) The following code should output the odd integers from 999 to 1: for (x = 999; x >= 1; x += 2) { printf("%d ", x); }
f) The following code should output the even integers from 2 to 100: counter = 2; Do { if (counter % 2 == 0) { printf("%u ", counter); } counter += 2; } While (counter < 100);
g) The following code should sum the integers from 100 to 150 (assume total is initialized to 0): for (x = 100; x <= 150; ++x); { total += x; }
Written in "C" please.
Explanation / Answer
a) For (x = 100, x >= 1, ++x) { printf("%d ", x); }
x should be decremented
for (x = 100, x >= 1, --x) { printf("%d ", x); }
b) The following code should print whether a given integer is odd or even: switch (value % 2) { case 0: puts("Even integer"); case 1: puts("Odd integer"); }
there is no break statement
switch (value % 2) { case 0: puts("Even integer"); break; case 1: puts("Odd integer"); break; }
c) The following code should input an integer and a character and print them. Assume the user types as input 100 A. scanf("%d", &intVal); charVal = getchar(); printf("Integer: %d Character: %c ", intVal, charVal);
It will not weight for user to type in A.
You need to consume next char entered by user while enetring int
scanf("%d", &intVal);getchar(); charVal = getchar(); printf("Integer: %d Character: %c ", intVal, charVal);
d) for (x = .000001; x == .0001; x += .000001) { printf("%.7f ", x); }
condition is wrong for loop termination
for (x = .000001; x <= .0001; x += .000001) { printf("%.7f ", x); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.