University of Delaware

CISC105 - General Computer Science

Homework #1
(solutions)

Variables and Program Looping

 
 
 
 1. (5) Identify syntactic errors in the following program.
 
	   main ()
	    (                                    //   {
	     INT sum;                            //   int

	     /* COMPUTE RESULT                   //   */
	     sum = 25 + 37 - 19                  //   ;

	     /* DISPLAY RESULTS */
	     printf ("The answer is %d\n" sum);  //   ...%d\n", sum);
	    }

     
 

 
 2. (2) What output might you expect from the following program?

	   main ()
	    {
	     int answer, result;

	     answer = 100;
	     result = answer - 10;
	     printf ("The result is %d\n", result + 5);
	    }

    The result is 95
 
 

 3. (24) Which of the following are invalid variable names?

	    sum                  ==>    char               ==>  6_05

	    _calloc                     Xx                      q

	    floating                    _2131                   one_two_3

	    ReInitialize         ==>    2Gether            ==>  A$
                                                                      




 4. (24) Which of the following are invalid constants?

	    624.819                ==>  0x11.2             ==>  0X0G1

	    06                          0xABCD                  475L

	    0Xec02                      0L                      -623.81

       ==>  0983                        -11E-11                 06677

 
 
 

 5. (2) What output would you expect from the following program?

	   main ()
	    {
	     char c, d;

	     c = 'd';
	     d = c;
	     printf ("d = %c\n", d);
	    }

    d = d
 
 

 6. (24) Give the format or conversion characters for displaying
         the following:

            int    %d %i                hex      %x             long octal     %lo

            float  %f %e                octal    %o             short int      %hd %hi

            double %lf %le              long int %ld %li        short hex      %hx

            char   %c                   long hex %lx            short octal    %ho

 
 
 

 7. (2) What is the value of result in the following expression?

	   int a = 10, b = 20, c = 30, d = 40, e = 50;
	   float result;

	   result = c + d - b + e % a * c / d;

    result = 50



 8. (2) What is the value of result in the following expression?

	   int result;
	   float x = 30, y = 7;

	   result = x / y;

    result = 4



 9. (2) What is the value of result in the following expression?

	   int m = 75;
	   float n = 30, result;

	   result = m / n;

    result = 2.500000
 
 
 
10. (2) How many times will the following loop be executed?

	   i = 0;
	   for ( ; i < 100; i = i + 1)
	    i = i + 4;

    20 times
 
 

11. (5) Express the following statement using the increment operator.

	   hours_worked = hours_worked + 1;

    ++hours_worked;
 
 

12. (6) Rewrite the following 'for' loop using a 'do' loop.

	   for ( x = 1; x <= 100; x = x + 1)
	    salary = hours_worked * pay_rate;
 
    x = 0;
    do
    {
     salary = hours_worked * pay_rate;
     x = x + 1;
    }
    while( x < 100);