Homework #3

Homework #3

1. What output does the following program fragment produce?
i = 1 ;
while ( i    <= 64)  {
printf ( “%d “, i ) ;
i  *= 2 ;
}

2. What output does the following program fragment produce?

i  =  1234 ;
do  {
printf ( “&d “, i ) ;
i   / =  10 ;
}    while   ( i > 0 ) ;

3. Show the output of each of the code blocks below:

a.)    for  ( i  =  13 ;  i  <  23 ;  i  +=  2 )    {
printf ( ” %d\n ” , i ) ;
}

b.)    for  ( i = 30 ;  i > 0 ;  i -= 8 )      {
printf ( ” %d\n ” ,  i ) ;
}

4.  Write C statements to sum the even integers from 1 and 100 using a for statement.  Assume the integer variables sum and count have been defined, but not

initialized, to be of type int.

5.  Write for statements that print the following sequences of values:

a.)    5,  10,  15,  20,  25

b.)    100,  90,  80,  70,  60,  50,  40,  30,  20,  10

6. Assume that we want to calculate the sum of the integers from 50 down to 1.  What is wrong with the code below?

sum  =  0 ;
num  =  50 ;
while  ( num  >= 0 )    {
sum += num ;
}

7. See if you can improve the following program segments through reorganization.  First analyze the code to determine the function actually being performed.  then

recode that function as clearly and simply as possibly (i.e. without the use of break or continue).

a.)     while ( a )    {            b.)    do {
if ( b )  {                       if  ( ! a )  {
continue ;                         continue ;
}                        }
c ( ) ;                            else    {
}                                b ( ) ;
c ( ) ;
}
}   while  ( a ) ;

Grade Lab

Write a C program, grade. c, which includes a function,
calculateAverage ( ) , that can calculate the average of an arbitrary number of test scores.  Your program will not know in advance how many test scores will be

entered into your funcion so you will need to use a sentinel value  ( – 1 ) to terminate the while loop used to collect the test scores.  The test scores are integers

in the range 0 to 100 and the test score average should be output to two decimal places.  Be sure that the user prompts and grades input into the program are displayed

in the output file.

Provided below is the main ( ) function that I would like you to use for your program.

int main (void)        {
int  i ;

fp = fopen ( “csis.txt”, “w” ) ;
for  ( i = 1;  i <= 4;  ++i )   {
calculateAverage ( ) ;
}
fclose (fp) ;
return 0 ;
}

Please do not modify the main ( ) function.  It should appear as the first function in the program.  Be sure to use function prototypes for each of the functions that

are used in your program.

Note that the for loop invokes the calculateAverage ( ) function four times.  Each time the calculateAverage ( ) function is invoked, a different set of test grades

will be entered.  Please be sure to use the data shown below:

First:  78,  93,  45,  88,  89,  -1
Second:  87,  68,  100,  -1
Third:  84,  86,  90,  86,  96,  68,  82,  -1
Fourth:  -1

Output from your program should be sent to the terminal window (your screen) as well as the requested csis.txt output file.  Be sure to read the document on Capturing

Program Output.  Your full name must appear as a comment in the source file that contains main ( ) .  Be sure to include the csis.txt output file in your zip archive.

Penny Lab

On the first day you deposit a penny in the bank.  On the second day you deposit two pennies.  On the third day you deposit four pennies.  On the fourth day you

deposit eight pennies, etc.  Assuming that you keep doubling your deposit each day, write a C program, penny.c, which determines how many days it will take for you to

accumulate at least one million dollars.  (Assume that no interest is paid.)  Note that the amount to be accumulated must be input at run-time.

For each day your output should include the amount of the deposit for that day as well as the total balance in the bank up to that day.  Your output should appear in

tabular format as shown below:

Please enter the amount of money you want to accumulate: 1000000
Day        Deposit        Balance
1        0.01            0.01
2        0.02            0.03
3        0.04            0.07
4        0.08            0.15
…            …                …
It took XX days to accumulate at least $1000000.00 .

Your program should include a main ( ) function, a function to output the table header as well as a function to generate the table.  The function that generates the

table should be passed an input parameter of type double that tells how much money you want to accumulate.  The function output should line up the columns by decimal

place and should provide a return value to main ( ) that tells how many days it took to accumulate the funds.  this returned value should then be output as shown

above.

Remember that the main ( ) function should appear as the first function in the program.  Be sure to use function prototypes for each of the functions that are used in

your program.

Output from your program should be sent to the terminal window (your screen) as well as the requested csis.txt output file.  Be sure to read the document on Capturing

Program Output.  Your full name must appear as a comment in the source file that contains main ( ) .  Be sure to include the csis.txt output file in your zip archive.
Fibonacci Lab

The Fibonacci Sequence is the series of numbers:

0,  1,  1,  2,  3,  5,  8,  13,  21,  …

Each additional number in the sequence is found by adding the two previous numbers before it.

Write a program, fibonacci.c, that calculates the first 20 Fibonacci numbers.  Your program should include a function, fib ( ) , that receives an input parameter from

main ( ) telling the function how many Fibonacci numbers to output.

Remember that the main ( ) function should appear as the first function in the program.  Be sure to use function prototypes for each of the functions that are used in

your program.

Output from your program should be sent to the terminal window (your screen) as well as the requested csis.txt output file.  Be sure to read the document on Capturing

Program Output.  Your full name must appear as a comment in the source file that contains main ( ) .  Be sure to include the csis.txt output file in your zip archive.

Order from us and get better grades. We are the service you have been looking for.