306-135 Introduction to Programming

Quiz 4 – 2/18/99

Name:

For both questions 1 and 2 assume the following main function:

#include <iostream.h>

float loopit ();

void main ()

{

int i1 = 3;

float f1 = 4.0;

cout << loopit (i1, f1) << "i1=" << i1 << "f1=" << f1;

}

1. What will be printed out when the program executes, assuming the following definition of loopit ?

float loopit (int abc, float pq)

{

int jj = 1;

if (jj <= abc)

pq = pq+abc;

else

pq = pq-abc;

return pq;

}

Answer:

2. What will be printed out when the program executes, assuming the following definition of loopit and assuming that you enter at the keyboard the number 4 followed by the number 5.5?

float loopit ()

{

int abc; float pq;

cin >> abc >> pq;

cout << endl << "In loopit data read in are " << abc << " and " << pq;

if (abc != 0)

{

pq = pq+abc;

}

return pq;

}

Answer: