CS101 Prelab 7

Name: _________________________  

Netid: ___________________     Section: ____________


Each blank is worth 1/6 point.
Print out this page and fill in the blanks to answer the following questions. Hand in this assignment at the beginning of Lab 7. You should use the material found in Lecture 10,11 and 12 of the course notes and/or the course C book Chapters 1,2 ,3.1 -3.5,7.1-7.3. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help.

The following exercises should help prepare you for the in-lab activities and MP2.
You will use the C operations % ,+ , * , / on values of datatype integer, use the printf and scanf functions and along with the if-else and switch statements.

Complete the following before coming to lab. Of course you can check your answers by opening Xemacs and copying and pasting each program into a separate file and then use gcc to compile and run each program by typing a.out at the Unix prompt.

Part A: Operations on integer values

  1. Write the results you would see on your terminal screen if the following C code were inserted in a complete C program , compiled (without errors) and executed. See lecture 11, slides 16, 17, and 22. 

    The following program demonstrates the use of the "printf" and "scanf" functions in C.

    Note that all variables in a program must be "declared" . A declaration statement is of the form:
    data-type    VariableName ; 

    The data-type int means that the variable only holds integers, -1, 12, 0 but not 3.14 .
    The data-types float and double hold real numbers (including integers). The only difference between float and double is how many digits they can hold. In Matlab all real numbers were held in data-type double variables.

    Comments in C are of the form /* blah blah blah */ . Comments in your C program are ignored by the compier (gcc).


    We use the conversion specifiers %i (integer) , %d (integer), %f (float) and %lf (double) in the printf and scanf functions below to control where the numbers are printed on the computer screen.


    #include <stdio.h>
void main(void)
{
int j = 7;
int k = 5;

int hours;
int mins;
int secs;

float x = 1.23456789; /* float type data holds about 6-7 digits */
double y = 1.23456789; /* double type data holds about 14 digits */


/* integer variables and operations */

printf( " result = %i \n", j / k ); /* a) Answer: ___result = 1_______________________ (computations involving only integers return integer values) */


printf( "%i result = \n", k / j ); /* b) Answer: ____0 result =__________________ (notice that the %i controls where the integer appears on the computer screen */


printf( "%i \n", j % k ); /* c) Answer: _____2_____________________ ( % means "remainder" just like doing 5th grade math) */








/* %f shows as default 6 digits to the right of the decimal point. %.2f will show 2 digits to the right of the decimal. */

printf( "x = %f \n", x ); /* d) Answer: ____x = 1.234568______________________ */


printf( " Your cost = $%.2f \n", x ); /* e) Answer: _____Your cost = $1.23_____________________ */


printf( " Omitting the change = $%.0f\n", x ); /* f) Answer: ____ Omitting the change = $1_______ */




/* double data-type variables holds more digits than float variables. */

printf( "x = %.9f \n", x ); /* g) Answer: ______x = 1.234567881 ________ */


printf( "y = %.9lf \n", y ); /* h) Answer: ______y = 1.234567890 ________ */








/* common errors */

printf(" Enter 9.876 :");

scanf( "%lf", &x ); /* Oops, x is of float data-type so we should have used %f . */


printf("x = %f \n",x); /* i) Answer: ______x = 0.000000____________________ */




printf(" Enter 3.14 : ");

scanf( " Hello World! %lf", &y ); /* For scanf, text inside " " doesn't display on screen and causes problems. */

printf(" y = %lf \n", y); /* j) Answer: ______y = 1.234568____________________ */







/* a useful trick (for MP 2) */


/* print time in format hh:mm:ss , for example 12:09:08 */


hours = 12;
mins = 9;
secs = 8;

printf(" %i:%i:%i \n", hours, mins, secs); /* k) Answer: _____ 12:9:8 _________ */


printf(" %02i:%02i:%02i \n", hours, mins, secs); /* l) Answer: ________ 12:09:08__________________ */


}




Part B: If, nested if  and switch statements

  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 = 0;

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


            no = 1;

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

     
             no = 4;

            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 = 3;



             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).


    m) ________not equal!______________________________




    n) _________The input is not valid!_____________________________




    o) _________three_____________________________

  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).


    p) _________positive!_____________________________




    q) _________equal!_____________________________




    r) _________two three not valid!______________________