Professional Documents
Culture Documents
Exam 1 Spring 2016 Solution: 3. Source Code - Compile - Link - Execute
Exam 1 Spring 2016 Solution: 3. Source Code - Compile - Link - Execute
Exam 1 Spring 2016 Solution: 3. Source Code - Compile - Link - Execute
4) One of the following C++ libraries has to be included before your program can
use toupper(ch) system function
1. iostream
2. cmath
3. iomainp
4. cctype
int temp;
temp=M;
Swap the contents of the double M=n;
2 variables “M” and “n”. (Declare N=Temp;
additional variables, if necessary.)
if (gender==’M’)
cout<<”Male”;
On the output screen, print “Male” if else if (gender==’F’)
cout<<”Female”;
the gender is 'M', “Female” if the else
5 gender is 'F', and invalid gender cout<<” invalid gender”;
otherwise. Assume gender is already
declared character variable
EXAM 1 SPRING 2016 SOLUTION
Question 2 (10 Points):
[A] (5 Points) Circle five (5) syntax errors in the following C++ program and
rewrite the corrected line in the provided space in the next column.
Rewrite the whole line (if any error exist)
1 #include <iostream>
6 extra_Bonus(0), total_Bonus(0);
8 and KPI:";
9 cin>>salary,KPI; cin>>salary>>KPI;
10 if (KPI <=0)
12 if (KPI>2)
13 { basic_Bonus=500;
14 extra_Bonus=salary/2.0;;
15 }
16 else
17 BASIC_BONUS=300; basic_Bonus=300;
18 total_Bonus=basic_Bonus+extra_Bonus;
19 cout<< Total Bonus is: "<<total_Bonus; cout<< “Total Bonus is: "<<total_Bonus;
Return 0; return 0;
}
EXAM 1 SPRING 2016 SOLUTION
[A] (5 Points) write a complete C++ program that:
Write a complete C++ program that reads time components (hours, minuets, second)
from the user as three integer values and then the program should compute the total
number seconds. Your program should give appropriate message in case of negative
input values or unrealistic values for time (i.e. hour> 24; minuets>60; second >60).
#include <iostream>
using namespace std;
int main()
{
// Declarations of all variables
int hours, minutes, seconds, totalSeconds; //[0.5]
//read hours, minutes and seconds values from user
cout<<"Please enter hours, minutes and seconds separated by space:"; //[0.5]
cin>>hours>>minutes>>seconds;
//Check that hours, minutes, and seconds are valid (e.g hours within 0-23)
if (hours<24 && minutes<60 && seconds<60) //[1]
{ //calculate totalSeconds.
totalSeconds=hours*3600+minutes*60+seconds; //[1]
return 0;
}
EXAM 1 SPRING 2016 SOLUTION
Question 3 (10 Points)
[A] Trace output of the following code. (5 Points)
int temp;
char weather;
if (temp < 5)
weather='C';
else if(temp>=5 && temp < 15)
weather='c';
else if(temp>=15 && temp<=28)
weather='M';
else
weather ='H';
cout<<fixed;
switch(weather)
{
case 'C':
case 'c': cout<<temp * 1.8 + 32<<"Celsius"<<endl;
break;
case 'M': cout<<setw(10)<<right<<setprecision(1)
<<temp * 1.8 + 32<<"\n\n";
break;
case 'H': cout<<setw(15)<<setprecision(3)<<temp * 1.8 + 32;
cout<<"Bye"<<endl;
return 0;
}
B y e
#include<iostream>
using namespace std;
int main()
{
int op;
cout<<"Enter code : " ;
cin>>op;
return 0; }
Read op
True/yes
if(op>=1 &&
op<=3) Print "Normal Operatoins"
False/no
True/yes
if (op==4 ||
Print "Maintenance Required” "
op==5)
False/no
Exit