Você está na página 1de 7

CIS 22A Midterm 2

Name: ___________________ left: right:

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; **
} ***

void displayAs(int cols)


{
for (int i = 0; i < cols; i++)
{
for (int n = 0; n <= i; n++)
cout << "*";
cout << endl;
}
}
Question #2: (20 pts)
Write a program that:
1) asks the user to enter 10 integers
2) prints out the values in the same order that the user has entered them
3) finds the index of the minimum value and the index of the maximum value
4) swaps the position of minimum and maximum values.
5) prints out the values with the new order
you must create an array and save the 10 integers in it.

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];

cout << "Please enter 10 integer numbers:" << endl;


for (int i = 0; i < SIZE ; i++)
cin >> arr[i];
cout << "Input: ";
for (int i = 0; i < SIZE ; i++)
cout << arr[i] << " ";

int lowIndex = 0, highIndex = 0;


for (int i = 0; i < SIZE ; i++)
{
if (arr[i] > arr[highIndex])
highIndex = i;
if (arr[i] < arr[lowIndex])
lowIndex = i;
}
int temp = arr[lowIndex];
arr[lowIndex] = arr[highIndex];
arr[highIndex] = temp;

cout << "\nOutput: ";


for (int i = 0; i < SIZE ; i++)
cout << arr[i] << " ";

}
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.";

void findAverage(float total, float numOfScores, float &average)


{
average = total / numOfScores;
}
Question #4(20): circle the true statements only :

1. The following statement will display 1 on the output screen.


cout << !( 6 > 7 || 3 == 4)

2. break statement causes a loop to terminate early.


3. cout << arr[3]; would print out the 4th element of arr, an array with 10 elements.
4. Giving this array definition int numbers[8] = {8} The following statement will display 8.
cout << numbers[5];

5. To allow file access in a program, you must #include <fstream>


6. It is possible for a function to have some parameters with default arguments and some without.

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.

Você também pode gostar