Name: ________________________ Netid:
_______________ Section: _______
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.
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 ) ; /* _____________________________________________ */
}
Write the output of the following code: (Do remeber 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 remeber 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");
}
____________________________________________________________________________
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
Question #1. Write the C statements that you used to declare your variables in step A. (you may not need all the blanks)
________________________________________
________________________________________
________________________________________
________________________________________
________________________________________
Question #2. Write the C statements that you used to print the game rules in step B. (you may not need all the Blanks)
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
Question #3. Write the C command to assign a random number between 0 and 9 to your variable (a).
________________________________________
Question #4. In your main loop to take in the bets and calculate how big the bank is. What is the stop condition you used in your loop?
________________________________________