UNITED INTERNATIONAL UNIVERSITY (UIU)
Department of Computer Science and Engineering (CSE)
MID-TERM EXAMINATION SPRING, 2025
DURATION: 1 HOUR 30 MINUTES FULL MARKS: 30
CSE 1111: Structured Programming Language
Answer all 3 (three) questions. Figures in the right margin indicate full marks of questions.
[Any examinee found adopting unfair means will be expelled from the trimester / program according to
the UIU disciplinary rules.]
1. Check out the following programs:
a.c b.c
#include <stdio.h> switch(a * b)
{
int main() case 0:
{ a = b + 2;
int a = 5 != 6? -1.2 : 0.3; b = a--;
int b = 1 + a--;
case 1:
if(!b--) printf(”a = %d and b = %d\n”, a, b);
printf(”Inside First IF\n”); break;
printf(”a = %d and b = %d\n”, a, b); case 2:
a = b + 2;
if(a * b > 0 && b - a > 0) b = a--;
printf(”Inside Second IF\n”);
case 3:
else if(a + b < 0)
printf(”Inside ELSE IF\n”); case 4:
break;
else printf(”a = %d and b = %d\n”, a, b);
printf(”Inside ELSE\n”);
default:
return 0; printf(”Inside default\n”);
} }
a) Determine the output of the program a.c. [3]
Solution:
Inside First IF
a = -2 and b = -1
Inside Second IF
Rubric:
� 1 mark for the correct decision in the first block
� 1 mark for printing the correct values of a and b
CSE 1111 Page 1 of 8
� 1 mark for the correct decision in the second block depending on the printed values
of a and b
b) Rewrite the code segment b.c using if-else block without changing the logical meaning. [3]
Solution:
if(a * b == 0)
{
a = b + 2;
b = a--;
printf(”a = %d and b = %d\n”, a, b);
}
else if(a * b == 1)
{
printf(”a = %d and b = %d\n”, a, b);
}
else if(a * b == 2)
{
a = b + 2;
b = a--;
}
else if(a * b == 3) {}
else if(a * b == 4) {}
else
{
printf(”Inside default\n”);
}
Rubric:
� 0.5 mark for correct transformation of each case
CSE 1111 Page 2 of 8
2. a) Determine the output of the following program: [4]
#include <stdio.h>
int main()
{
int a = 8, b = 0, c = 0;
do
{
if(a % 2 == 0) b++;
else c++;
printf(”%d %d %d\n”, a, b, c);
if(a == 8) a = 3;
else if(a == 3) a = 6;
else if(a == 6) a = 9;
else if(a == 9) a = 0;
} while(a != 0);
return 0;
}
Solution:
8 1 0
3 1 1
6 2 1
9 2 2
Rubric:
� 1 mark for the correct output of each line
b) Determine the output of the following program: [3]
#include <stdio.h>
int main()
{
int n = 9;
int i = 1;
int j;
for(j = i * 7; i < n / 2 && i != j; j = j - 1 + i)
{
j = j - 2 + i;
printf(”i=%d, j=%d\n”, i, j);
do{
j += 3, n--;
} while(j % 2 != 0);
if(i == 1) continue;
i++;
}
CSE 1111 Page 3 of 8
return 0;
}
Solution:
i=1, j=6
i=1, j=11
i=1, j=13
i=1, j=15
i=1, j=17
Rubric:
� 0.6 mark for correct output of each line
c) Draw a flowchart for a rocket launch countdown system. The system should begin with a [3]
countdown timer set to 10 seconds. It should then:
� Count down from 10 to 1, printing the countdown number each second.
� Once the countdown reaches 0, display an alert message “Blasting Off!”
Solution:
Rubric:
� 2 marks for correct algorithm
� 1 mark for correct symbols
CSE 1111 Page 4 of 8
d) Write a program that takes an odd integer n as input and displays a triangular pattern as demon- [5]
strated in the following table:
Sample Input Sample Output
0
3 101
21012
0
101
5 21012
3210123
432101234
Solution:
#include <stdio.h>
int main()
{
int i, j, n;
scanf(”%d”, &n);
for(i = 0; i < n; i++)
{
for(j = 0; j < n - 1 - i; j++) printf(” ”);
for(j = i; j >= 1; j--) printf(”%d”, j);
printf(”0”);
for(j = 1; j <= i; j++) printf(”%d”, j);
printf(”\n”);
}
return 0;
}
CSE 1111 Page 5 of 8
Alternate Solution:
#include <stdio.h>
int main()
{
int i, j, n;
scanf(”%d”, &n);
for(i = 0; i < n; i++)
{
for(j = 0; j < n - 1 - i; j++) printf(” ”);
for(j = 0; j < 2 * i + 1; j++)
{
int diff = i > j? i - j: j - i;
printf(”%d”, diff);
}
printf(”\n”);
}
return 0;
}
Alternate Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, n, m;
scanf(”%d”, &n);
m = n - 1;
for(x = 0; x < n; x++)
{
for(y = -m; y <= m; y++)
{
if(abs(y) > x) printf(” ”);
else printf(”%d”, abs(y));
}
printf(”\n”);
}
return 0;
}
Rubric:
� 1 mark for correct syntax
� 1.5 mark for correct shape
CSE 1111 Page 6 of 8
� 1.5 mark for correct numbers
� 1 mark for not printing anything wrong or extraneous (only applicable if everything
is printed correctly)
3. a) Manually trace the following code segment for the array m[4]. Show the changes of all the [4]
variables.
#include <stdio.h>
int main()
{
int i, j, m[4];
for(i = 0, j = 1; i < 4; i = i + 1, j = j + 2)
{
m[i] = i * i + j;
printf(”i = %d, j = %d, ”, i, j);
printf(”m[%d] = %d\n”, i, m[i]);
}
return 0;
}
Solution:
i = 0, j = 1, m[0] = 1
i = 1, j = 3, m[1] = 4
i = 2, j = 5, m[2] = 9
i = 3, j = 7, m[3] = 16
Rubric:
� 1 mark for the correct tracing of each iteration
CSE 1111 Page 7 of 8
b) Write a C program that reads 20 integers into an array, then determines how many of those [5]
numbers are even, and how many are odd. The program should also calculate and display the
average of all 20 numbers.
Solution:
#include <stdio.h>
int main()
{
int a[20];
int even = 0, odd = 0, sum = 0;
for(int i = 0; i < 20; i++) scanf(”%d”, &a[i]);
for(int i = 0; i < 20; i++)
{
sum += a[i];
if(a[i] % 2 == 0) even++;
else odd++;
}
float avg = sum / 20.0;
printf(”Even numbers: %d\n”, even);
printf(”Odd numbers: %d\n”, odd);
printf(”Average: %.2f\n”, avg);
return 0;
}
Rubric:
� 1 mark for correct syntax
� 2 mark for correct algorithm
� 2 mark for correct output that includes 0.5 mark for each of the counts and 1 mark
for the average
CSE 1111 Page 8 of 8