CS 101  Lab Activity 9

Objectives:


References




Part 1. Prelab

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.

1. Part A: Calculating simple interest.

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.

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 simple 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 in comments ONLY if the existing code has an error */
 
   float P;    /* _______________________________________________________________ */
   int T;   /* _______________________________________________________________ */
   R float;  /* _______________________________________________________________ */
   double A;          /* _______________________________________________________________ */ 

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


2. Part B: More fun with loops.

Write the output of the following code: (Do remember to Enter a Blank line in answer when \n is printed using printf function)

#include <stdio.h>

void main(void)
{

   int i,j;

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




__________________________________________________________________________



3. Write the output of the following code: (Do remember to Enter a Blank line in answer when \n is printed using printf function)

#include <stdio.h> 
 
void main(void)
{
  do
  {  
     printf("A ");
     if ( 0 > 1)
         printf("B ");
     else
       {
         break;
         printf("C ");
       }
    printf("D ");
  } while(1 > 0);
  
  printf("/n");
}



__________________________________________________________________________


4. Complete the code below, by filling in the blanks, to reverse the values of the arary named a and print it.

#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=____________________; i <____________________; i=i+1)
    printf("%i ", a[i]); /* prints 17 3 4 -2 -1 */
 printf("\n");
}




____________________________________________________________________________

Part C: Arrays

5. 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 0 to the second element of the array. Remember that array index starts with 0, instead of 1.

int nums[20];

_______________________________________________________



_______________________________________________________

6. Write down the code program choices (A, B or C) that correctly assign the value 10 to every element of the array named a.
There may be more than one correct answer.

A)

#include <stdio.h>    
void main(void)                 
   {
      int row, col;                     
      int a[5][5];      
      for(row=0; row < 10 ; row = row + 1)                              
        for(col=0; col < 10 ; col = col + 1)
           a[row][col] = 10;
for(row=0; row < 5; ++row) { for(col=0; col < 5; ++col) printf("a[%i][%i] = %i ", row, col, a[row][col]); printf("\n"); } }


B)

 

#include <stdio.h>


void main(void)

{
  int row, col;
  int a[5][5];

  for(row=0; row < 5 ; row = row + 1)
  {
    a[row][row] = 10;
    for(col=row ; col < 4; col = col + 1)
    {
      a[row][col+1] = a[row][col];
      a[col+1][row] = a[row][col];
    }
   }

  for(row=0; row < 5; ++row)
  {
    for(col=0; col < 5; ++col)
       printf("a[%i][%i] = %i ", row, col, a[row][col]);
    printf("\n");
  }

}

C)

 #include <stdio.h>
   void main(void)                      
   {
      int row, col;                     
      int a[5][5];      
      for(row=0; row < 5 ; row = row + 1)                               
        {
           col = 4;
           while( col >= 0)
           {
               a[row][col] = 10;
               col = col + 1;
           }
         }
       for(row=0; row < 5; ++row)
       {
         for(col=0; col < 5; ++col)    
           printf("a[%i][%i] = %i ", row, col, a[row][col]);
         printf("\n");
       }  
   } 



Answer: _________________________________________




Part 2: Gamesters of Triskelion

As you may have surmised, some of the CS101 TAs are in reality aliens from the planet "Triskelion" who have taken human form and have come to Earth here . The "Providers" as they are called on their home planet of "Triskelion" want you to write a C program that simulates a slot-machine. This game will be used to make the "Providers" wealthy and in control of those who are enticed by the allure of making themselves rich.

Problem Description

Here's how the program will work. Read completely through these points but don't start coding yet.

Implementation

In Unix use the mkdir command to create a directory named lab9.  Use the cd command to make lab9 your pwd.  Use the Unix cp command to copy two files named 'banditdemo' and bandit.c'  into your lab9 directory(files named 'banditdemo' and 'bandit.c' are located in the  ~cs101ta/public_html/labs/lab9  directory).  We've provided a banditdemo program that you can download  to see what your own program should do.

Use gedit to open the skeleton source file 'bandit.c' as your starting point.
When compiling, name your executable file 'bandit' by using the -o switch, e.g. at the Unix prompt type,
gcc bandit.c -o bandit
and if you don't have any error messages, run your program by typing,
bandit
 

The program can be broken into the following steps:

  1. Declare your variables. There should be the following variables (you can use more if you want).

  2. Print out the rules of the game.

  3. Inside of a loop, do the following:



The two steps inside the loop may occur in the opposite order, depending on which type of loop you use. Also, you may have to perform step 1 once before entering the loop, depending on what type of loop you use.

Since the outcome of a slot-machines is supposed to be random, we'll use a random number generator called rand (see FER p. 260). Unlike the MATLAB rand function, the C rand function returns an integer value from 0 to RAND_MAX (a large, predefined positive integer). Since we generally only want numbers between 0 and some small integer, we can use the modulus operator. For example, suppose we want to make the integer variable r equal to a random integer between 0 and 100. We could use the following statement.

 r = rand() % 101;

The rand function is found in the standard library, so you need to have the line

 #include <stdlib.h>

at the top of your .c file in order to use it.


Answer questions 1-4 on the answer sheet


Congratulations! You're done with Lab 9!