SYCS-135� Introduction to Programming

 

Quiz 5 - 10/14/99

 

Name:� ��������� Solutions������������������ ������������������ ���������

 

For questions 1 and 2 assume the following main function:

 

#include <math.h>

#include <iostream.h>

float loopit( int, float& );

void main ()

{

����� int m1 = 2;

����� float f1 = 4.0;

����� cout << �m1 = � << m1 << � f1 = � << f1;

����� cout << endl << loopit (m1, f1) << endl;

����� cout << �\nm1 = � << m1 << � f1 = � << f1;

}

 

1. [4] What will be printed out when the program executes using the following definition of loopit?

 

float loopit(int abc, float& qq)

{

����� if (qq <= abc || 3 < sqrt(qq) )

����������������� qq = qq - 1;

����� else qq = qq + 1;

����� return (qq - abc);

}

 

Answer:��������� ����������� m1 = 2 f1 = 4.0������� ����������� ����������������������� �����������

����������������������� ����������� 3.0������� ����������������������� ����������������������� �����������

����������������������� ����������������������� ����������������������� ����������������������� �����������

����������������������� ����������� m1 = 2 f1 = 5.0������������������� ����������������������� �����������

 

2. [6] What will be printed out when the program executes, using the following definition of loopit?

 

float loopit(int abc, float& pq)

{

����� int m = abc;

����� while (m > 0)

����� {

����������������� pq = pq + m;

����� ����� cout << � for m =� � << m << �� pq =� � << pq << endl;

����������������� m = m -1;

����� }

����� return pq;

}

 

Answer:��������� ����������� m1 = 2 f1 = 4.0������������������� ����������������������� �����������

����������������� ����� For m = 2 pq = 6.0����� ����������������� �����

����������������� ����� For m = 1 pq = 7.0����� ����������������� �����

����������������� ����� 7.0������� ����������������� ����������������� �����

����������������� ������������ m1 = 2 f1 = 7.0�������������������� �������������������������������.