0% found this document useful (0 votes)
59 views7 pages

CS2311 Midterm Solution

The document contains a midterm exam with 6 programming questions. Question 1 asks to write the console output of 5 code snippets with various language features like arithmetic, increment/decrement operators, functions, arrays. Question 2 provides a bubble sort algorithm with errors and asks to correct it. Question 3 asks to implement a recursive Fibonacci function. Question 4 asks to write a function to remove redundant numbers in an integer array.

Uploaded by

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

CS2311 Midterm Solution

The document contains a midterm exam with 6 programming questions. Question 1 asks to write the console output of 5 code snippets with various language features like arithmetic, increment/decrement operators, functions, arrays. Question 2 provides a bubble sort algorithm with errors and asks to correct it. Question 3 asks to implement a recursive Fibonacci function. Question 4 asks to write a function to remove redundant numbers in an integer array.

Uploaded by

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

CS2311 2022-23 Sem A

Midterm

Question 1 [36 marks]


Write the console output of the following programs.
A.
int a = 10;
double b = 10;
cout << 25/a << ' ' << 25/b;

ANS:
2 2.5

B.
char x = 'a';
cout << x++ << ' ' << ++x;

ANS:
ac

C.
int func(int a, int b) {
cout << a + b << ' ';
return a - b;
}

int main() {
int a = 2, b = 2;
if (func(a, b) && func(a+2, b))
cout << 'A';
else
cout << 'B';
return 0;
}

ANS:
4B

1
D.
int func(char c) {
int i;
switch (c) {
case 'x': i = 1; break;
case 'y': i = 2; break;
case 'z': i = 3; break;
default: i = 4;
}
return i;
}

int main() {
cout << func('y') + func('a');
return 0;
}

ANS:
6

E.
int a[6] = {2, 5, 6, 7, 9, 8};
for (int i = 0; i < 6; i++) {
if (a[i]%2 == 0)
continue;
else if (a[i]%3 == 0)
break;
else
cout << a[i] << ' ';
}

ANS:
57

F.
int a[2][4] = {{1,2,3}, {4}};
cout << a[0][1] << ' ' << a[0][3] << ' ' << a[1][2];

ANS:
200

2
Question 2 [24 marks]
The following program tries to sort an integer array into ascending order using bubble sort. The
array size is defined as N = 6. This program has both syntax errors and bugs. Please correct them.
Marks will be deducted for incorrect answers.

Ln Code with Syntax Errors and Bugs Corrected Code (ANS)


1 #include <iostream> #include <iostream>
2 using namespace std; using namespace std;
3 #define N 6 #define N 6
4 int main() { int main() {
5 int a[N] = {6,3,2,5,4,1}; int a[N] = {6,3,2,5,4,1};
6 for (i = 0; i < N-1; i++) { for (int i = 0; i < N-1; i++) {
7 bool sorted = false; bool sorted = true;
8 for (j = N-1; j > i; j++) { for (int j = N-1; j > i; j++) {
9 if (a[j-1] > a[j]) { if (a[j-1] > a[j]) {
10 int tmp = a[j]; int tmp = a[j];
11 a[j-1] = a[j]; a[j] = a[j-1];
12 a[j] = tmp; a[j-1] = tmp;
13 sorted = true; sorted = false;
14 } }
15 } }
16 if (sorted = true) if (sorted == true) // if (sorted) is also correct
17 break; break;
18 } }
19 return 0; return 0;
20 } }

3
Question 3 [20 marks]
The Fibonacci numbers, commonly denoted as F(n), form a sequence, called the Fibonacci
sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
That is,
F(0) = 0, F(1) = 1
F(n) = F(n – 1) + F(n – 2), for n > 1
Write a function and name it as fibonacci to calculate F(n) for a given n, where 0 <= n <= 30.
Below is a program that calculates the Fibonacci of an integer number entered by the user.
#include <iostream>
using namespace std;
int fibonacci(int n);
int main() {
int n;
cout << "Please input an integer number \n";
cin >> n;
cout << "The fibonacci number of " << n << " is: \n";
cout << fibonacci(n);
return 0;
}

With a correct implementation of fibonacci, the example Input/Output of this program should be:
Example 1:
Please enter an integer number:
3
The fibonacci number of 3 is:
2

Explanation: F(3) = F(2) + F(1) = F(1) + F(0) + F(1) = 1 + 0 + 1 = 2

Example 2:
Please enter an integer number:
4
The fibonacci number of 4 is:
3

Explanation: F(4) = F(3) + F(2) = F(3) + F(1) + F(0) = 2 + 1 + 0 = 3

4
Please implement int fibonacci(int n) below.
ANS:

int fibonacci(int n)
{
if (n == 1)
return 1;
else if (n == 0)
return 0;
else
return fibonacci(n-1) + fibonacci(n-2);
}

// NOTE: correct non-recursive implementation also gets full marks

5
Question 4 [20 marks]
Write a function to remove redundant numbers in an integer array. The function prototype is given
below,
int removeRedundant(int a[], int n);
where a[] is the input array, and n is the number of elements in a[]. The returned value is the
number of elements in a[] after removing all redundant numbers.
Below is a program that calls removeRedundant to process an integer array entered by the user.

#include <iostream>
using namespace std;
#define N 10
int removeRedundant(int a[], int n);
int main() {
int a[N], i;
cout << "Please enter 10 integer numbers: \n";
for (i = 0; i < N; i++)
cin >> a[i];
int new_size = removeRedundant(a, N);
cout << "After removing redundant numbers: \n";
for (i = 0; i < new_size; i++)
cout << a[i] << ' ';
return 0;
}

With a correct implementation of removeReundant, the example Input/Output of this program


should be:
Example 1:
Please enter 10 integer numbers:
2 3 9 10 2 8 9 10 7 1
After removing redundant numbers:
2 3 9 10 8 7 1

Example 2:
Please enter 10 integer numbers:
1234443210
After removing redundant numbers:
12340

6
Please implement int removeRedundant(int a[], int n) below.
ANS:

int remvoeRedundant(int a[], int n)


{
int i, j;
i = 0;
while (i < n) {
for (j = 0; j < i; j++) {
if (a[i] == a[j])
break;
}
if (j != i) {
for (j = i; j < n-1; j++)
a[j] = a[j+1];
n--;
} else {
i++;
}
}
return n;
}

You might also like