0% found this document useful (0 votes)
98 views6 pages

02 Prog I Examinations

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

02 Prog I Examinations

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

University of Modern Sciences & Arts (MSA) End-Term Examination-Fall 2004-2005

Fundmentals of computing for engeeris (CS-101) Time Allowed: 3 Hours

SECTION (I): TRACING PROBLEMS (Total: 9 marks)


Display the output of the following programs:

(Q1) (3 marks)
#include <iostream.h>

void main()
{
char x[] = {'a', 'b', 'c', 'd', '\0'};
char y[] = {'k', 'l', 'm', 'n', '\0'};

int i =0;
char t;

while( i <2)
{
t = x[i+2];
x[i+2] = y[i];
y[i] = t;

i++;
}

y[i] = '\0';
cout<<x<<endl;
cout<<y<<endl;

i = 0;
while(i<2)
{
t = y[i+2];
y[i+2] = x[i];
x[i] = t;

i++;
}
cout<<x<<endl;
cout<<y<<endl;

Page 1 of 6
University of Modern Sciences & Arts (MSA) End-Term Examination-Fall 2004-2005
Fundmentals of computing for engeeris (CS-101) Time Allowed: 3 Hours

(Q2) (3 marks)
#include <iostream.h>

void DoIt(int x[], int v) void main()


{ {
for (int i=0; i<5; i++)
{ int x[] = {10,20,30,40,50};
v+=x[i];
} DoIt(x, x[2]);
}
Display(x);
void Display(int x[]) }
{
for (int i=0; i<5; i++)
{
cout<<x[i]<<endl;
}
}

(Q3) (3 marks)

#include <iostream.h>

void Come(int x[], int &v)


{
int t = x[0];
x[0] = v;
v = t;
}

void main()
{
int x[] = {10,20,30,40};

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


{
Come(x, x[i]);
}

for (i=0; i<4; i++)


cout<<x[i]<<endl;
}

Page 2 of 6
University of Modern Sciences & Arts (MSA) End-Term Examination-Fall 2004-2005
Fundmentals of computing for engeeris (CS-101) Time Allowed: 3 Hours

SECTION (II): PROBLEM SOLVING (Total: 17 marks)


Problem 1: (10 marks)
Write a program to do the followings:
 Read an array of values with size = 100. (X)
 Read another array of values with size = 100. (Y).
 Generate a third array of (Pos) as the following protocol:
X:
1 50 7 13 3 14 60 8
2 4

Y:

0 1 2 3 4 5 6 7
1 60 50 8 12 28 34 7
3

Your Generated array (Pos):


Pos:
4 2 7 0 6 -1 1 3
As you show, each value in X , will be saved as its position in Y:
12 à at position 4
50 à at position 2
7 à at position 7
13 à at position 0
34 à at position 6
14 à not founded in Y , so set its position as (-1).
60 à at position 1
8 à at position 3

 Swap each 2 adjacent cells in Y array.


 Reverse X array.
 Re-Generate the Pos array.
X:
8 60 14 34 13 7 50 12

Y:
0 1 2 3 4 5 6 7
6 13 8 50 2 12 7 34
0 8

Pos:
2 0 -1 7 1 6 3 5

 Make the user enter new values of (Y).


 Re-Generate the Pos array.
 Make the user enter new values of (X).
 Re-Generate the Pos array.
 Swap each 2 adjacent cells in X array.
 Reverse Y array.

Page 3 of 6
University of Modern Sciences & Arts (MSA) End-Term Examination-Fall 2004-2005
Fundmentals of computing for engeeris (CS-101) Time Allowed: 3 Hours

 Re-Generate the Pos array.


Problem 2: (7 marks)
Write a program to do the followings:
 Read a string from the user. (Letters).
 Read an array of values with size = 100 (Weights).

Each value in Weights, will assigned to the corresponding letter in Letters:

Letters:
A W E S I R Y N T \0 .. .. ..

Garbage

Weights: 8 10 5 7 2 4 3 9 12 0 0 0 0

 After reading the Weights array you have to set values that correspond to the garbage to
zero.

 Read another string from the user (X).


 Calculate the weight for each word in the string X.

Y E S S I R W E W A N T \ .. ..
0
3 5 7 7 2 4 10 5 10 8 9 12

15 11 15 39

Page 4 of 6
University of Modern Sciences & Arts (MSA) End-Term Examination-Fall 2004-2005
Fundmentals of computing for engeeris (CS-101) Time Allowed: 3 Hours

SECTION (III): FUNCTION DESIGN (Total: 14 marks)


Function 1: (7 marks)

Design a function according to the following aspects:


Input:
 A string. (X)
 A letter. (L)
Output:
All distances between each 2 occurrences of (L).

Example:
X:
A E S A A K H M A y \0 .. ..

2 1 4

your return will be the values : 2 , 1, 4.

Function 2: (7 marks)

Design a function according to the following aspects:


Input:
 An array of values (X).
 The number of cells in the array. (N)
 A target value. (T)

Output:
 How many occurrences of T in X.
 The first position of T.

Example:

X:
16 1 10 7 10 1 22 6
0 9

N:8
T : 10

The output will be:


3 à number of occurrences of 10.

Page 5 of 6
University of Modern Sciences & Arts (MSA) End-Term Examination-Fall 2004-2005
Fundmentals of computing for engeeris (CS-101) Time Allowed: 3 Hours

1 à first position of 10 in the array.

Page 6 of 6

You might also like