0% found this document useful (0 votes)
133 views8 pages

Serial: CSC 103 Final Exam

Here is a program that reads data from the Description.txt file into 4 parallel arrays and displays the requested output: #include <iostream> #include <fstream> #include <string> using namespace std; int main() { const int MAX_RECORDS = 30; string names[MAX_RECORDS]; float widths[MAX_RECORDS], lengths[MAX_RECORDS], heights[MAX_RECORDS]; ifstream infile("Description.txt"); int count = 0; float totalLength = 0; while(count < MAX_RECORDS && infile >> names[count] >> widths[count] >> lengths[count] >> heights[

Uploaded by

bhg9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views8 pages

Serial: CSC 103 Final Exam

Here is a program that reads data from the Description.txt file into 4 parallel arrays and displays the requested output: #include <iostream> #include <fstream> #include <string> using namespace std; int main() { const int MAX_RECORDS = 30; string names[MAX_RECORDS]; float widths[MAX_RECORDS], lengths[MAX_RECORDS], heights[MAX_RECORDS]; ifstream infile("Description.txt"); int count = 0; float totalLength = 0; while(count < MAX_RECORDS && infile >> names[count] >> widths[count] >> lengths[count] >> heights[

Uploaded by

bhg9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Bahrain

College of Information Technology SERIAL


Department of Computer Science
First Semester, 2016-2017
Computer Programming for Scientists and Engineers

CSC 103
Final Exam

Date: 18th Jan 2017 Time: 8:30 - 10:30

STUDENT NAME

STUDENT ID # 2 0 SECTION #

QUESTION # MARKS COMMENTS

1 16

2 16

3 14

4 16

5 18

TOTAL 80

NOTE: THERE ARE EIGHT (8) PAGES IN THIS TEST


ONLY ONE SOLUTION WILL BE CONSIDERED FOR EACH QUESTION
LAST PAGE IS EMPTY

Page 1 of 8
Question 1 ( 16 marks )
Show the output of each code in the corresponding box to the right.

(1) bool OK=false; 7


int w=10;
Print
while ( w > 2 ) 4
{
w = w - 3; Print
cout<< w << endl; 1
if ( !OK ){
print
cout<<"Print"<<endl;
continue; }
}

(2) int num = 5; Number is 4


char let = 'F';
Outstanding
if ( 'B' < let ) {
--num;
cout<<"Number is "<< num << endl;
}
switch ( let ) {
case 'F':
cout<<"Outstanding";
break;
case 'M':
default:
cout<<"Not impressive";
}

(3) int m1, m2; 8


int Array[6]={5,8,10,13,2,2 };
for (int y=1; y<=5; y+=2) 9==1
{ 13
cout<< Array[y] <<endl;
m1 = y + Array[y]; 16==4
m2 = y + Array[y]%2; 2
if ( m1 < m2 )
cout<< m1 << endl; 7==5
else
cout<< m1 << "==" << m2 << endl;
}

Page 2 of 8
(4) void NumRef(double & P1, long & P2) 3.1 >> 7
{
string str = " >> "; 4.1 3
P1++;
cout<< P1 << str << P2 << endl;
P1++;
P2 = P2 % 5 + 1 ;
}
int main()
{
double C=2.1;
long D=7;
NumRef(C, D );
cout<< C <<"\t"<< D <<endl;
return 0;
}

(5) void check(int a, float b) 18 1


{
static float k = 5.0; 27 0
k += a + b;
int m = (a + 1) % 3;
cout<< k <<"\t"<< m <<endl;
}
int main()
{
int h = 4;
check(h, 9.0);
check(h, 5.0);
return 0;
}

