CS101 Prelab 7

Name: _________________________  

Netid: ___________________     Section: ____________

 

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. However, if you obtain some or all of your answers by writing the results produced by the compiler, please make sure to understand the results. If something is puzzling, be sure to ask 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 int, 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 Eclipse and copying and pasting each program into a separate file and then running each program as you have learned in lab 6.

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). The comments below do NOT need to be typed if you are typing the code manually.


    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: _____________________________ (computations involving only integers return integer values) */


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


printf( "%i \n", j % k ); /* c) Answer: _____________________________ ( % 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: ______________________________ */


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


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




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

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


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








/* common errors */

printf(" Enter 9.876 :");

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


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




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: ______________________________ */







/* 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: ______________________________ */


printf(" %02i:%02i:%02i \n", hours, mins, secs); /* l) Answer: ______________________________ */


}

 

 

 

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




n) __________________________________________




o) __________________________________________

3.      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);       /*  ; semicolon 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) __________________________________________




q) _________________________________________




r) _________________________________________