1. Identify and correct the errors in each of the following statements. (Note: There may be more than one error per statement.)
a) scanf( "d", value );
b) printf( "The product of %d and %d is %d"n, x, y );
c) firstNumber + secondNumber = sumOfNumbers
d) if ( number => largest )
{
largest == number;
}
e) */ Program to determine the largest of three integers /*
f) Scanf( "%d", anInteger );
g) printf( "Remainder of %d divided by %d isn", x, y, x % y );
h) if ( x = y );
{
printf( %d is equal to %dn", x, y );
}
i) print( "The sum is %dn," x + y );
j) Printf( "The value you entered is: %dn, &value );
k) if(age>=65);
{
printf( "Age is greater than or equal to 65" );
}
else
{
printf( "Age is less than 65" );
}
l) int x = 1, total;
while ( x <= 10 )
{
total += x;
++x;
}
m) While(x<=100)
total += x;
++x;
n) while ( y > 0 )
{
printf( "%dn", y );
++y;
}
o) For(x=100,x>=1,++x)
{
printf( "%dn", x );
}
p) for ( x = .000001; x == .0001; x += .000001 )
{
printf( "%.7fn", x );
}
q) The following code should output the odd integers from 999 to 1:
for ( x = 999; x >= 1; x += 2 )
{
printf( "%dn", x );
}
r) 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;
}
2.Write a single C statement or line that accomplishes each of the following:
a) Print the message "Enter two numbers."
b) Assign the product of variables b and c to variable a.
c) State that a program performs a sample payroll calculation (i.e., use text that helps to document a program).
d) Input three integer values from the keyboard and place them in integer variables a, b and c.
3.State which of the following are true and which are false. If false, explain your answer.
a) C operators are evaluated from left to right.
b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales, his_account_total, a, b, c, z, z2.
c) The statement printf("a = 5;"); is a typical example of an assignment statement.
d) A valid arithmetic expression containing no parentheses is evaluated from left to right.
e) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.
4.What, if anything, prints when each of the following statements is performed? If nothing prints, then answer "Nothing." Assume x = 2 and y = 3.
a) printf( "%d", x );
b) printf( "%d", x + x );
c) printf( "x=" );
d) printf( "x=%d", x );
e) printf("%d=%d",x+y,y+x);
f) z=x+y;
g) scanf( "%d%d", &x, &y );
h) // printf( "x + y = %d", x + y );
i) printf( "n" );
5.Which, if any, of the following C statements contain variables whose values are replaced?
a) scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f );
b) p = i + j + k + 7;
c) printf( "Values are replaced" );
d) printf( "a = 5" );
6.Given the equation y = ax3 + 7, which of the following, if any, are correct C statements for this equation?
a) y = a * x * x * x + 7;
b) y = a * x * x * ( x + 7 );
c) y = ( a * x ) * x * ( x + 7 );
d) y = ( a * x ) * x * x + 7;
e) y = a * ( x * x * x ) + 7;
f) y = a * x * ( x * x + 7 );
7.State the order of evaluation of the operators in each of the following C statements and show the value of x after each statement is performed.
a) x = 7 + 3 * 6 / 2 - 1;
b) x = 2 % 2 + 2 * 2 - 2 / 2;
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
8.What does the following program print?
#include < stdio.h>
int main( void )
{
unsigned int x = 1, total = 0, y;
while ( x <= 10 ) {
y = x * x;
printf( "%dn", y );
total += y;
++x;
} // end while
printf( "Total is %dn", total );
} // end main
9.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( "%dn", x );
}
b) for ( x = 5; x <= 22; x += 7 )
{
printf( "%dn", x );
}
c) for(x=3;x<=15;x+=3)
{
printf( "%dn", x );
}
d) for ( x = 1; x <= 5; x += 7 )
{
printf( "%un", x );
}
e) for(x=12;x>=2;x-=3)
{
printf( "%dn", x );
}
10.Write for statements that print the following sequences of values:
a) 1, 2, 3, 4, 5, 6, 7
b) 3, 8, 13, 18, 23
c) 20, 14, 8, 2, –4, –10
d) 19, 27, 35, 43, 51