You are on page 1of 8

University of Bahrain College of Information Technology Department of Computer Science Semester II, 2010-2011 ITCS101/ITCS103 (Introduction to Computer Science

& IT/Computer Programming)

Final Exam
FORM A Date: 24/7/2011 Time: 11:30AM-1:30PM

STUDENT NAME STUDENT ID # SECTION #

NOTE: THERE ARE (8) PAGES IN THIS TEST WRITE ONLY ONE SOLUTION FOR EACH QUESTION

QUESTION # 1

MARKS 20

COMMENTS

14

16

16

14 80

TOTAL

Page 1 of 8

Question 1 [20 Points]


1- What is the output of the following code? int x=6; do{ x--; if (x>=4) continue; cout<<x<<endl; } while(x!=0); OUTPUT

2- What is the output of the following code? OUTPUT int int for { } for(int j=3; j>0; j-=2) { cout << y[j] <<'\t'<< x[2*j] <<'\t'<< x[2*j+1]; cout << endl; } y[4],x[]={7,8,9,3,6,7,2,10}; n=0; (int k=0; k<8; k+=2) y[n]= x[k]- x[k+1]; n++;

3- What is the output of the following code? void doThat (int& x, int y) { y+=3; x+=5; } void main() { int n1=7, n2=1; cout<<n1<<"\t"<<n2<<endl; doThat(n1,n2) cout<<n1<<"\t" <<n2<<endl; }

OUTPUT

Page 2 of 8

4- What is the output of the following code? void XV(int x){ static int f=0; f+=pow(x,2); cout<<f<<endl; } void main(){ for (int i=1;i<=3;i++) XV(i); } 5- What is the output of the following code? cout<<fixed<<showpoint; cout<<setprecision(1); cout<<ceil(4.2)<<endl; cout<<floor(-3.4)<<endl; cout<<left<<setfill('#'); cout<<setw(8)<<sqrt(25.0); OUTPUT OUTPUT

6- What is the output of the following code? int y=5; void main() { cout<<y<<endl; int y=9; cout<<y<<endl; y+=3; cout<<y<<endl; } 7- What is the output of the following code? int Matrix[3][3]={ {1,2,-3},{4,5,6},{-7,8,9} }; int i,j,k; OUTPUT for (i=0; i<3;i++) for (j=0;j<3;j++) if (Matrix[i][j] < 0) Matrix [i][j] = 0; for (k=0;k<3;k++) cout << Matrix[k][3-k-1]<< endl; OUTPUT

Page 3 of 8

Question 2 [14 Points]


Write a C++ program that prompts the user to input an integer greater than 0 which represents the size/height of the triangle shape. The program should print the following triangle shape according to the user input by alternating ! and + characters. Invalid error message must be displayed if the size is less than 1. Example: if the size of the shape = 4, then the program should output: ! !+! !+!+! ! + ! +! + !

Page 4 of 8

Question 3 [12+4=16 Points]


A customer survey collects feedback regarding the food quality in a restaurant. Each score is between -5 (bad) to +5 (excellent). There are 10 scores to be input by the user. PART A Write a C++ function named, getFoodQuality(), that prompts the user to input 10 scores of type integer. The function should calculate the average score and return the quality of the food based on the average score. If the average is less than -3.0, return bad, if less than 0.0 poor, if 0.0 average, if less than or equal to 3.0 quite good, if more than 3.0, excellent. Note: If a particular score is outside the range -5 to +5, then the score is assumed to be spoiled, and a score of 0 is used instead.

PART B Write a main function that calls the function defined in PART A and prints the quality of the food.

Page 5 of 8

Question 4 [16 Points]


Consider an existing file named cars.txt containing unknown number of lines. Each line contains information about one car as follows: colour(char), model(string), year(integer), price(double). Colour may be: R for red, B for blue, Y for yellow, or G for green; model may be: Honda, Rover, Ford, GMC. Write a C++ program that reads the contents of the file cars.txt into 4 parrallel arrays,
calculates and displays the averages price of all cars, and then displays cars information whose price is less than the average price as shown below. Assume the maximum number of

lines in the input file is 100. cars.txt (Input file)_


R Honda 2006 2150.00 R GMC 2008 6450.00 B Rover 2009 4750.00 .................... G Ford 2010 3750.00

Screen Output
Average Cars Price = 3950.00 Cars with price less than the Average: Name Year colour Price Honda 2006 R 2150.00 Ford 2010 G 3750.00

Page 6 of 8

Question 5 [14 Points]


Write a function, getBordersSum, that takes a 2-dimensional array of integers, row size, and column size as parameters. The function should calculate and return the sum of all integers around the borders of the array as shown in the example below: If the array size is 5x4 and contains the following numbers:
2 1 3 7 1 1 12 2 5 8 0 11 3 6 6 7 4 6 10 5

Then, the function should sum all the numbers around the array borders (see highlighted cells) and return the sum. In this example, the sum = 2+1+0+7+1+4+3+6+7+10+1+8+6+5= 61 Assume that the maximum number of columns in the array is 150.

Page 7 of 8

Page 8 of 8

You might also like