CS101 Prelab 9


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 9. 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  C loop statements (while, do-while,for)  and debug a program.  
 

Part A: Calculating simple interest.

  1. Slim Shady shows you his C program that calculates the returns on his investment using the formula for continuous compounding interest. However,  the code is a programmer's nightmare. Please help him by pointing out the errors. Write the correct lines of code in the blanks below.  If a line of code doesn't have any error then leave the blank empty.
  2.  

     

    A= P eRT

    Where A = amount after T years , P = Principal (initial investment),  R = APR (annual percentage rate as a decimal, 5% = .05)

                      

          /* 
           Project: CS101 Prelab 9
           File: investment.c
           author: Slim Shady 
           class: CS101
           Description: Program to calculate continuous compounding interest interest
           */
              
           #include <stdio.h>     /*This is included to make use of printf function*/
    #include <math.h>     /*This is included to make use of exp function*/
    /*Don't forget to compile this file with the -lm option, i.e. gcc investment.c -lm */

          void main(void)
          {                            /* write new code ONLY if the existing code has an error */
          
                 float  P                /* _______________________________________________________________ */
                 inut T ;                /* _______________________________________________________________ */
                 R float ;               /* _______________________________________________________________ */
                 float ; A               /* _______________________________________________________________ */

                 printf("Enter the Principal : ) ;             /* ____________________________________________ */
                 scanf("%i",&P) ;                              /* ____________________________________________ */
                 printf("Enter the number of years :") ;       /* ____________________________________________ */
                 scanf("%i",T) ;                               /* ____________________________________________ */
                 printf("Enter the rate of interest(%) :") ;   /* ____________________________________________ */
                 scanf('%i',&R) ;                              /* ____________________________________________ */
                 
                 A = P exp(RT) ;                               /* ___________________________________________ */
                   
                   printf("A = %f \n" A ) ;	     /* _____________________________________________ */          
           }

     


    Part B: More fun with loops.


  3. Write the output of the following code:
    #include <stdio.h>

    void main(void)
    {

      int i,j;

      for(i=0; i<3; i=i+1)
      {
        for(j=0 ;j<=i ; j=j+1)
              printf("A");
        printf("\n");
      }
    }







    __________________________________________________________________________



  4. Write the output of the following code:
    #include <stdio.h> 

    void main(void)

    {

    do

    while(0 > 1)
            printf("B");

         printf("A");
     } while(0 > 1);



    printf("\n");

    }


    __________________________________________________________________________


  5. Complete the code below, by filling in the blanks, to reverse the values of the arary named a.
    #include <stdio.h>
    #define N 5

    void main(void)
    {
       int i , temp;
       
       int a[N] = {-1 , -2 ,  4,  3, 17};

       /* swap first element with last element , second element with next to last element ... */
       for(i=0; i < N/2; i = i+1)
       {
          temp = a[i];

          a[i] = a[ __________________]; /* Hint: you need to use both i and N */

          a[ _______________] = temp;  /*  Hint: same as  above */

        }
     
       for(i=0; i <N; i=i+1)
           printf("%i ", a[i]);   /* prints 17  3   4   -2    -1  */
        printf("\n");
    }



    ____________________________________________________________________________

    Part C: Arrays


  6. Assume you have an array called nums that contains 20 integers.Write two C statements, one to assign the value 1 to the first element of the array using subscripting and a second C statement to assign the value 20 to the last element of the array. Remember that array index starts with 0, instead of 1. 
    int nums[20];
    _______________________________________________________



    _______________________________________________________

  7. Complete the code below, by filling in the blanks, to assign the value 10 to every element of the array named a.

     #include <stdio.h>


       void main(void)
       {

          int row, col;


          int a[5][5];


          for(row=0; row < ________ ; row = row + 1)
         

    for(col=0; _______________ ; col = col + 1)

             a[row][col] = 10;


       }