Page 3 of 8
Question 2 ( 16 marks )
Write a C++ program that uses nested for-loops to display a shape of characters (see
examples). The program should ask the user to enter an integer n representing the number
of rows/columns in a square. The number n should be positive and not less than 5. If the user
input for n is invalid, then display an appropriate message and exit the program. The program
should display a square of size n filled with different characters. The second row and row
before last should be filled with the character (#). All other rows will be filled with the
character (*).
Examples
if n=4 if n=5 if n=6 if n=7
Error, small number * * * * * * * * * * * * * * * * * *
# # # # # # # # # # # # # # # # # #
* * * * * * * * * * * * * * * * * *
# # # # # * * * * * * * * * * * * *
* * * * * # # # # # # * * * * * * *
* * * * * * # # # # # # #
* * * * * * *

Answer
int main()
{
int n;
cout<<"Enter a positive number not less than 5 ";
cin >> n;

if ( n >= 5 ) {

for (int row=1; row<=n; row++)


{
for (int col=1; col<=n; col++)
if ( row==2 || row==n-1 )
cout<<"#";
else
cout<<"*";

cout<< endl;
}

}else
cout<<"Error, small number";

return 0;
}

Page 4 of 8
Question 3 ( 9 + 5 marks )
(A) Write a function called Admission that accepts three parameters: School Score 𝑺𝑺
(double), Written Test Score TS (double) and Interview Score IS (double). The function will
calculate total score as follows:
𝑡𝑜𝑡𝑎𝑙 𝑠𝑐𝑜𝑟𝑒 = 𝑆𝑐ℎ𝑜𝑜𝑙 𝑆𝑐𝑜𝑟𝑒 + 𝑊𝑟𝑖𝑡𝑡𝑒𝑛 𝑇𝑒𝑠𝑡 𝑆𝑐𝑜𝑟𝑒 + 𝐼𝑛𝑡𝑒𝑟𝑣𝑖𝑒𝑤 𝑆𝑐𝑜𝑟𝑒
Then the function will return admitted college (string) as follows:
total Score 𝒂𝒅𝒎𝒊𝒕𝒕𝒆𝒅 𝒄𝒐𝒍𝒍𝒆𝒈𝒆
≥ 90 Engineering
80 to 89 𝐼𝑇
< 80 Others

Answer
string Admission(double SS, double TS, double IS)
{

total = SS + TS + IS;
if ( total >= 90)
return "Engineering";
else if ( total >= 80 )
return "IT";
else
return "Others";
}

(B) Complete the below main program to prompt the user to enter the School score (double),
the Written Test Score (double) and the Interview Score (double). The program will then
display the admitted college by calling the function defined in Part (A). Follow the sample
below.

SAMPLE INPUT/OUTPUT
Enter School, Written test and Interview Scores:
65 12 14
Admitted College is Engineering
int main()
{
Answer
double SS, TS, IS;
cout<<"Enter School, Written test and Interview Scores:\n";
cin >> SS >> TS >> IS;

cout<<"Admitted College is "<< Admission(SS,TS, IS);

return 0; }

Page 5 of 8
Question 4 ( 16 marks )
Write a function named BusStatistics(), that takes as parameter a two-dimensional array
named bus of size 4x5. By referring to the given Figure, the rows in the array bus represent
the bus routes and the columns represent the days that the buses run. The numbers in the
slots of the array show the number of passengers that were on a given route on a given day.
The function will perform the following:
Mon Tue Wed Thu Fri
1. For each route, print the total number of 0 1 2 3 4
passengers. 0 8 12 9 7 10
2. For each route, find and print the largest
number of passengers.

Routes
1 5 7 3 1 4
3. For all routes and all days, find and print
the largest number of passengers. 2 20 15 18 31 14

3 6 9 0 8 11
Answer
void busStatistics(int bus[4][5])
{
int max=0;
int tmax=0;
float sum, total = 0;

for (int r=0; r<4; r++)


{
sum = 0;
max = bus[r][0]; // or zero
for (int c=0; c<5; c++) {
sum += bus[r][c];

if ( bus[r][c] > max )


max = bus[r][c];
}

cout<<"\nTotal number of passengers for route "<< r <<" is "<<sum ;


cout<<"\nLargest number of passengers for route "<< r <<" is "<<max;

if ( max > tmax )


tmax = max;
}

cout<<"The largest number of passengers for all days and all routes is "
<< tmax << endl;
}

Page 6 of 8
Question 5 ( 18 marks )
A file named Description.txt contains unknown number of metal sheets dimensions and
description (maximum no. of records is 30). Each line in the file consists of Metal sheet name
(string), width (float), length (float) and height (float). Write a program that reads the data
from the file into 4 parallel arrays. Your program should calculate and display the following
(see screen output):

 The number of Metal sheets in the file.


 The average length for all metal sheets.
 Metal sheets information (Metal sheet name and volume) for all items in the file. The
volume is equal to width × length × height.
 The metal sheet name with smallest volume.

Description.txt (Input file) Screen Output


ALUM1 1.25 2.25 .50 There are 4 metal sheets.
ALUM2 2.54 3.66 .93 The average length = 3.85 m2
ALUM3 1.4 .3 .2
Metal Name Volume
ALUM4 3.1 5.1 .74
ALUM1 1.40
ALUM2 8.64
ALUM3 0.08
ALUM4 11.69

ALUM3 has the smallest volume 0.08

300 is the maximum bidding price for seller


stockandship and buyer Redfish

Page 7 of 8
Answer
#include<fstream>
#include<iostream>
using namespace std;

int main()
{
ifstream fin;
fin.open("Description.txt");

string name[30];
float width[30];
float length[30];
float height[30];

int index, i = 0;
float sum = 0;
fin >> name[i];
while ( fin ){
fin >> width[i]>> length[i] >> height[i];

sum += length[i];
i++;

fin >> name[i];


}

cout<<"There are " << i <<" Metal sheets \n";


if ( i > 0 )
cout<<"The average length = "<< sum/i <<" m2\n";

float volume, min = 0;


cout<<"Metal Name \t Volume \n";

for (int j=0; j < i ; j++) {


volume = width[j]*length[j]*heigth[j];
cout<<name[j] <<"\t"<< volume <<endl;

if ( volume < min ) {


min = volume;
index = j;
}
}

cout<<name[index] <<" has the smallest volume "<< min << endl;

fin.close();
return 0;
}

Page 8 of 8

You might also like