Vous êtes sur la page 1sur 7

University of Tripoli / Electrical and Electronic Engineering Department GS200/Spring2017

First Mid-term Exam


Name: ID:...Group:

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]

2) float a=5,b=3,c=8;int d=c++;char x=a;


cout<<(d==c)<<endl<<(x+1!=b)<<endl<<(c/a);
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________ [2.5 marks]

3) unsigned int a=25, e=10; float b=2./3.; bool c=true;


char d='h';
cout<<setfill('^)<<setw(20)<<"Welcome"<<setw(5)<<"m\n"<<
a <<"\n"<< e\b <<endl<<c<<endl<<d<<endl<<e<<endl;
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________ [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]

3) j=[ones(2),zeros(2);1:4], h=j([2 5 8 12]);


k=j([2 3],[1 3]); b=h.*k(:); disp(b);
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
___________________________________________________[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);

what does the screen display if the user inputs:

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

what does the program display if the user inputs:


1) abcdabcd ________________________________________________
2) yeswecanYesWeCan _________________________________________
3) ALIALI1 __________________________________________________
[1 mark each]

3 of 7 Pages
from the results, what is the function of the program?
______________________________________________________________
______________________________________________________________
____________________________________________________ [2 marks]

c) for the following program:


vec=input('please enter a vector of numbers');
m = vec(1);
for i = 2:length(vec)
if vec(i) < m
m = vec(i);
end
end
fprintf('%0.1f',m);

what does the program display if the user inputs:


1) [8, 7, 6, 5, 4, 3, 2] ____________________________________________
2) 5 : -2 : -10 _________________________________________________
3) [] _______________________________________________________
[1 mark each]
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?

n=input('please enter a number ');


if(rem(2,n)==0)
disp('the number is even');
else
disp('the number is odd');
end
command window:

please enter a number 8


the number is odd

[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?

k=input('please enter a num');


for c=1:k
f=f*c;
end

[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

Vous aimerez peut-être aussi