GS200 First Exam
GS200 First Exam
GS200 First Exam
Q1) for the following programs write down what will be displayed on the screen if they were
executed:
Part A C++ programs:
1) cout<<\thello <<endl;
cout<<this is your first exam<<ends
<<the first question is easy;
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________ [2.5 marks]
1 of 7 Pages
Part B MATLAB programs:
1) t=linspace(0,2*pi,5);
plot(t,sin(t));
hold on;
plot(t,cos(t));
hold off;
xlabel('Time'); ylabel('Amplitude');
title('Waves'); legend('Sin','Cos');
xlim([0 2*pi]);ylim([-0.5 0.5]);
[2.5 marks]
2) r=rand(1,10); r=r+1;
r=floor(r); u=find(r>0.5);r(u)=-1;
disp(r);
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________ [2.5 marks]
2 of 7 Pages
Q2) a) for the following program:
char=input('please enter a character','s');
u=false;
if ((char>='A')&&(char<='Z'))
u=true;
elseif ((char>='a')&&(char<='z'))
u=false;
else
disp('this is not a letter');
end
disp(u);
1) T _________________________________________________________
2) n _________________________________________________________
3) 9 _________________________________________________________
[1 mark each]
from the results, what is the function of the program?
______________________________________________________________
______________________________________________________________
____________________________________________________ [2 marks]
b) for the following program:
str1=input('enter the first word','s');
str2=input('enter the second word','s');
k=0;
l1=length(str1);
if(l1==length(str2))
for i=1:l1
if(str1(i)~=str2(i))
k=k+1;
end
end
disp(k);
else
disp('the lengths of the words are not the same');
end
3 of 7 Pages
from the results, what is the function of the program?
______________________________________________________________
______________________________________________________________
____________________________________________________ [2 marks]
Q3) for the following programs either add or modify a line of code so it run successfully
a) the following C++ program was written to find the average of three numbers but when
running the program, it gives a wrong answer. what should you do to get the wright answer?
#include<iostream>
#include<conio.h>
int main(){
int a,b,c;
cout<<"Please enter three numbers"<<endl;
cin>>a>>b>>c;
int d=a+b+c/3;
cout<<the average is <<d<<endl;
getch();
}
4 of 7 Pages
[3 marks]
b) the following C++ program was written to display a sentence on the screen but at the
compilation phase an error popped up. what should you do to remove the error?
#include<iostream>
#include<conio.h>
int main{
int a,b,c;
cout<<"Hello World!"<<endl;
getch();
}
[3 marks]
c) the following C++ program was written to take the student ID and the name of a student then
type it in a certain format. but when running the program, it doesnt print the student name.
what should you do to make work correctly?
#include<iostream>
#include<iomanip.h>
#include<conio.h>
int main(){
char name[50];int id;
cout<<"Please enter the student ID(8 digits)"<<endl;
cin>>id;
cout<<"please enter the student name"<<endl;
cin.getline(name,20,'\n');
cout<<setw(10)<<setfill('#')<<id<<setw(10)<<name<<setw(3)<<" ";
getch();
}
5 of 7 Pages
[3 marks]
d) the following MATLAB program was written to check whether a number is even or odd .
but when running the program, it gives wrong results. what should you do to make give right
results?
[3 marks]
e) the following is used to calculate the factorial of a number, but when running the program
is gives an error. what should you do to remove this error?
[3 marks]
6 of 7 Pages
Q4) a) Draw a flow chart and write the algorithm then write a program that takes the name and
the ID of a student and the number of units of each subject he/she studied and the result of each
subject then calculates the total number of units and the cumulative average of that semester
( ) then prints the result as shown below:
+++++++++++++++++++++++++++++
student name: Nuri Benbarka
student id: 21714441
*****************************
total number of units: 18
cumulative average: 75.2778%
[7.5 marks]
b) Draw a flow chart then write a program in MATLAB that takes seconds then converts them
into days, hours, minutes, seconds as the examples below:
Example 1: Example 2: Example 3:
Input: Input: Input:
23 3601 184210
Output: Output: Output:
seconds=> 23 seconds=> 1 seconds=> 10
minutes=> 0 minutes=> 0 minutes=> 10
hours=> 0 hours=> 1 hours=> 3
days=> 0 days=> 0 days=> 2
[7.5 marks]
7 of 7 Pages