You are on page 1of 5

University of Tripoli

Computer Engineering Department


Finale Exam Introduction to Programming (EC251) Sprint-2022

Answers of Final Exams


Date: 14-8-2022 Q1 –12 Q2- 12 Q3 - 14 Q4 – 12 Q5 – 10 Total
Time: 135 Min 60

(Q1) (5 marks)
A. Fill in the blanks ( 4 marks)
- The main difference between structures and arrays is …Structures are heterogeneous data structure
(different data types) while arrays are homogenous data structure (all elements are same data type)
- Structures in C language can be used to defined……. users’ own data types …………,…………..
- Passing by reference is used when we want ……. update or return more than one data item…………
- The algebraic formula a – [ ( b / c ) x d ] can be presents in C language ……a – b / c * d …..…………

B. Consider the following void main( void ) {


incomplete C program. int a=5;
Fill in the blanks char s[]="EC251-Spring2022";
according to the char *p=(s + 6);
descriptions given in the char c=’X’;
comments in the program double f=15;
printf( "_% d_", a ); /* Print the value of a */
printf("_% s_", s ); /* Print the content of s */
printf("_% c_", p ); /* Print the value pointed by p */
printf("_% c_", c ); /* Print the value of c */
printf("_%f_", f ); /* Print the value of f */
} (3 marks)
C. Assume that Q is integer,
write the equivalent of the following conditional operator

Z = Q > 0 && Q < 9 ? 8 : 10 ;


a) Using IF statement (2 mark) b) Using Switch/Case Statement (3 mark)

----------------------------------------------------------------- switch(Q)
------------------------------------------------------------------
{
If ( Q > 0 &&| Q < 9 )
Z = 8; case 1: case 2: case 3:
else case 4: case 5: case 6:
Z = 9; case 7: case 8:
Z = 8;
------------------------------------------------------------------ break;
------------------------------------------------------------------- default:
-----------------------------------------------------------------
------------------------------------------------------------------
Z = 9;
}

1/5 Final Exam – EC251 Spring2022 Y.Gdura


Q2) (12 marks)

D. Fill in the two columns, assume that you have the following statements: ( 3 marks)
Equivalent
int X = 2, Y = 5 ; C Expression Algebraic Value
float F= 2.5, G = 3.5; Expression
X+=5 X=X+5 7
F-=G F=F–G -1.0000
Y*=(X–3) Y = Y*(X–3) -5 or 20
F/=3 F=F/3 0.83 or -0.5
X%=(Y–2) X = X%(Y-2) 0 or 2
E. Consider the following function named
void reverse(char A[20])
“reverse” that reverses a given string A,
which received as argument, into B and {
print the reversed version B. int i = ..0.. , j = ..0.., count = …0…;
char …B[20]…;
Fill in the blanks to complete the while ( A[count] != '\0' )
implementation of reverse …count++;
for (j = count. ; j >= 0 ; j - -)
B[ …i++…] = …A...[ j ];
printf("%..s..", B);
} (4 marks)

F. Print out the output of the following code segments (5 Marks)


.

float arr[5] = {1.5, 5.2, 4.3}; int m = 1, x[5]={1,5, 3, 4,5};


float *ptr1; x[ 1 ] = m++;
float *ptr2 = &arr[2]; x[ 3 ] = m+ 5;
*ptr1 = *ptr2 + 3; x[ x[1] ] = x[ 1 ] * 2;
printf("%f %f ",*ptr2,*ptr1+arr[0]); x[ 6 - x[1] ] = 9;
for (int i=0; i < 5 ; i++)
printf("%d",x[i]);

(2 marks) ( 3 marks)

Answer: …..4.300000….. ……8.300000…… …1… …2… …3… …7… …9….

2/5 Final Exam – EC251 Spring2022 Y.Gdura


Q3) ( 14 marks)

A) Write a C function that accepts a 1-dimensional floating-point array and its size as arguments and
prints the smallest values in that array. (5 marks)
……………………………………………………………………………………………………………………………
………………………void Smallest1DArray( float *Arr, int Size) …………………………………
…………………… { …………………………………………………………………………………………………
………………………… …………float smallest = Arr[0] ; ………………………………………………
………………………………………for ( int i = 1 ; i < Size ; i++) ……………………………………..……
……………………………………………if ( Arr[ i ] < smallest ) …………………………………………
…..………………………………………………smallest = Arr[ i ] ; ……………………………………………
………………………………………printf(“ % f “ , smallest ) ; ……………………………………………
………………………} ……………………………………………………………………………………………………
……………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………

