Lab 7 Answer Sheet

Name: ________________________ Netid: _______________ Section: _______


Name: ________________________ Netid: _______________ Section: _______

 

Name: ________________________ Netid: _______________ Section: _______

 

 

Part 1: Prelab

 

Part A: Operations on integer values

1.
a) Answer: _____________________________ (computations involving only integers return integer values)


b) Answer: _____________________________ (notice that the %i controls where the integer appears on the computer screen 


c) Answer: _____________________________ ( % means "remainder" just like doing 5th grade math)


d) Answer: ______________________________


e) Answer: ______________________________


f) Answer: ______________________________


g) Answer: ______________________________


h) Answer: ______________________________


i) Answer: ______________________________


j) Answer: ______________________________


k) Answer: ______________________________


l) Answer: ______________________________

 

=====================================================================

Part B: If, nested if and switch statements

2.


m) __________________________________________


n) __________________________________________


o) __________________________________________




3.


p) __________________________________________



q) _________________________________________



r) _________________________________________



 

Part 2: Simple C programs

Part A.

1.      The following program compiles and runs without errors. Write the ouput the following code produces.


       #include <stdio.h>

       void main(void)
       {
          int num;
        
          num = 8;

          if (num%2)
              printf("Odd!");
         
          num = 9;

          if ( num%2)
               printf("Odd!\n");
          else
               printf("Even!\n");

          num = 30;

          if (num%2 )
         {
              printf("Not multiple of Two\n");
         }
         else if ( num%3 )
                {
                    printf("Not multiple of Three\n");
                }
                else if ( num%5)
                       {
                           printf("Not multiple of Five\n");
                       }
                       else
                       {
                           printf( "The input is a multiple of Two, Three, and Five!\n" );
                        }

          num = 2;

          switch(num)
          {
              case 1: printf("One ");
                          break;
              case 2: printf("Two ");
                          break;
              case 3: printf("Three ");
                          break;
              default: printf( "The input is not valid! " );
          }

          printf("\n");

       }

Write the output of the program (above).

 

a) __________________________________________


b) __________________________________________


c) __________________________________________ 

2.      The following program compiles and runs without errors. Write the ouput the following code produces.

#include <stdio.h>

void main(void)
{

    int num;

    num = 10;

    if (num < 0); /* ; semicolon here means end of statement so the printf immediately below is not part of the if */

   printf("Negative Number!\n");

    num = 0;

    if ( num )

        printf(" Equal to zero!\n");

    else
      
        printf("Not equal to zero!\n");

    num = 1;
       
    switch(num)
   {

      case 1: printf("One ");

      case 2: printf("Two "); /* note that the break statements are missing */

      case 3: printf("Three  ");
     
      default: printf( "Not valid! " );

    }

    printf("\n");

}

d) ________________________________________




e) ________________________________________




f) ________________________________________


=====================================================================

Part B.


1. Complete the algorithm for the change program.

float amount;
int cents;
int quarters, dimes, nickels, pennies;

printf Enter the amount:
scanf %f amount
amount = amount + .005 /* correct for any round-off errors */
cents = 100 * amount

quarters = cents / 25;
if (quarters > 0)
printf %i quarters;
cents = cents % 25;

dimes = cents / 10
if ( dimes > 0)
printf %i dimes
cents = cents % 10


________________________________ /* nickels */



________________________________/* nickels */



________________________________/* nickels */



pennies = _______________________/* pennies */



if ( ____________________________ ) /* pennies */



________________________________/* pennies */







2. Our program is not just supposed to work for values less than 1.00. Try entering 1.17 into your program and write the response.
(some blanks may be left empty)

____________________ quarters


____________________ dimes


____________________ nickels


____________________ pennies

 

=====================================================================

Part C.


3. Write the results you obtain when you enter the following input to your program:


input = 2/3/8output = ____________________________________________


input = 12/7/96 output = ____________________________________________