Chapter 4 4.5 Find the error in each of the following (Note: there may be more t
ID: 3672617 • Letter: C
Question
Chapter 4
4.5 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 );
ANS:
b) The following code should print whether a given integer is odd or even:
switch ( value % 2 ) {
case 0:
printf( "Even integer " );
case 1:
printf( "Odd integer " );
}
ANS:
c) for ( x = .000001; x <= .0001; x += .000001 )
printf( "%.7f ", x );
ANS:
d) The following code should output the odd integers from 999 to 1:
for ( x = 999; x >= 1; x += 2 )
printf( "%d ", x );
ANS:
e) The following code should output the even integers from 2 to 100:
counter = 2;
Do {
if ( counter % 2 == 0 )
printf( "%d ", counter );
counter += 2;
} While ( counter < 100 );
ANS:
f) 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;
ANS:
4.6 State which values of the control variable x are printed by each of the following for statements:
a) for ( x = 2; x <= 13; x += 2 )
printf( "%d ", x );
ANS:
b) for ( x = 5; x <= 22; x += 7 )
printf( "%d ", x );
ANS:
c) for ( x = 3; x <= 15; x += 3 )
printf( "%d ", x );
ANS:
d) for ( x = 1; x <= 5; x += 7 )
printf( "%d ", x );
ANS:
e) for ( x = 12; x >= 2; x -= 3 )
printf( "%d ", x );
ANS:
4.7 Write for statements that print the following sequences of values:
a) 1, 2, 3, 4, 5, 6, 7
ANS:
b) 3, 8, 13, 18, 23
ANS:
c) 20, 14, 8, 2, -4, -10
ANS:
4.8 What does the following program do? What is the output.
1 #include <stdio.h>
2
3
/* function main begins program execution */
4 int main()
5 {
6 int x;
7 int y;
8 int i;
9 int j;
10
11 /* prompt user for input */
12 printf( "Enter two integers in the range 1-20: " );
13 scanf( "%d%d", &x, &y ); /* read values for x and y */
14
15 for ( i = 1; i <= y; i++ ) { /* count from 1 to y */
16
17 for ( j = 1; j <= x; j++ ) { /* count from 1 to x */
18 printf( "@" ); /* output @ */
19 } /* end inner for */
20
21 printf( " " ); /* begin new line */
22 } /* end outer for */
23
24 return 0; /* indicate program ended successfully */
25
26 } /* end function main */
Explanation / Answer
4.5).
a) For ( x = 100, x >= 1, x++ )
printf( "%d ", x );
ANS: instead of x++, it shoud be x--
b)
Ans: missing of break statement after first case
switch ( value % 2 ) {
case 0:
printf( "Even integer " );
break;
case 1:
printf( "Odd integer " );
}
c). increment value is very less, infinite loop
d). Ans: instead of x += 2 , it shoud be x -= 2
e) No need to check this -> if ( counter % 2 == 0 )
it should be While ( counter <= 100 );
f) should not be semicolon after for loop:
for ( x = 100; x <= 150; x++ )
4.6)
a. 2,4,6,8,10,12
b. 5, 12, 19
c. 3, 6, 9, 12, 15
d. 10
e. 12, 9, 6, 3
4.7)
a. for(i=1; i<=7; i++)
printg("%d ",i);
b. for(i=3; i<=23; i+=5)
printf("%d ",i);
c. for(i =20; i>=-10; i-=6)
printf("%d ",i);
4.8)
This program is asking to inter two integers in range of [1-20]
lets say x and y
Then, it prints x number of lines and each line having y number of '@' character
Ex.
Enter two integers in the range 1-20: 3
4
@@@
@@@
@@@
@@@
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.