EXAMINATION PAPER AND ANSWER BOOKLET
COLLEGE OF Art and Science (CAS)
DEPARTMENT OF MAHEMATICAL AND PHYSICAL SCIENCES (DMPS)
Semester: Summer 2017 Academic Year: 2016-2017
Course Title: Introduction to Algorithms Course Code: COMP151
Midterm Examination II - Summer 2017 (Solution)
Exam Duration: 70 Minutes Exam Total Marks: 45
Date: August 10, 2017 (Thursday)
Time: 12.30 - 01.40 P.M.
Venue: 29A
Faculty: Dr. Khizar Hayat
Student Name: ______Student ID: ______
Student Signature:______Course Section #: ______
EVALUATION
Question Number / Maximum Mark / Marks ObtainedNumerical / Words
1 / 10
2 / 4
3 / 6
4 / 7
5 / 8
6 / 10
Total Marks / 45
Graded By: Dr Khizar Hayat Signature:______
Checked By: Dr Alaa Al Obaidi Signature:______
Answer all the questions. Enter your answers in the table given below.
Question 1 (10 Marks)
TRUE or FALSE
a) Suppose x=1; after the statement y=x--; executes, y is 0 and x is 0. (F)
b) The use of if clause without using else clause is not a syntax error. (T)
c) The statements cout<++i; and cout<i++; have the same output. (F)
d) cout<!true; (F)
e) The body of do while() loop may execute at least once. (T)
f) Using continue; would break a loop. (F)
g) The statement while(1){}; refers to an infinite loop. (T)
h) It is a syntax error if you don't end a case label of switch with break; (F)
i) The ternary operator ( : ? ) is used for repetition purposes. (F)
j) Suppose x=5; then the output of the statement cout<”x”; is 5 (F)
Question 2 (4 Marks)
Correct the errors in the following program:
#include<iostream>
using namespace std;
int main()
{
int x;
x=25;
if(x==0); //no semicolon
cout<x<endl;
else if(25-x=0); // Lvalue error. Use ==
cout<x=*1<endl; //should be *=
else
cout++x<endl; //should be <
return(0);
}
Question 3 (6 Marks)
What is the output of the following programs?
a.
#include<iostream>
using namespace std;
int main()
{
int x,y=2;
x=--y; Output
cout<x--<endl; 1
if(!x) 4
cout<x+4<endl; 0
else
cout<x+2<endl;
cout<x<endl;
return(0);
}
b.
#include<iostream>
using namespace std;
int main()
{ Output
int i=7; 7 i
do
{
cout<i--<”\t”;
}
while(i<5);
cout<”i"<endl;
return 0;
}
c.
#include<iostream>
using namespace std;
int main()
{ Output
int i=1; Afwan
while(i<0)
{
cout<”Shukran”;
}
cout<”Afwan"<endl;
return 0;
}
Question 4 (7 Marks)
Write a C++ program to compute the factorial of an input number:
#include<iostream>
using namespace std;
int main()
{
unsigned n, fact=1;
cout<"Enter the number\n";
cin>n;
cout<endl<n;
while(n>1)
{
fact*=n;
n--;
}
cout<"!="<fact<endl;
return(0);
}
Question 5 (8 Marks)
Write a C++ program to print the following pattern of any size, using a nested loop:
#include<iostream>
"" / "
" / " / "
" / " / " / "
" / " / " / " / "
" / " / " / " / " / "
" / " / " / " / " / " / "
using namespace std;
int main()
{
int n=7;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i+j>=n+1)
cout<"\"\t";
else
cout<" \t";
}
cout<endl;
}
return(0);
}
Question 6 (10 Marks)
Write a C++ code for a menu based program that continuously displays the following menu for area functions.
Main Menu
A. Area of rectangle
B. Area of a circle
C. Area of square
D. Area of a triangle
E. Exit
Enter a choice:
After user enters a choice, the program takes the input(s) and computes the area of the required shape accordingly.
#include<iostream>
using namespace std;
int main()
{
char choice;float area;
do
{
cout<"Main Menu\n";
cout<"\tA.\tSquare\n";
cout<"\tB.\tRectangle\n";
cout<"\tC.\tCircle\n";
cout<"\tD.\tTriangle\n";
cout<"\tE.\tExit\n\n";
cout<"My Choice\t";
cin>choice;
switch(choice)
{
case ‘a’:
case ‘A’:
float length, width;
cout<"Enter the length and width of the Rectangle\n";
cin>length>width;
area=length*width;
cout<"The area of the given Rectangle is "<area<endl;
break;
case ‘b’:
case ‘B’:
float radius;
cout<"Enter the radius of the Circle\n";
cin>radius;
area=3.14*radius*radius;
cout<"The area of the given Circle is "<area<endl;
break;
case ‘c’:
case ‘C’:
float side;
cout<"Enter the length of the side of the Square\n";
cin>side;
area=side*side;
cout<"The area of the given Square is "<area<endl;
break;
case ‘d’:
case ‘D’:
float base,height;
cout<"Enter the lengths of the base and height of the Triangle\n";
cin>baseheight;
area= base*height/2;
cout<"The area of the given Square is "<area<endl;
break; ;
case ‘e’:
case ‘E’:
cout<"The Quitting\n;
default:
cout<"Invalid Choice\n";
}
}while(ch=='e'||ch=='E');
return 0;
}
5