B) Write a C program that has two functions: main function and the other function (called “Average” )
accepts a 2-dimensional floating-point array of size 2 x 3 as argument and returns the average of the
values in that array. The main function should declare a 2D array with the values {8,3,5,2,4,5}, and
then gets the average by calling the Average function and finally prints the average. (9 marks)

------------------Function Main --------------------------- ----------------------Function Average --------------------


------------------------------------------------ ( 3 marks ) --------------------------------------------------( 6 marks )
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------ float Average ( float Arr[2][3] )
…….void main() ------------------------------ {
----{ --------------------------------------------- float Sum = 0 ;
---- - float A[2][3]= { 8, 3, 5, 2, 4, 5 }; for ( int i = 0 ; i < 2 ; i++)
------- printf( “%f”, Average ( A ) ) ;-------------- for ( int j = 0 ; j < 3 ; j++)
---} ---------------------------------------------- Sum+=arr[i][j];
---------------------------------------------------- return Sum/6;-------------------------------------
-------------- }
---------------------------------------------------- -----------------------------------------------
-------------- ------------------------------------------------------------------
------------------------------------------------------------------- ------------------------------------------------------------------
---------------------------------------------------------------- -----------------------------------------------------------------------
------------------------------------------------------------------ ----------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------- ------------------------------------------------------------------
-------------------------------------------------------------- -----------------------------------------------------------------------
------------------------------------------------------------------ -------------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------ ------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------

3/5 Final Exam – EC251 Spring2022 Y.Gdura


Q4)
A) Write a C function named printRange that, when called, prints out all integers between two
given values M and N, assuming M <= N, and M should be the first argument to the function
and N the second argument to the function. For example, if the call printRange( 1, 100 ) is
executed, this call should print out the sequence 1 2 3 … 98 99 100)
(5 marks)
……………………………………………………………………………………………………………………………
void Q4A(int M, int N)
{
for (int i=M ; i <=N ; i++)
printf("%d",i);
}
OR
void Q4A(int M, int N) {
While ( M <=N ) {
printf("%d", M);
M++;
{
}

B) Convert the following flowchart to Pseudo Code

……………………………………………( 7 mark ).
………START………………………………………………
……… SET Engine  0 …………………………….
……… SET Count  0 …………………………….
……… SET Number  0 …………………………….
L1: … GET/READ/INPUT Size ………………
……… IF Size = -1 THEN …………………………
………………Average  Enigne / Number ……
………………PRINT Average , Count ……………
………………STOP …………………………………………
………ENDIF ……………………………………………
……… IF Size > 1.5 THEN …………………………
………………Count  Count + 1
………ENDIF ……………………………………………
………Number  Number + 1 ……………………
………Engine  Engine + Size …………………
………GOTO L1 …………………………………………
……………………………………………………………………
……………………………………………………………………
……………………………………………………………………
…………………………………………………………………
……………………………………………………………

4/5 Final Exam – EC251 Spring2022 Y.Gdura


Q5) (10 marks)

A) Define a structure called “Name” that has one data member; a string of size 20, and define another
structure called "Personal-Info" that has two members: National-ID as a string of size 12, and
array of above structure “Name” to presents the first name, second name, and family name.

Name Structure (2 marks) Personal-Info Structure ( 3 marks)


…………………………………………………………………………………… …………………………………………………………………………………
…………………………………………………………………………………… …………………………………………………………………………………
…………………struct Name ……………………… …………struct Personal-Info ………………
…………………{…………………………………………… ……… {……………………………………………
………………… char N[20] ; ……………… …………… char N-ID[12] ; ………………
…………………….…}…………………………………………… …………… struct PerName[3] ; ……
…………………………………………………………………………………… ……….…}……………………………………………………………………
…………………………………………………………………………………… …………………………………………………………………………………
…………………………………………………………………………………… …………………………………………………………………………………
…………………………………………………………………………………… …………………………………………………………………………………
……………………………… …………………………
…………………………………………………………………………………… …………………………………………………………………………………
…………………………………………………………………………………… …………………………………………………………………………………
…………………………………………………………………………………… …………………………………………………………………………………
…………………………………………………… …………………………………………………………………….
…………………………………………………………………………….
B) Write a C program that allows entering the Personal information of 12 persons. (5 marks)
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
……………………………

void main ()
{
struct Personal-Info Employee[12] ;
for ( int i =0 ; i < 12 ; i++)
{
scanf(“%s”, Employee[i].N-ID;
scanf(“%s”, Employee[i].PerName[0].N);
scanf(“%s”, Employee[i].PerName[1].N);
scanf(“%s”, Employee[i].PerName[2].N);
}
}
………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………
Good Luck

5/5 Final Exam – EC251 Spring2022 Y.Gdura

You might also like