Sei sulla pagina 1di 7

University of Tripoli / GS200 / Spring2017 / Date:04-6-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++; [3.75 marks]
goto x;
z:cout<<"good bye";

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 << " "; } [3.75 marks]

cout << "\n";}

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

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

1 of 7 Pages
b) Rewrite the following program without using the commands if-else.
int h;
int h; cin>>h;
cin>>h; (h%2)?cout<<"even"<<endl:cout<<"odd"<<endl;
[3.75 marks]
if(h%2) cout<<even<<endl;
else cout<<odd<<endl;
c) Rewrite the following program without using the command isdigit( ).
char str[80]; char str[80];
cin.getline(str,80); cin.getline(str,80);
for(int i=0;i<strlen(str);i++){
for(int i=0;i<strlen(str);i++){
if( '0'<=str[i]<='9') str[i]++;}
if(isdigit(str[i])) str[i]++;}
cout<<str;
cout<<str; [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;
}
}
int a, x, y; cout<<"Enter any
cout<<"Enter the operation number. 1 or number"<<endl;
2"<<endl; cin>>x;
cin>>a; cout<<"Thank you";
switch(a) break;
{ default :
case 1: cout<<"no operation"<<endl;
cout<<"Enter two numbers"<<endl;
cin>>x>>y; }
cout<<"X="<<x<<endl<<"Y="<<y; [3.75 marks]
break;
case 2:

2 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 _____This is not a **** number


2) 7 _____This is a **** number
3) 1 _____This is a **** number

In your words, what does this program do? [1 mark each]


check whether the entered number is Prime number or not, if prime it will
display This is a **** number else it will display this is not a ****
number.
[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____1496_____________________________________________
2) 14520172016 __4012____________________________________________
3) 224444_______88___________________________________[1 mark each]
In your words, what does this program do?
_________________________________________________________________________

3 of 7 Pages
_________________________________________________________________________
_______________________________[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 ______3______________________________________
2) ahmedmohamed_____1_____________________________________
3) noyes_____________0_____________________________________
[1 mark each]
In your words, what does this program do?
it counts the number of duplication of string A in string B element
[2 marks]

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:

4 of 7 Pages
#include <iostream>
#include <ctype.h>
void main()
{ char A [80];
cout<<"please type a sentence\n";
cin.getline(A,80);
int r=0, x=0, y=0,i=0;
while (1) {
if(isdigit(A[i]))
r++;
else if (isalpha(A[i]))
x++;
else if (isspace(A[i]))
y++;
i++;
if (A[i]=='\0')
break;
}
cout<<"the number of digits is : "<< r;
cout<<"\nthe number of letters is : "<< x;
cout<<"\nthe number of spaces is : "<< y;
cin.get();
cin.get();
}
[7.5 marks]
b) write a program that takes an integer then finds the length and the reverse of that integer. as shown in the
example.
Example:

5 of 7 Pages
#include <iostream>
void main()
{ int r=0, x=0,z;
cout<<"the number is";
cin>>z;
r=z;
while (r!=0) {r/=10;
x++;
}
cout<<"the lenght is : "<< x;
cout<<"\nthe reverse is : " ;
while (z!=0){cout<<(z%10);
z/=10;
}
cin.get();
}

Other easy solution ,but will not accepted cause it gives a different
output format.

#include <iostream>
void main()
{ int r=0, x=0;
cout<<"the number is";
cin>>r;
cout<<"\nthe reverse is : " ;
while (1) {cout<<(r%10);
r/=10;
x++;
if(r==0)
break;
}
cout<<"\nthe lenght is : "<< x;

6 of 7 Pages
cin.get();
}
_________________________________________________________________________
___________________________[7.5 marks]

7 of 7 Pages

Potrebbero piacerti anche