Find the error in each of the following. (Note: There may be more than one error
ID: 3837040 • 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 show how it is put into the compiler so it can be run, thanks.
Explanation / Answer
a) For (x = 100, x >= 1, ++x) { printf("%d ", x); }
corrected form:- in this statement ++x should be replaced by --x and opecing braces should be placed before the for statement,and also semicolon inside for statement.
{
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"); }
answer: in the above code, printf is missing,replace puts with printf.also we can put break statement,after every input.
switch (value % 2)
{
case 0:
printf("Even integer");
break;
case 1:
printf("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);
answer:- one more character reading set is required,so need to add one more scanf satement in order to preint integer and character,so one more scaf() will be added which can read the values along with the charatcter.
scanf("%d", &intVal);
scanf("%c,"&charval");
charVal = getchar();
printf("Integer: %d Character: %c ", intVal, charVal);
---------------
d) for (x = .000001; x == .0001; x += .000001)
{
printf("%.7f ", x);
}
answer:-
{
for (x = .000001; x <= .0001; x += .000001)
------------
e) The following code should output the odd integers from 999 to 1: for (x = 999; x >= 1; x += 2) { printf("%d ", x); }
answer:- in thw question it is asked about printing of odd numbers,therefor +=2 should be changed to x-=2,which means we need a decrementing loop not the incremented loop.
for (x = 999; x >= 1; x -= 2)
printf("%d ", x);
--------
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; }
answer:-semicolon after for statement is not required,and is not correct according to the syntax
{
for (x = 100; x <= 150; ++x)
total += x;
}
------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.