Sie sind auf Seite 1von 7

University of Tripoli / GS200 / Spring2017 / Date:28-5-2017

Electrical and Electronic Engineering Department Total: 60Marks. Time :2 hours

Second Mid-term Exam


Name: ID:...Group:

Q1) for the following programs write down what will be displayed on the screen if they were
executed:
1) char a[80]="hello", b[80]="yes", c[80]="day";
cout<<strncpy(a,b,1)<<endl<<strcat(a,c)<<endl;
cout<<strcmp(a,b)<<" "<<strlen(a);
______________________________________________________________
______________________________________________________________
__________________________________________________[3.75 marks]

2) int f1=10, f2=2, f3=0;


w:cout<<f1++;
goto y;
x:if(f2--<=0)goto z;
goto w;
y:cout<<f3++;
goto x;
z:cout<<"good bye";
______________________________________________________________
______________________________________________________________
__________________________________________________[3.75 marks]

3) for (int y=6;y >= -6;y-=3) {


for (int x=-6;x <= 6;x+=3) {
if ((int) sqrt (pow(x,2)+pow(y,2))==6) cout << "*";
else cout << " ";
}
cout << "\n";
}
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________[3.75 marks]

1 of 7 Pages
4) int i=5;
do {
int j=0;
do {
j++;
(i%2) ? cout<<'<':cout<<'>';
} while(j!=i);
cout<<endl;
i--;
} while(i>0);
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
_________________________________________________ [3.75 marks]

Q2) a) Rewrite the following program without using the command for( ).
_________________________________
for(int i=1;i<100;i++){ _________________________________
x=i*i; _________________________________
cout<<i<<^2<<x<<endl; _________________________________
} _____________________[3.75 marks]

b) Rewrite the following program without using the commands if-else.


________________________________
int h;
________________________________
cin>>h;
________________________________
if(h%2) cout<<even<<endl;
________________________________
else cout<<odd<<endl;
____________________[3.75 marks]

c) Rewrite the following program without using the command isdigit( ).


char str[80];
cin.getline(str,80);
for(int i=0;i<strlen(str);i++){
If(isdigit(str[i])) str[i]++;
}
cout<<str;

2 of 7 Pages
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________[3.75 marks]

d) Rewrite the following program without using the commands if-else.


int a, x, y;
cout<<Enter the operation number. 1 or 2<<endl;
cin>>a;
if (a==1){
cout<<Enter two numbers<<endl;
cin>>x>>y;
cout<<X=<<x<<endl<<Y=<<y;
}
else{
if(a==2){
cout<<Enter any number<<endl;
cin>>x;
cout<<Thank you;
}
else{
cout<<no operation<<endl;
}
}
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ ______________________________
______________________________ __________________[3.75 marks]

3 of 7 Pages
Q3) a) for the following program:
int num, i;
bool P = true;
cout << "Enter a positive integer: ";
cin >> num;
for(i = 2; i <= num / 2; ++i){
if(num % i == 0){
P = false;
break;
}
}
if (P) cout << "This is a **** number";
else cout << "This is not a **** number";
what does the screen display if the user inputs:

1) 6 _________________________________________________________
2) 7 _________________________________________________________
3) 1 _________________________________________________________
[1 mark each]
In your words, what does this program do?
______________________________________________________________
______________________________________________________________
_____________________________________________________[2 marks]
b) for the following program:
int param1, param2;
cout<< "enter the first number" << endl;
cin>> param1;
cout<< "enter the second number" << endl;
cin>> param2;
int i=0, ans=0;
while (param1||param2) {
ans+=(((param1%10) *(param2%10)) %10) *pow (10, i);
param2/=10;
param1/=10;
i++;
}
cout<<ans;

what does the program display if the user inputs:


1) 12341234_________________________________________________
2) 14520172016 ______________________________________________
3) 224444__________________________________________________
[1 mark each]

4 of 7 Pages
In your words, what does this program do?
______________________________________________________________
______________________________________________________________
_____________________________________________________[2 marks]
c) for the following program:
char A [80], B [80];
cin.getline(A,80);
cin.getline(B,80);
int r=0, x=0, y=0;
while (1) {
for (int i=0; i<strlen(A); i++) {
for (int j=0; j<strlen(B); j++) {
if(A[i]==B[j]) {
for (int k=j; k<strlen(B); k++)
B[k]=B[k+1];
x=1;
break;
}
}
if(x==1) {
x=0;
continue;
}
else {
y=1;
break;
}
}
if(y==0) r++;
else break;
}
cout<< r;
what does the program display if the user inputs:
1) abcabcabcabc ____________________________________________
2) ahmedmohamed__________________________________________
3) noyes__________________________________________________
[1 mark each]
In your words, what does this program do?
______________________________________________________________
______________________________________________________________
____________________________________________________ [2 marks]

5 of 7 Pages
Q4) a) write a program that takes a string then finds the number of number digits, letters and
spaces in that string. as shown in the example.
Example:

______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
___________________________________________________[7.5 marks]

6 of 7 Pages
b) write a program that takes an integer then finds the length and the reverse of that integer. as
shown in the example.
Example:

______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
___________________________________________________[7.5 marks]

7 of 7 Pages

Das könnte Ihnen auch gefallen