Structured Programming Language - Mid - Solution
Structured Programming Language - Mid - Solution
SRUCTURED
PROGRAMMING LANGUAGE
CSE 1111
SOLUTION BY
NURUL ALAM ADOR
nurulalamador.github.io/UIUQuestionBank
Index
Trimester Page
Spring 2024 3
Fall 2023 10
Summer 2023 18
Spring 2023 27
Fall 2022 38
Summer 2022 47
nurulalamador.github.io/UIUQuestionBank 2
Spring 2024
include <stdio>
Int main {
int Num, a;
Num = 20%3;
a = Num+10
printf("%d %f ", Num, a,);
return 0;
}
Solution:
The code has been rewritten below with the error correction:
#include <stdio.h>
int main() {
int Num, a;
Num = 20%3;
a = Num+10;
printf("%d %d ", Num, a);
return 0;
}
Solution:
Output:
result = 6
a = 3
2. a) Rewrite the code segment using “if … else” without changing the logical meaning.
nurulalamador.github.io/UIUQuestionBank 3
switch(num) {
case 1: sum *= 3;
case 2:
case 3: sum += --j * 2;
i--; break;
case 4: sum = ++i * j--;
break;
case 5: break;
i += 10;
default: sum *= i++ / j--;
i=i % j; break;
}
Solution:
The code has been rewritten below using “if … else” :
if (num == 1) {
sum *= 3;
sum += --j * 2;
i--;
}
else if (num == 2 || num == 3) {
sum += --j * 2;
i--;
}
else if (num == 4) {
sum = ++i * j--;
}
else if (num == 5) {
}
else {
sum *= i++ / j--;
i=i % j;
}
2. b) Manually trace the following code segment and show all the changes of the
variables i,p, and x in each step.
#include <stdio.h>
int main() {
int p=1;
int x = 490;
for(int i=1;i<=p;){
printf("%d %d %d\n",i,p,x);
if (x % 29 == 0){
printf("Not a great number!");
break;
}
else {
x -= 13;
p += x % 10;
i += 3;
nurulalamador.github.io/UIUQuestionBank 4
}
}
return 0;
}
Solution:
All the changes of the variables i, p, and x in each step has been shown below:
i p x i<=p x % 29 x -= 13 p += x % 10 i += 3
7 12 464 7<=12 0 == 0
(True) (True)
Solution:
The flow chart has been drawn below:
Start
p=1
x = 400
x -= 13
p += x % 10
i=1 i += 3
false
true
i <= p Print i p x x % 29 == 0
true
false
End
3. a) Write a C program to print the following pattern of digit ‘2’. Take n as user input
where n is odd and n>=5.
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 5
Sample input n=5
*****
*
Sample output *****
* *
*****
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(i == 0 || i == n-1 || i == n/2) {
printf("*");
}
else if(i < n/2) {
if(j == n-1) {
printf("*");
}
else {
printf(" ");
}
}
else {
if(j == 0) {
printf("*");
}
else {
printf(" ");
}
}
}
printf("\n");
}
return 0;
}
3. b) Replace the “outer” while loop with “for” and the “nested” for loop with “while”
loop in the following code without changing the logical meaning of the program.
nurulalamador.github.io/UIUQuestionBank 6
Solution:
The code has been rewritten below as per the question:
4. Manually trace the given code segment below. Show the changes of all the variables i,
hi, hlw and array arr elements in each step.
Solution:
All the changes of the variables i, hi, hlw, and array arr elements in each step has been
shown below:
7<=4
0 4 {15, 20, 30, 40} 7
(False)
5. Take an array as input of size N. Then take another number as input in K. Your task is
to add this number to the even indexed elements, and subtract from the odd indexed
elements.
N=5 14 16 34 36 54
Array Elements: 10 20 30 40 50
K=4
Solution:
nurulalamador.github.io/UIUQuestionBank 7
The program has been written below:
#include <stdio.h>
int main() {
int N, K;
printf("N=");
scanf("%d", &N);
int arr[N];
printf("Array Elements: ");
for(int i=0; i<N; i++) {
scanf("%d", &arr[i]);
}
printf("K=");
scanf("%d", &K);
return 0;
}
5. Write a program which will take input of N x N numbers in a 2D array A. Now swap all
the elements in the first and last column within the array and finally print the array.
3 741
147 382
283 065
560
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int A[n][n];
for(int i=0; i<n; i++) {
nurulalamador.github.io/UIUQuestionBank 8
for(int j=0; j<n; j++) {
scanf("%d", &A[i][j]);
}
}
return 0;
}
nurulalamador.github.io/UIUQuestionBank 9
Fall 2023
1. b) Compute the values of the variables a, b, c, and d. ASCII codes: A-65, a-97, 0-4.
(i) float a = 101/7;
(ii) float b = (float)(3%5);
(iii) float c = 23>43 || 6!=6;
(iv) double result = 12 + (1 * '3');
Solution:
The values of the given variables have been written below:
(i) a = 14.000000
(ii) b = 3.000000
(iii) c = 0.000000
(iv) result = 63.000000
1. c) Find outputs of the following code segment for (i) num = 2.3, and (ii) num = 127.
int num;
scanf("%d", &num);
if (num % 2 != 0) {
printf("Mashrafe\n");
}
if (num < 100) {
printf("Shakib\n");
}else if (num >= 100){
printf("Mahmudullah\n");
}
if (num >= 0 && num < 5){
printf("Imrul\n");
}else if (num >= 0 && num <= 49){
printf("Tamim\n");
}else{
printf("Rubel");
}
Solution:
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 10
(i) Output for num = 2.3 :
Shakib
Imrul
Mashrafe
Mahmudullah
Rubel
2. a) Rewrite the code segment using “if … else” without changing the logical meaning.
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int result=a--/b++;
switch(a+b){
case 1:
result+=a/c*2;
b++;
case 2:
case 3:
result=a*c/b;
a++;
case 4: break;
a=2;
default: result=5;
}
printf("%d %d %d %d", a,b,c,result);
Solution:
The code has been rewritten below using “if … else” :
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int result=a--/b++;
if (a+b == 1) {
result+=a/c*2;
b++;
result=a*c/b;
a++;
}
else if (a+b == 2 || a+b == 3) {
result=a*c/b;
a++;
}
else if (a+b == 4) {
}
else {
result=5;
}
printf("%d %d %d %d", a,b,c,result);
nurulalamador.github.io/UIUQuestionBank 11
2. b) Manually trace the following code segment and show all the changes of the
variables start, end, i, count in each step.
int start=105,end=112,count=0;
for(int i=end; i>=start; i--){
if(end%2 != 0){
count++;
start++; end+2;
}else{
end--; start+1;
}
}
Solution:
All the changes of the variables start, end, i and count in each step has been
shown below:
3. a) Write a C program to display the following ‘M’ pattern for n. For example, for n = 3,
& n = 5 the output pattern will be as follows. You must program for n, not for 3 or 5.
Sample output * * * *
* * * * * * *
* * * * *
* *
* *
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for(int i=0; i<n; i++) {
nurulalamador.github.io/UIUQuestionBank 12
for(int j=0; j<n; j++) {
if(j == 0 || j == n-1) {
printf("* ");
}
else if(i <= n/2) {
if(j == i || j == n-1-i) {
printf("* ");
}
else {
printf(" ");
}
}
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
3. b) Replace the “outer” for loop using “while” loop and the “inner” for loop using “do
while” loop in the following code without changing the logical meaning of the
program.
Solution:
The code has been rewritten below as per the question:
if (b%2!=0) {
printf("%d \n", a);
} else {
printf("%d \n", b);
}
nurulalamador.github.io/UIUQuestionBank 13
i--;
}
int n;
printf("Enter a +ve integer:");
scanf("%d", &n);
if (n <= 0) {
printf("Enter a +ve integer.");
}else {
int fact = 1, i = 1;
do {
fact *= i;
i++;
} while (i <= n);
printf("Factorial of %d is %d", n, fact);
}
Solution:
The flow chart has been drawn below:
Start
Declaring n
Scan n
true
true
Print "Enter a +ve integer." i <= n
false
End
Print "Factorial of" n "is" fact
nurulalamador.github.io/UIUQuestionBank 14
4. b) Write a C program to take input of all the bank account balance of n clients of a
bank. Remove any balance less than 500.00 taka. Now, display all the balances.
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
float balances[n];
for(int i=0; i<n; i++) {
scanf("%f", &balances[i]);
}
return 0;
}
5. a) Manually trace the given code segment. Show the changes of all the variables i,
and array ara elements in each step.
Solution:
All the changes of the variables i and elements of array ara in each step has been
shown below:
nurulalamador.github.io/UIUQuestionBank 15
{ 8, 24, 2, 3<5 arr[3] = 3*arr[2]
3 5
4, 7 } (True) = 3*2 = 6
{ 8, 24, 2, 5<5
5
6, 7 } (False)
1<5 1 == 0
" 1 2
(True) (False)
5. b) Manually trace the given code segment and show the changes of all the variables
row, col, and sum in each step.
Solution:
All the changes of the variables row, col and sum in each step has been shown
below:
1 2 3
11 5 6
Here, A =
12 7 9
8 13 4
sum row row<4 col col<3 col>row sum += A[row][col] col++ row++
3<3
5 " 3 1
(False)
nurulalamador.github.io/UIUQuestionBank 16
1<4 0<3 0>1
" 1 0 1
(True) (True) (False)
1<3 1>1
" " 1 2
(True) (False)
3<3
11 " 3 2
(False)
0<3 0>2
11 2 0 1
(True) (False)
1<3 1>2
" " 1 2
(True) (False)
2<3 2>2
" " 2 3
(True) (False)
3<3
" " 3 3
(False)
1<3 1>3
" " 1 2
(True) (False)
2<3 2>3
" " 2 3
(True) (False)
3<3
" " 3 4
(False)
4<4
11 4
(False)
nurulalamador.github.io/UIUQuestionBank 17
Summer 2023
1. c) Find outputs of the following code segment for (i) a = 0, b = 0, and (ii) a = -1, b = -7:
#include<stdio.h>
void main(){
int a, b;
scanf("%d%d", &a, &b);
if(!(a-b) && ++a)
printf("Pattern\n");
if((a>0&&b>0)||(a<0&&b<0)){
printf("Fizz\n");
if(a>0)
printf("Positive\n");
return 0;
if(b<0)
printf("Negative\n");
}
else if(a>0 && b<0)
printf("Buzz\n");
nurulalamador.github.io/UIUQuestionBank 18
else printf("FizzBuzz\n");
}
Solution:
(i) Output for a = 0, b = 0 :
Pattern
FizzBuzz
Fizz
2. a) Rewrite the code segment using “if … else” without changing the logical meaning.
char rank;
scanf(“%c”, &rank);
int bonus = 0;
switch(rank) {
case ‘p’:bonus += 20;
case ‘g’:bonus += 20;
case ‘s’:bonus += 20;
break;
default: bonus += 10;
}
printf(“\n%d”, bonus);
Solution:
The code has been rewritten below using “if … else” :
char rank;
scanf(“%c”, &rank);
int bonus = 0;
if(rank == 'p') {
bonus += 20;
bonus += 20;
bonus += 20;
}
else if(rank == 'g') {
bonus += 20;
bonus += 20;
}
else if(rank == 's') {
bonus += 20;
}
else if(rank == 's') {
bonus += 10;
}
printf(“\n%d”, bonus);
2. b) Manually trace the following code segment and show the changes of the variables
nurulalamador.github.io/UIUQuestionBank 19
i, j, n in each step.
#include <stdio.h>
void main() {
int i = 2, n = 10, j=0;
for(j = n; j > i; j--) {
if(j % 2 == 0) i++;
else n--;
}
i += 2;
}
Solution:
All the changes of the variables i, j and n in each step has been shown below:
2 10 0
10 > 1 0 == 0
" " 10 3 9
(True) (True)
9>3 1 == 0
3 " 9 9 8
(True) (False)
8>3 0 == 0
" 9 8 4 7
(True) (True)
7>4 1 == 0
4 " 7 8 6
(True) (False)
6>4 0 == 0
" 8 6 5 5
(True) (True)
5>5
5 " 5 5+2 = 7
(False)
7 8 5
3. a) Write a C program that takes an integer n as input from the user and prints a
specific pattern given as follows. For example, for n = 4, the output pattern will be
as follows. You must program for n, NOT for 4.
*****
*****
*****
*****
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for(int i=0; i<n; i++) {
nurulalamador.github.io/UIUQuestionBank 20
for(int j=0; j<n-i-1; j++) {
printf(" ");
}
printf("*****\n");
}
return 0;
}
3. b) Replace all the “for” loops in the following code using only “while” loops without
changing the logical meaning of the program.
Solution:
The code has been rewritten below as per the question:
int i = 6;
while(i<10){
int j = 9;
int arr[10]= {0};
while(j>=i){
int k = 15,
arr[j] = ++c;
int i = 1;
j--;
while(i<6){
}
arr[i] = ++k-2;
i++;
k++;
}
i+=2;
i = 0;
}
while(i<10){
int c = 0;
if(i%2==0) arr[i] = ++k;
i++;
}
4. a) Write a C program that takes n number of integers as input into an array of size
N, where n is an odd number and n<=N. Your task is to reverse the first half array
elements & the last half array elements, keeping only the middle element intact.
1234567 3214765
10 20 30 40 50 20 10 30 50 40
987 987
Solution:
The program has been written below:
nurulalamador.github.io/UIUQuestionBank 21
#include <stdio.h>
int main() {
int arr[9999]; //Let N=9999
int n;
scanf("%d", &n);
for (int i=0; i<n; i++) {
scanf("%d", &arr[i]);
}
start = 0;
end = (n/2)-1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
start = (n/2)+1;
end = n-1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return 0;
}
4. b) Draw a flow chart to take an integer as input. Then, display its odd factors and
calculate the sum of its even factors. Hint: any integer number is a multiple of any
of its factors.
1 5 [Odd factors]
20
36 [Sum of even factors: 2+4+10+20=36]
1 7 [Odd factors]
28
48 [Sum of even factors: 2+4+14+28=48]
Solution:
The flow chart has been drawn below:
nurulalamador.github.io/UIUQuestionBank 22
Start
Declaring n
Scan n
sum = 0
i=1
true true
i <= n n%i == 0 sum += i
false false
End
5. a) Manually trace the given code segment. Show the changes of all the variables i, j,
jump and array array A and B elements in each step.
Solution:
All the changes of the variables i, j, jump and elements of array A, B in each step has
been shown below:
nurulalamador.github.io/UIUQuestionBank 23
3 10 jump B[0] jump
0<3 0<3
2 20 100 0 = A[0]*2 0 = A[0]+B[0] = B[0]/2
(True) (True)
1 30 = 3*2 = 6 = 3+10 = 13 = 13/2 = 6
3 19 "
3<3 A[0]
2 20 9 3
(False) =4
1 30
4 22 "
3<3 A[1]
2 26 13 3
(False) =3
1 30
4 22 "
3<3 A[1]
3 26 16 3
(False) =2
1 33
4 22
3<3
3 26 16 3
(False)
2 33
5. b) Manually trace the given code segment and show the changes of all the variables
i, j, x and sum in each step.
nurulalamador.github.io/UIUQuestionBank 24
int n=4, sum=0, x = 0;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(j==n-1 || i+j==n-1){
x = arr[i][j];
sum+=x;
}
}
}
Solution:
All the changes of the variables i, j, x and sum in each step has been shown below:
Here, n = 4
5 7 3 13
31 2 11 23
arr =
17 19 43 53
37 47 29 61
0<4 0<4 0 == 3 || 0 == 3
0 0 0 0 1
(True) (True) (False)
1<4 1 == 3 || 1 == 3
" " " 1 2
(True) (False)
2<4 2 == 3 || 2 == 3
" " " 2 3
(True) (False)
x= sum
3<4 3 == 3 || 3 == 3
" " " 3 arr[0][3] = 0+13 4
(True) (True)
= 13 = 13
4<4
13 13 " 4 1
(False)
1<4 0<4 0 == 3 || 1 == 3
" " 1 0 1
(True) (True) (False)
1<4 1 == 3 || 2 == 3
" " " 1 2
(True) (False)
x= sum
2<4 2 == 3 || 3 == 3
" " " 2 arr[1][2] = 13+11 3
(True) (True)
= 11 = 24
x= sum
3<4 3 == 3 || 4 == 3
24 11 " 3 arr[1][3] = 24+23 4
(True) (True)
= 23 = 47
4<4
47 23 " 4 2
(False)
2<4 0<4 0 == 3 || 2 == 3
" " 2 0 1
(True) (True) (False)
nurulalamador.github.io/UIUQuestionBank 25
x= sum
1<4 1 == 3 || 3 == 3
" " " 1 arr[2][1] = 47+19 2
(True) (True)
= 19 = 66
2<4 2 == 3 || 4 == 3
66 19 " 2 3
(True) (False)
x= sum
3<4 3 == 3 || 5 == 3
" " " 3 arr[2][3] = 66+53 4
(True) (True)
= 53 = 119
4<4
119 53 " 4 3
(False)
x= sum
3<4 0<4 0 == 3 || 3 == 3
" " 3 0 arr[3][0] = 119+37 1
(True) (True) (True)
= 37 = 156
1<4 1 == 3 || 4 == 3
156 37 " 1 2
(True) (False)
2<4 2 == 3 || 5 == 3
" " " 2 3
(True) (False)
x= sum
3<4 3 == 3 || 6 == 3
" " " 3 arr[3][3] = 156+61 4
(True) (True)
= 61 = 217
4<4
217 61 " 4 4
(False)
4<4
" " 4 "
(False)
nurulalamador.github.io/UIUQuestionBank 26
Spring 2023
1. c) Find the outputs of the following program for (i) b=10, and (ii) b=2:
#include <stdio.h>
int main() {
int b;
scanf("%d", &b);
if(b >= 10) {
printf("SPL\n");
b--;
}
if(b < 10) {
printf("Spring\n");
b--;
}
else if((b>=3) || (b<10))
printf("2023\n");
else if(b>=3 && b<10)
printf("Happy Coding!");
else
printf("Huh!");
return 0;
}
nurulalamador.github.io/UIUQuestionBank 27
Solution:
(i) Output for b = 10 :
SPL
Spring
Spring
2. a) Rewrite the following code segment using “switch … case” without changing the
logical meaning.
int n, a;
scanf("%d %d", &n, &a);
if(n>a) {
if(n-a>5) {
printf("Difference is greater than 5 \n");
}
else {
printf("Difference is less than or equal to 5 \n");
}
}
else {
printf("Please give a larger value of n \n");
}
Solution:
The code has been rewritten below using “switch … case” :
int n, a;
scanf("%d %d", &n, &a);
switch (n>a) {
case 1:
switch (n-a>5) {
case 1:
printf("Difference is greater than 5 \n");
break;
default:
printf("Difference is less than or equal to 5 \n");
break;
}
break;
default:
printf("Please give a larger value of n \n");
break;
}
2. b) Manually trace the following code segment and show the changes of the values of
variables i, j, result, x, y in each step.
nurulalamador.github.io/UIUQuestionBank 28
int result = 5, i, x = 2, y = 2;
for(int j = 8; j > 3; --j) {
i = (j * result) / x;
result += y;
x += (y-2);
y++;
}
Solution:
All the changes of the variables i, j, result, x, y in each step has been shown below:
3>3
25 9 12 7 3
(False)
3. a) Replace the nested “for” loops in the following code using only nested “do …
while” loops without changing the logical meaning of the program:
int main() {
int weeks = 2, days_in_week = 7;
for (int i = 1; i <= weeks; ++i) {
printf("Week: %d\n", i);
for (int j = 1; j <= days_in_week; ++j) {
if (i%2 == 0) {
if(j%2 == 0)
printf(" Day: %d\n", j);
}
else{
if(j%2 != 0)
printf(" Day: %d\n", j);
}
}
}
return 0;
}
Solution:
The code has been rewritten below as per the question:
nurulalamador.github.io/UIUQuestionBank 29
int main() {
int weeks = 2, days_in_week = 7;
int i = 1;
do {
printf("Week: %d\n", i);
int j = 1;
do {
if (i%2 == 0) {
if (j%2 == 0)
printf(" Day: %d\n", j);
}
else {
if (j%2 != 0)
printf(" Day: %d\n", j);
}
j++;
} while (j <= days_in_week);
i++;
} while (i <= weeks);
return 0;
}
3. b) Write a C program that takes an integer n as input from the user and prints the
following pattern using nested loop.
Solution:
The program has been written below:
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i=1; i<=n; i++) {
int count = 2*i;
for (int j=0; j<n-i; j++) {
printf(" ");
}
for (int j=0; j<i; j++) {
printf("%2d ", count);
count += 2;
}
nurulalamador.github.io/UIUQuestionBank 30
count -= 2;
for (int j=0; j<i-1; j++) {
count -= 2;
printf("%2d ", count);
}
printf("\n", i);
}
return 0;
}
#include <stdio.h>
int main() {
float cgpa[100];
int count = 0;
float sum = 0;
float highestCount = 0;
int highest = 0;
int lowest = 4;
nurulalamador.github.io/UIUQuestionBank 31
float average = sum/count;
if (count != 0) {
printf("Average CGPA of student who achieved more than 3.00:
%f\n", average);
} else {
printf("No students achieved CGPA more than 3.00\n");
}
return 0;
}
Start
i=0
sum = 0
odd = 1
true true
i < 1000000 i%2 == 0 sum += i/odd
false false
i++
Print sum sum -= i/odd
odd += 2
End
5. a) Manually trace the given code segment for the following array “arr”. Show the
changes of all the variables in each step.
nurulalamador.github.io/UIUQuestionBank 32
#include<stdio.h>
int main() {
int arr[10]= {0};
int k = 15;
for(int i=1; i<6; i+=2)
{
arr[i] = ++k-2;
k++;
}
int c = 0;
for(int i=6; i<10; i++)
{
for(int j=10; j>=i; j--)
{
arr[j] = ++c;
}
}
for(int i=0; i<10; i++)
{
if(i%2==0)
{
arr[i] = ++k;
}
}
}
Solution:
All the changes of the variables and elements of array arr in each step has been
shown below:
Manual tracing for 1st for loop:
nurulalamador.github.io/UIUQuestionBank 33
{ 0, 14, NIL, 16, NIL, 7 7>=6 arr[7] = ++3
3 6
18, NIL, NIL, 3, 2 } (True) =4
10<10
" " 10
(False)
nurulalamador.github.io/UIUQuestionBank 34
{ 22, 14, 23, 16, NIL, 23 3<10 1==0
3 4
18, 5, 9, 12, 14 } (True) (False)
" 10<10
" 10
(False)
5. b) Manually trace the following code snippet and find the final content of the 2D
array “arr” after the execution of the code.
Solution:
All the changes of the variables in each step has been shown below:
t3 = t1 + t2
x = t1
arr[j][i] = t3 t1 = y
t1 t2 t3 x y z i i<5 y = t2 j j<5
t1 = t2 t2 = z
z = t1+t2
t2 = t3
t3 = 0+1 = 1
x=0
0<5 0<5 arr[0][0] = 1
0 1 0 y=1 0
(True) (True) t1 = 1
z=1
t2 = 1
1 1 1 0 1 1 1 1<5 t3 = 1+1 = 2
nurulalamador.github.io/UIUQuestionBank 35
(True) arr[0][1] = 2
t1 = 1
t2 = 2
t3 = 1+2 = 3
2<5 arr[0][2] = 3
1 2 2 2
(True t1 = 2
t2 = 3
t3 = 5
3<5 arr[0][3] = 5
2 3 3 3
(True t1 = 3
t2 = 5
t3 = 8
4<5 arr[0][4] = 8
3 5 5 4
(True) t1 = 5
t2 = 8
5<5 t1 = 1
5 8 8 5
(False) t2 = 1
t3 = 2
x=1
1<5 0<5 arr[1][0] = 2
1 1 8 1 y=1 0
(True) (True) t1 = 1
z=2
t2 = 2
t3 = 3
1<5 arr[1][1] = 3
1 2 2 1 1 2 1
(True) t1 = 2
t2 = 3
t3 = 5
2<5 arr[1][2] = 5
2 3 3 2
(True t1 = 3
t2 = 5
t3 = 8
3<5 arr[1][3] = 8
3 5 5 3
(True t1 = 5
t2 = 8
t3 = 13
4<5 arr[1][4] = 13
5 8 8 4
(True) t1 = 8
t2 = 13
5<5 t1 = 1
8 13 13 5
(False) t2 = 2
t3 = 3
x=1
2<5 0<5 arr[2][0] = 3
1 2 13 2 y=2 0
(True) (True) t1 = 2
z=3
t2 = 3
t3 = 5
1<5 arr[2][1] = 5
2 3 3 1
(True) t1 = 3
t2 = 5
nurulalamador.github.io/UIUQuestionBank 36
t3 = 8
2<5 arr[2][2] = 8
3 5 5 2
(True t1 = 5
t2 = 8
t3 = 13
3<5 arr[2][3] = 13
5 8 8 3
(True t1 = 8
t2 = 13
t3 = 21
4<5 arr[2][4] = 21
8 13 13 4
(True) t1 = 13
t2 = 21
5<5 t1 = 2
13 21 21 5
(False) t2 = 3
By observing the manual tracing so far, we understand that each row of the array is
a Fibonacci series of five numbers and shifting one value to the left after each row.
Therefore, we can assume the final content of the 2D array without further manual
trace. The final content of 2D array arr is:
1 2 3 5 8
2 3 5 8 13
arr = 3 5 8 13 21
5 8 13 21 34
8 13 21 34 55
nurulalamador.github.io/UIUQuestionBank 37
Fall 2022
#includes <studio.h>
int Main() {
int a, b, float sum;
Scanf(“%i”, &a);
a , b=10;
a+b =sum;
Printf(“%d”, &sum);
}
Solution:
The code has been rewritten below with the error correction:
#include <stdio.h>
int main() {
int a, b;
float sum;
scanf("%i", &a);
a=b=10;
sum = a+b;
printf("%f", sum);
}
1. b) Identify the invalid variable names from the following. Mention the reasons that
make them invalid.
sum_of_digit , switch , calculate sum , ( )value , const , Sum,
calculate-sum , 1st_sum
Solution:
The following variable names are invalid from following list:
switch
Reason: Variable names cannot be any keyword
calculate sum
Reason: Variable names cannot have spaces
( )value
Reason: Variable names cannot have parentheses
const
Reason: Variable names cannot be any keyword
calculate-sum
Reason: Variable names cannot have hyphens
1st_sum
Reason: Variable names cannot start with number
nurulalamador.github.io/UIUQuestionBank 38
float b = (int)(17.0/5);
float c= (int)17/5.0;
int d = (a>b) && c;
Solution:
The values of the given variables have been written below:
a = 15
b = 3.000000
c = 3.400000
d = 1
2. a) Manually trace the following C code segment for num=3 and show the changes of
values of all the variables in each step.
#include <stdio.h>
int main() {
int num;
int sum = 10, i =7, j = 2;
scanf("%d", &num);
switch(num) {
case 1:
case 2:
sum += --j*2;
i--;
case 3:
sum = ++i*j--;
break;
case 4:
sum *= i++/j--;
i=i%j;
default: break;
}
printf("%d %d %d",sum,i,j);
return 0;
}
Solution:
Here,
num = 3
sum = 10
i = 7
j = 2
nurulalamador.github.io/UIUQuestionBank 39
Since there are break after this statement, operations of switch will stop here.
16 8 1
2. b) Re-write the given C code segment in Q.2(a) using the “if-else” statement without
changing the logical meaning and output.
Solution:
The code has been rewritten below using “if … else” :
#include <stdio.h>
int main() {
int num;
int sum = 10, i =7, j = 2;
scanf("%d", &num);
if(num == 1 || num == 2) {
sum += --j*2;
i--;
sum = ++i*j--;
}
else if(num == 3) {
sum = ++i*j--;
}
else if(num == 4) {
sum *= i++/j--;
i=i%j;
}
printf("%d %d %d",sum,i,j);
return 0;
}
3. a) Write a complete program to print the following series up to ‘Nth’ term and also
find the sum of the series.
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a=0;
int b=0;
int sum=0;
for(int i=0; i<n; i++){
nurulalamador.github.io/UIUQuestionBank 40
int term = a*a+b;
printf("%d, ", term);
sum += term;
a += 2;
b++;
}
printf("\nSum - %d", sum);
return 0;
}
3. b) Manually trace the following code for ‘rows = 3’. Show changes of all the variables
in each step.
Solution:
All the changes of the variables in each step for rows = 3 has been shown below:
count count1 i i <= rows space space <= rows-i k k != 2*i-1 count <= rows-1
1 <= 2 (True)
1 <= 3
0 0 1 1 ++count 0
(True)
++space
2 <= 2 (True)
1 " " 2 ++count "
++space
2 <= 2
0 != 1 (True)
" " " "
(True) ++count
++k
nurulalamador.github.io/UIUQuestionBank 41
1 != 1
3 " " 1
(False)
0 0 " 0
1 <= 1 (True)
2 <= 3
" " 2 1 ++count "
(True)
++space
1 <= 2
0 != 3 (True)
" " " "
(True) ++count
++k
2 <= 2
1 != 3 (True)
2 " " 1
(True) ++count
++k
3 <= 2
2 != 3 (False)
3 " " 2
(True) ++count1
++k
3 != 3
" 1 " 3
(False)
0 0 " 0
3 <= 3 1 <= 0
" " 3 1 "
(True) (False)
0 <= 2
0 != 5 (True)
" " "
(True) ++count
++k
1 <= 2
1 != 5 (True)
1 " 1
(True) ++count
++k
2 <= 2
2 != 5 (True)
2 " 2
(True) ++count
++kk
3 <= 2
3 != 5 (False)
3 " 3
(True) ++count1
++k
3 <= 2
4 != 5 (False)
" 1 " 4
(True) ++count1
++k
nurulalamador.github.io/UIUQuestionBank 42
5 != 5
" 2 " 5
(False)
0 0 " 0
4 <= 3
" " 4 "
(False)
4. a) Manually trace the given code segment for the following array assuming size=5.
Show changes of all the variables in each step.
Solution:
All the changes of the variables in each step has been shown below:
arr size i i<size j j<size arr[i] == arr[j] k k<size-1 size-- j-- j++
2<4
2<5 10 == 10
" " " 2 2 (True)
(True) (True)
arr[2] = 10
3<4
{10, 20, 10,
" " " 3 (True)
10, 100}
arr[3] = 100
2<3
2<4 10 == 10
" 4 " 2 2 (True)
(True) (True)
arr[2] = 100
2<3 10 == 100
" 3 " 2
(True) (False)
3<3
" " " 3
(False)
nurulalamador.github.io/UIUQuestionBank 43
3<3
" " 3
(False)
2<3 3<3
" " 2 3
(True) (False)
3<3
" " 3 "
(False)
4. b) Write a program that reads a number n. Now, take n inputs in an array named
marks[10], here n is less than 10. Now, find the maximum number and its position
within the even values of the array.
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int marks[10];
int n;
scanf("%d", &n);
if (index != -1) {
printf("Even value max = %d, found in index %d", max,
index);
} else {
printf("No even number found!");
}
return 0;
}
nurulalamador.github.io/UIUQuestionBank 44
#include <stdio.h>
int main(void) {
int row = 10;
while (row >= 1) {
int column = 1;
while (column <= 10) {
printf("%s", (row % 2) ? "<": ">");
++column;
}
--row;
puts("");
}
}
Solution:
The flow chart has been drawn below:
Start
row = 10
true
row >= 1 column = 1
print “ ”
false
false
End column <= 10 --row
true
true false
row % 2 == 1
++column
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 45
Sample Input: Sample Output:
3 * *
*
*
5 * *
* *
*
*
*
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
nurulalamador.github.io/UIUQuestionBank 46
Summer 2022
include <stdio.h>
void main() {
int num1 = 5, float num2, char chr = 'q';
scanf(“%d”, num2);
num1 = num2 % chr;
printf("Result is = %f ", num1);
}
Solution:
The code has been rewritten below with the error correction:
#include <stdio.h>
void main() {
int num1 = 5;
float num2;
char chr = 'q';
scanf("%f", &num2);
num1 = (int)num2 % (int)chr;
printf("Result is = %d ", num1);
}
1. b) Identify the invalid variable names from the following. Mention the reasons that
make them invalid.
largest_val, smallest-val, while, 2ndNum, !New, avg marks, val9
Solution:
The following variable names are invalid from following list:
smallest-val
Reason: Variable names cannot have hyphens
while
Reason: Variable names cannot be any keyword
2ndNum
Reason: Variable names cannot start with number
!New
Reason: Variable names cannot have exclamatory sign
avg marks
Reason: Variable names cannot have spaces
nurulalamador.github.io/UIUQuestionBank 47
a = 10.000000
b = 10
c = 12.500000
d = 12
2. a) Write down the output of the following C program, for num = 1 and num = 3.
#include <stdio.h>
int main() {
int num;
int sum = 0, i = 10, j = 5;
scanf("%d", &num);
switch(num) {
case 1:
sum = 2*i++;
j++;
case 2:
sum = 2*j--;
i++;
break;
case 3:
sum = ++i*j--;
case 4:
sum = i++*j--;
default:
sum=0;
i=0;
j=0;
}
printf("%d %d %d", i, j, sum);
return 0;
}
Solution:
Output for num = 1 : Output for num = 3 :
12 5 12 0 0 0
2. b) Manually trace the following code segment and show the changes of values of
the variables i, sum, b, a, y, x in each step.
int sum=0, i, a = 1, b, x = 1, y = 1;
for(i=1; i<=5; i++) {
sum = sum + a;
b = 6*x + 1;
a = a + b;
y++;
x = x + y;
}
Solution:
All the changes of the variables i, sum, b, a, y, x in each step has been shown below:
nurulalamador.github.io/UIUQuestionBank 48
i sum b a y x i<=5 sum = sum+a b = 6*x+1 a = a+b y++ x = x+y
6<=5
6 225 91 216 6 21
(False)
3. a) Replace the nested “for” loop in the following code using nested “do-while” loop
without changing the logical meaning of the program:
void main() {
int n = 3, i, j, sum = 0;
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) {
if(i == j) sum += i + j;
else if(i > j) sum += i + n;
else sum += n – j;
}
}
}
Solution:
The code has been rewritten below as per the question:
int main() {
int n = 3, i, j, sum = 0;
i = 0;
do {
j = 0;
do {
if(i == j) sum += i + j;
else if(i > j) sum += i + n;
else sum += n - j;
j++;
} while (j <= i);
i++;
} while (i < n);
}
3. b) Write a program to find the online average of the positive numbers given as
inputs by the user. To solve this problem, you should do the following:
i. Write an infinite loop that will terminate if the user gives 0 as input.
nurulalamador.github.io/UIUQuestionBank 49
ii. If the user gives a positive number as input, you should keep adding it.
iii. You should also keep track of how many positive numbers are given as
inputs.
iv. Finally, when the loop terminates, you should calculate the average by
dividing the sum of the positive numbers by the total positive numbers.
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int infinite = 1;
float input;
int positiveNumber = 0;
float sum = 0;
while(infinite == 1) {
scanf("%f", &input);
if(input > 0) {
positiveNumber++;
sum = sum+input;
}
else if(input == 0) {
infinite = 0;
}
}
4. a) Show the manually tracing (show the values of all the variables and array
elements
in each step) for following code segment.
Solution:
All the changes of the variables in each step has been shown below:
{ 0, Null, Null,
3 0 True F[0] = 3+0 = 3 False
Null, Null, Null }
nurulalamador.github.io/UIUQuestionBank 50
{ 3, Null, Null,
" 1 True F[1] = 3+1 = 4 True F[1] = 4*2 = 8
Null, Null, Null }
{ 3, 8, Null,
" 2 True F[2] = 3+2 = 5 False
Null, Null, Null }
{ 3, 8, 5,
" 3 True F[3] = 3+3 = 6 True F[3] = 6*2 = 12
Null, Null, Null }
{ 3, 8, 5,
" 4 True F[4] = 3+4 = 7 False
12, Null, Null }
{ 3, 8, 5,
" 5 True F[5] = 3+5 = 8 True F[5] = 8*2 = 16
12, 7, Null }
{ 3, 8, 5,
" 6 False
12, 7, 16 }
#include <stdio.h>
int main() {
int arr[100];
int n;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
int sum = 0;
int count = 0;
printf("Index Value\n");
printf("----- -----\n");
for(int i=0; i<n; i++){
printf("%5d %5d\n", i, arr[i]);
if(i%2 != 0) {
sum = sum+arr[i];
nurulalamador.github.io/UIUQuestionBank 51
count++;
}
}
int average = sum/count;
printf("Average: %d\n", average);
return 0;
}
5. a) Draw a flowchart to find the sum of the following series up to n terms, where n is
input integer number from keyboard.
1 − 2 + 3 − 4 + ⋯ 𝑢𝑝𝑡𝑜 𝑛 𝑡𝑒𝑟𝑚𝑠
Solution:
The flowchart has been drawn below:
Start
sum = 0
i=1
n
scan n
sum = sum - i
true
true false
i <= n i % 2 == 0 sum = sum + i
false
print sum
End
3. a) Write a program that takes an integer n as input from the user and prints the
following pattern. Program for n, NOT 3 or 5.
[ P.T.O ]
nurulalamador.github.io/UIUQuestionBank 52
Sample input, n Sample output
6 4 2
3 4 2
2
10 8 6 4 2
8 6 4 2
5 6 4 2
4 2
2
Solution:
The program has been written below:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
return 0;
}
nurulalamador.github.io/UIUQuestionBank 53