Sample Midterm
Sample Midterm
Sample Midterm
Question #1(35):
Show the output of the following codes. Assume that #include <iostream> and using
namespace std; is included. If you think the code has an error, fix it and show the output.
1)
int main()
{
int i, j;
for( i = 0; i < 3; ++i)
{
cout << "\n what’s up?";
if( i == 1)
break;
}
for ( j = 0; j < 3; j++)
{
cout << "\n Hey";
what's up?
if ( j % 2)
what's up?
continue;
Hey
cout << "\n you.";
you.
}
Hey
return 0;
Hey
}
you.
2)
void calcFuntion (int a, int& b)
{
int c;
c = a + 2;
a *= 3;
b = c - a; 1 0 10
return;
}
int main()
{
int x = 1, y = 2, z = 10;
calcFunction(x, y);
cout << x << " " << y << " " << z << endl;
return 0;
}
3) i is 7 j is 2
void p() i is 7 j is 3
{
int i = 6;
static int j = 1;
i++;
j++;
cout << "i is " << i << " j is " << j << endl;
}
int main()
{
int i = 10, j = 5;
p();
p();
return 0;
}
___________________________________________________________________________
4)
void f(int &p1, int p2)
{ x1 is 2 x2 is 1
p1++;
++p2;
}
int main()
{
int x1 = 1, x2 = 1;
f(x1, x2);
cout << "x1 is " << x1 << " x2 is " << x2;
return 0;
}
___________________________________________________________________________
5)
int main()
{
int x[3]; 4
int i;
for (i = 0; i < 3; i++)
x[i] = 2 * i;
cout << x[--i];
return 0;
}
6)
void xFunction(int num)
{
do
{
if (num % 2 != 0)
cout << num << " ";
num--;
} 1
while (num >= 1); 1
cout << endl; num is 3
}
int main()
{
int num = 1;
while (num <= 2)
{
xFunction(num);
++num;
}
cout << "num is " << num;
return 0;
}
_____________________________________________________________________
7)
void displayAs(int = 3);
int main()
{
displayAs(); . *
cout << endl; **
} ***
Sample run:
Please enter 10 integer numbers:
88 3 56 4 13 9 24 90 44 37
Input : 88 3 56 4 13 9 24 90 44 37
Output: 88 90 56 4 13 9 24 3 44 37
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 10;
int arr[SIZE];
}
Question #3: (25 pts)
Write a program that:
1. opens a file called “text.txt”. If the file doesn’t open successfully displays an error message.
2. reads scores from the file. This file contains an unspecified number of scores that are separated by blanks.
3. Sums the scores and saves them in a variable called total.
4. Finds how many scores are there in the file and saves it in a variable called numOfScores.
5. Calls “findAverage” function and passes three arguments to it.
6. Displays the average of the scores with one decimal place
Don’t use arrays
findAverage function:
1. receives three parameters;
2. calculates the average.
the header of the function:
void findAverage( float total, float numOfScores, float &average)
#include <iomanip>
#include <fstream>
using namespace std;
void findAverage( float, float, float &);
int main()
{
ifstream inFile;
inFile.open("text.txt");
float total = 0, numOfScores = 0, average = 0, score;
if(inFile)
{
while(inFile >> score)
{
total += score;
numOfScores++;
}
inFile.close();
findAverage(total, numOfScores, average);
cout << fixed << setprecision(1) << "The average: " << average;
}
else
cout << "The file can't be located.";
7. Given that x = 2, y = 1, and z = 0, the following cout statement displays 1 on the output screen.
cout << (x || !y && z) << endl;
8. The following statements checks if num greater than zero and less than 100.
if (num > 0 && <100)
9. The scope of a variable declared in a for loop's initialization expression always extends beyond the body of
the loop.
10. A function header eliminates the need to place a function definition before all calls to the function.