Lab 7 Answer Sheet

Name: ________________________     Netid: _______________    Section: _______


Name: ________________________     Netid: _______________    Section: _______


Name: ________________________     Netid: _______________    Section: _______



Part 1.

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

#include <stdio.h>

    void main(void){
        int no;
        int ir, hr;


        no = -1;

        if (no > 0)
            printf("positive!");


        no = 0;

        if ( no  == 0)
             printf("equal!\n");
        else
             printf("not equal!\n");

 
         no = 2;

        if (no == 1)
       {
            printf("one\n");
        }
       else if ( no == 2 )
       {
            printf("two\n");
        }
        else if ( no == 3 )
        {
            printf("three\n");
         }
         else
        {
            printf( "The input is not valid!\n" );
         }


     

         no = 2;



         switch(no){
             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.      Common errors in writing if, nested if and switch statements.

 
  #include <stdio.h>

    void main(void){
        int no;
        int ir, hr;


        no = -1;

        if (no > 0);       /*  ; here means end of statement so the printf immediately below is not part of the if */
            printf("positive!\n");


        no = 0;

        if ( no  = 1)       /* == means test for equality = means assign. If no is assigned 1 that means true in C.   */
             printf(" equal!\n");
        else
             printf("not equal!\n");
  

         no = 2;



         switch(no){
             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");

    }
    
 
 
  Write the output of the program (above).

d) __________________________________________




e) _________________________________________




f) _________________________________________
 
 
 
 
 


Part 2. 


   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



      ________________________________



      ________________________________



     ________________________________



     ________________________________



     ________________________________



     ________________________________



     ________________________________      





2. TA's signature ____________________________________________



Part 3.


3. TA's signature ____________________________________________