OBJECT-ORIENTED DESIGN 273-422 Midterm Exam 2 hours
There are 4 questions. The percentage of marks is indicated for each question.
NAME: ______________________________ STUDENT #: __________________
[20] 1. Write a C++ function 'print_pattern() which uses nested 'for' loops
to display patterns like the following:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
The function should take one integer parameter which is the number
of lines to be printed (e.g. 5 to produce the above pattern).
Note: no main() function is required for this question.
[25] 2. What would be printed by the following C++ program ?
#include <iostream.h>
int calc( int n )
{
static int sum = 0;
sum += n;
if ((n++ % 2) == 0)
return (sum);
else
return (sum + 1);
}
int main( void )
{
cout << calc( calc(5) ) ;
return 0;
}
[35] 3. Write a C++ function which, given an array of integers (range: 0-100) representing the marks of a class of programming students, calculates the average mark and two other pieces of information: the number of students who failed (mark less than 50), and the number of students who got A (mark 80 or more).
This function should be called 'marks', should be of return type 'void' and have 5 parameters:
The 2 input parameters to this function should be the array of marks and the number of elements in that array.
The 3 pointer output parameters to this function should be the average mark, the number who failed, and the number who got A.
(Do not use global or file-scope variables. Do not use reference parameters. This function should not print anything.)
Note: the average of a set of values is the sum of those values divided by the number of values.
Note: you are just asked to write this function, not a whole program, so no main() function is required.
[20] 4. The following C++ program has at least 9 errors (syntactic, semantic, etc).
Indicate the errors by circling them (where appropriate) and giving a short explanation of what is wrong.
#include <iostream.h>
#include <string.h>
int main( void )
{
char buffer[10] = "dictionary",
ptr; /* an abbreviation for pointer */
double num1;
ptr = buffer;
while ( *ptr != '0' ) ;
{
double num2;
num2 = strlen( ptr );
cout << divide( num1, num2 ) ;
ptr++;
}
return ( num2 );
}
static double divide( double a, b )
{
if ( b != 0.0 )
return ( a / b );
}