NAME:
CSC126

Exam 1

Part I

Trace the following programs (5 points each):

1. 
#include<iostream>
using namespace std;
int main()

{
	int i,j=10;
	cout<<"Keep on trying....";
	for (i=0; i < 6; i++)
	{
		cout<<"i is now "<<i<<endl;
		j = j + i;
		cout<<j<<endl;
		if (j < 14)
			cout<<"Keep on trying......";
	}
	return 0;
}

2. Trace with input: 10 2.34567 9.1

#include<fstream>
#include<iomanip>
using namespace std;

int main()

{
	int x;
	float y,z,total;

	cin>>x>>y>>z;;

	cout<<setprecision(2)<<y<<"  "<<z<<'\t'<<y+z<<endl;

   total = y + z;
	cout<<setprecision(2)<<y<<"  "<<z<<'\t'<<total<<endl;
	cout<<setprecision(3)<<x<<"  "<<y<<"  "<<z<<'\t'<<'\t'<<y+x+z<<endl;

	cout<<setw(10)<<'*';
	cout<<endl;
	cout<<setfill('*')<<setw(10)<<'*';
	cin>>x>>y>>z;

}

Part II
Multiple Choice (3 points each)

Identify the letter of the choice that best completes the statement or answers the question.

____	1.	The value of the C++ expression 14 / 4 + 4.3 is:
a.	7	
b.	7.3	
c.	7.8	
d.	None of these	

____	2.	Suppose that x, y, and z are int variables. The expression x(y+z) in C++ is written as
a.	x * y + z	
b.	x * (y + z)	
c.	y + x * z	
d.	None of these	

____	3.	Suppose that x and y are int variables. Which of the following is a valid input statement?
a.	cin>>x>>cin>>y;	
b.	cin>>x>>y;	
c.	cin<<x<<y;	
d.	None of these	

____	4.	Suppose that alpha, beta, and gamma are int variables and the input is

100 110 120
200 210 220
300 310 320

What is the value of gamma after the following statements executes?

cin>>alpha;
cin.ignore(100,'\n');
cin>>beta;
cin.ignore(100,'\n');
cin>>gamma;

a.	100	
b.	200	
c.	300	
d.	None of these	

____	5.	Suppose that x is a double variable and x = 76.7. Which of the following statement outputs 77?
a.	cout<<static_cast<int>(x);	
b.	cout<<static_cast<int>(x + 0.5);	
c.	cout<<static_cast<int>(x) + 0.5;	
d.	None of these	

____	6.	Suppose that x = 55.68, y = 476.859, and z = 23.8216. What is the output of the following statements?

cout<<fixed<<showpoint;
cout<<setprecision(3);
cout<<x<<' '<<y<<' '<<setprecision(2)<<z<<endl;

a.	55.680 476.859 23.82	
b.	55.690 476.860 23.82	
c.	55.680 476.860 23.82	
d.	None of these	

____	7.	Suppose that x = 476 and y = 38. What is the output of the following statements?

cout<<"12345678901234567890"<<endl;
cout<<left<<setw(5)<<x
    <<setw(5)<<y
    <<setw(7)<<"Hello"<<endl;

a.	12345678901234567890
476  38     Hello	
b.	12345678901234567890
476  38   "Hello"	
c.	12345678901234567890
476  38   Hello	
d.	None of these	

____	8.	Suppose that x = 87 and y = 423. What is the output of the following statements?

cout<<"12345678901234567890"<<endl;
cout<<setw(5)<<x
    <<left<<setw(5)<<y
    <<setw(7)<<"Sunny"<<endl;

a.	12345678901234567890
   87423  Sunny	
b.	12345678901234567890
   87  423  Sunny	
c.	12345678901234567890
87   423   Sunny	
d.	None of these	

____	9.	What is the output of the following C++ code?
	
	int x = 35;
	int y = 45;
	int z;

	if(x > y)
	   z = x + y;
	else
		z = y – x;

	cout<<x<<" "<<y<<" "<<z<<endl;

a.	35 45 80	
b.	35 45 10	
c.	35 45 –10	
d.	None of these	

____	10.	What is the output of the following code fragment if the input value is 4?
      
int num;
int alpha = 10;
cin>>num;
switch(num)
{
case 3: alpha++;
        break;
case 4: 
case 6: alpha = alpha + 3;
case 8: alpha = alpha + 4;
	  break;
default: alpha = alpha + 5;
}
cout<<alpha<<endl;

a.	13	
b.	14	
c.	15	
d.	17	

____	11.	Suppose j, sum, and num are int variables, and the input is

26  34  61  4  -1 

What is the output of the following code?

sum = 0;
cin>>num;
for(j = 1; j <= 4; j++)
{
sum = sum + num;
cin>>num;
}
cout<<sum<<endl;

a.	124	
b.	125	
c.	126	
d.	None of these	

____	12.	What is the value of counter after the following statements executes?

counter = 0;
while(counter <= 50)
      counter = counter + 3;

a.	48	
b.	50	
c.	51	
d.	None of these	

Part  III 
Short Coding Questions (5 points each)

1.   Convert from a switch to an if else:

switch (x)
{
   case 3:
              y++;
   case 4:
              z--;
              break;
   default:
             cout<<”end”;
}

2.	Convert from an if else to a switch:

if (x == 10 || x == 7)
   cout<<”first condition true”;
else if (x < 10 && x > 7)
   cout << “second condition true”;
else 
   cout << “no condition true”;

3.Use a loop to print out all the numbers between 1 and 500.

4.   Use nested loops to print out this:

10
   9
     8
       7
         6
           Blast off!

5.   Assume your boss has told you that pay and a ½ is given for overtime.  If the variables pay, hours,and  overtime, hold pay per hour, total hours worked, and overtime hours, give a statement to compute salary.

6.	Assume your boss has told you to create a list of phone numbers of people who are between 18-65, and who own a car.  If the bool variable own is true if the person owns a car and false otherwise, and int variable age hold the age, what would the if statement look like?

7.	When are the following necessary?  

a.	#include<iostream>

b.	#include<iomanip>

c.	#include<fstream>

Part IV
Program (19 points)

Assume that we have a file with 4 lines.  Each line has the name of a division in our company, and the profits that the company made over the last year.

The first two lines might be:

Piscataway      15600
Edison              12300

You MAY assume that the names have NO blanks in them.

Write a program that reads in the four lines of data and prints out each line nicely with total profits and average profits as follows:

DIVISION                        PROFITS
Piscataway                       15600
Edison                               12300
------
----

TOTAL PROFIT         -----             
AVERAGE                  ------

Here is a step-by-step method to do this:
1.	Opens the input file
2.	In a loop (OR IF YOU WISH YOU CAN DO THIS WITHOUT A LOOP JUST DO IT FOUR TIMES OVER):
a.	reads a line of the data
b.	prints out the line in nice format
c.	adds the profit to the  total

3.	Print out the total profits for all the parts of the company
4.	Compute and print the average profits for all parts of the company