Name: ________________________ Netid:
_______________ Section: _______
Each blank in problem #1 is worth 1/12 point for a total of 1 point.
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. If the line of code has an error then write the corrected line of code in the blank next to that incorrect line of code. 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 ONLY if the existing code has an error. P, R, and A are floats, T is an int, */
float P; /* _______________________________________________________________ */
int T; /* _______________________________________________________________ */
R float; /* _______________________float R________________________________________ */
A double; /* ________________double A;_______________________________________________ */
printf("Enter the Principal: %f ") ; /* ___________printf("Enter the Principal : "); ;____________________________ */
scanf("%i",&P) ; /* ________scanf("%f", &P); ____________________________________ */
printf("Enter the number of years : ) ; /* _____________printf("Enter the number of years :") ;_________ */
scanf("%T",T) ; /* _________scanf("%i",&T) ;___________________________________ */
printf("Enter the rate of interest(%) :") ; /* ____________________________________________ */
scanf('%i',&R) ; /* _________ scanf("%f",&R) ;___________________________________ */
A = P exp(RT) ; /* ______P*exp(R*T);_____________________________________ */
printf("A = %f \n" A ) ; /* _______printf("A = %f \n", A ) ; _________________________ */
}
Problem #2 3/12 point.
Write the output of the following code:
#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");
}
}
AAA AA A ________________________________
Problem #3 is worth 3/12 point.
Write the output of the following code.
#include <stdio.h>
void main(void)
{
do
{
printf("G ");
if ( 0 > 1)
printf("H ");
else
{
break;
printf("I ");
}
printf("J ");
} while(1 > 0);
printf("\n");
}
_________G_________________________________________________________________
Problem #4 is worth 6/12 point.
Complete the code below, by filling in the blanks, to reverse the values of the array named a.
#include <stdio.h>
#define X 5
void main(void)
{
int i , temp;
int a[X] = {0 , -8 , 7 ,9, 1};
/* swap first element with last element , second element with next to last element ... */
for(i=0; i < X/2; i = i+1)
{
temp = a[i];
a[i] = a[ ___X-(i+1) or X -i -1_______________]; /* H Hint: you need to use both i and X. Also note X=5 and a[] has indices between 0 and 4 */
a[ ___X-(i+1) or X -i -1____________] = temp; /* Hint: same as above */
}
for(i=0; i <X; i=i+1)
printf("%i ", a[i]); /* prints 1 9 7 -8 0 */
printf("\n");
}
Problem #5 is worth 6/12 point.
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];
__________nums[0]
= 1;
__________nums[1] = 0;
Problem #6 is worth 6/12 point.
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: ________A__and __B_____________________________
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)
int bank = 100;
int bet;
int a,b,c;
____________________________________
Question #2. Write the C statements that you used to print the game rules in step B. (you may not need all the Blanks)
__
printf("\n\n*** Welcome to the Virtual Slot Machine! ***\n\n");
printf("You will begin with $100.\n"
"Each bet is at lest $1, up to whatever money you have.\n"
"For each play, 3 random numbers are generated.\n"
" First, your bet will be deducted from your bank.\n"
" If all three numbers are the same, you win 45 times what you bet.\n"
" If two consecutive numbers are the same, you win "
"3 times what you bet.\n"
" Otherwise, you lose your bet.\n\n");
printf("To stop playing, enter 0 for your bet.\n"); ********Or something similar
Question #3. Write the C command to assign a random number between 0 and 9 to your variable (a).
________________a=rand()%10;_____or _____a=rand()%(9-1+1)+1______________
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?
______________bank<0__________________________