Sunteți pe pagina 1din 10

IITM-CS1100 : Introduction to Programming (Jan-May 2019) End-Semester Examination

Mon, May 6, 2019, 09:00 - 12:00 Max Marks : 80

To be filled in by the student : (1 mark !)

Roll No :

Name :

Final Evaluation:
(for office use only)
Read the following instructions carefully
Question Points Score
• Write down your Roll No and Name above in the space pro-
1 14 vided (an exercise worth 1 mark !).
2 3 • Answer ALL questions. Answers are to be written in the
space provided in the question paper booklet itself.
3 5
• For questions where the instruction in the beginning of the
4 4 question says predict the behaviour of the following program
- you are expected to write down compilation errors if it will
5 5 occur. If you think the compilation will be successful, you are
expected to write the output of the program. Use t symbol
6 4 for indicating relevant blank spaces in the output (if needed).
7 4 • Some questions give snippets of code, which you are expected
to complete.
8 7
• When you are asked to write a program - pseudo-codes will
9 12 not be accepted as answers. Syntax errors in the program
will be penalized.
10 11 • When you are asked to write a program, you are expected to
write header files required wherever necessary. For example,
11 5 using the strlen function without including string.h will
12 5 be penalized.
• You will be given separate sheets for rough work.
13 1
Total: 80

Grading Process Table (for office use only)

Question: 1 2 3 4 5 6 7 8 9 10 11 12 13 Total
Points: 14 3 5 4 5 4 4 7 12 11 5 5 1 80
Score:
1. Short answer type questions. In C language . . .:
(a) (2 marks) Which data type is appropriate for each of the following kinds of data?
(I) Average height of students in class.

float

(II) The frequency histogram of letters in the sentence “able was I ere I saw elba”.
array

(b) (3 marks) Predict the output of code snippets.

(I) printf("%10d",12345);

Solution:
_____12345

(II) printf("%d",4 > 5? 4:5);

Solution:
5

(c) (1 mark) Suppose a,b,c had values 25,17,9 respectively.

printf ("%d", (a<b)?(a>c?a:c):(b>c?b:c));


the value printed is:

Solution:
17

(d) (1 mark) The 1s complement of the binary number : 01010 is:

Solution: 10101

(e) (1 mark) Write down -4 in 3 bit 2s complement form.

Solution: 100

(f) (1 mark) Convert 237 in base 8 to decimal.


Solution: 159

(g) (3 marks) Assume that a function GCD that computes the GCD of two nonzero positive integers is
available to you. How will you use this function to find the GCD of three nonzero positive integers.
Write a small snippet of code.

Your Answer:

int GCD3 (int m, int n, int q)

Solution: return GCD (GCD(m,n), q);

(h) (2 marks) Suppose S1, and S2 are two variables of type STUDENT which contains members firstName,
lastName, rollNumber all of type char *, then which of the following are valid statements in C:
1. S1 = S2 ;
2. if (S1 == S2) printf("%s", S1.lastName);

Your Answer:

Solution: Statement 1 is valid


Statement 2 will give a syntax error

2. (3 marks) Consider the following program, and predict the output (as in the printf statement).
int main () {
char array[26];
int i;
for (i = 0; i < 26; i++)
array[i] = i+65;
array[15]++;
printf ("%c %c \n", array[15], array[16]);
return 0;
}

Your Answer:

Solution: Q Q One other solution: include <stdio.h> is missing, will not print anything.
3. (5 marks) Predict the behaviour of the following program:
#include <stdio.h>
int main () {
char array[26];
int i;
for (i = 0; i < 26; i++) Your Answer:
array[i] = i+65;
for (i = 0; i < 26; i++)
switch(array[i])
{ Solution: case ’Q’: 81
case ’Q’: printf("%d \n", array[i]); case ’F’: Fail
break; 5
case ’F’: printf("Fail \n");
case ’R’: printf("%d \n", i); case ’R’: 17
break; others: none
default: printf("none \n");
}
return 0;
}
4. (4 marks) Write a program that takes three single digit numbers, corresponding to that of the units, tens,
and 100s places of a number, and prints the number.

Your Answer:

Solution:

#include <stdio.h>
int main() {
int a, b, c;
scanf ( "%d %d %d:", &a, &b, &c);
printf ("number is %d\n", a + b*10 + c*100);
return 0;
}

5. (5 marks) Assume that arrays a and b contain the coefficients of two polynomials (ascending order of
degree), respectively. m and n correspond to the degrees of polynomials a, b, respectively. Replace {?} so
that the array c stores the coefficients of product of polynomials a and b, in ascending order of degree.
#include "stdio.h" Your Answer:
int main () {
int i, j;
int m, n;
int a[10],b[10], c[21]; Solution:
.
{?1:} <= m
.
{?2:} <= r
.
{?3:} i+j
for (i = 0; i {?1}; i++) {
{?4:} c[i+j]
for (j = 0; {?2}; j++) {
{?5:} j
c[{?3}] = {?4} + a[i]*b[{?5}];
}
}

6. (4 marks) The function MatrixMultiply computes the product of two matrices A and B and stores the
result in C. Assume that the matrices are defined to be of size [N1][N1], and are large enough to store
the result. Replace {?} appropriately so that it operates as specified. Assume that the matrix A[][] is
of dimension m × n, and matrix B[][] is of dimension n × r.

void MatrixMultiply (int A[][{?1}], int B[][{?2}], int C[][{?3}], int m, int n, int r) {
int i, j, k;
int sum;
for (i = 0; i {?4}; i++) {
for (j = 0; j {?5}; j++) {
sum = 0;
for (k=0; k < n; k++) {
{?6}
}
C[i][j] = sum;
}
}

Solution: {?4}:

< m
Solution: {?1}: N1
{?2}:N1 {?5}:
{?3}:N1
< r

{?6}: sum = sum + A[i][k]*B[k][j];

7. (4 marks) Write a function Swap that exchanges two char variables (assume letters of the alphabet) with-
out using a temporary variable.
Your Answer:

Solution:

void Swap (char *x, char *y) {


*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}

8. Given a task to sort an array a of size n containing integers.


(a) (4 marks) Write down a complete function that takes as input an array a, and size n, and sorts it
in descending order using bubble sort.

Your Answer:

Solution:
void BubbleSortDescend (int *array, int n) {
int i = 0,j;
int temp;
int exchanges = 1;
int numtimes = 0;

while ((i < n) && (exchanges == 1)) {


exchanges = 0;
j = i;
while (j < n) {
if (array[j] < array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
exchanges = 1;
numtimes++;
}
printf ("i = %d j = %d last elem = %d\n", i, j, array[n-i-1]);
j++;
}
i++;
}
printf ("numtimes = %d \n", numtimes);
}

(b) (3 marks) If the array a = [8, 7, 5, 6, 3, 4, 2, 1] how many exchanges are made by your
program.

Your Answer:

Number of exhanges: 2
Number of comparisons: 13

9. In this question, you are asked to design (using structures) a data type that can hold the information
about a student at IIT Madras with the following fields. (1) Roll Number (8 characters) (2) first name (20
characters) (3) last name (10 characters) (4) program registered (1 character) (5) marks (unsigned integer).

(a) (4 marks) Declare the structure. Indicate what each variable represents as comments in the code.
Typedef your structure to name it Student.

Your Answer:

Solution:
typedef struct {
char firstName[21];
char lastName[11];
char rollNumber [9];
char program;
unsigned marks;
} STUDENT;

(b) (4 marks) Write down a main program which reads in the information about 10 students from the
user and stores them in an array named Students each element of which is of type Student. You
can assume that the input data is compliant with the constraints specified in the beginning of the
question.
Your Answer:

Solution:
#include <stdio.h>
#include <string.h>
int main () {
int i;
char line[200];
STUDENT Students[10];
for (i = 0; i < 10; i++) {
fgets (line);
sscanf (line, ‘‘%s %s %s %c %d’’, Students[i].rollNumber,
Students[i].firstName, Students[i].lastName,
Students[i].rollNumber, marks);
}
}

(c) (4 marks) Define a pointer ptr to the Student structure. Allocate space for ptr. Write a printf
statement to print the last name and marks fields of ptr.

Your Answer:

Solution:
STUDENT *ptr;
ptr = (STUDENT *) malloc (sizeof(STUDENT));
printf ("%s %d \n", ptr->lastName, ptr->marks);

10. There is a file named Document.txt which is made up of strings (made up letters of the alphabet) sepa-
rated by spaces on each line. Assume that each line has a maximum length of 200 characters.

(a) (5 marks) Write a program to determine the number of lines in the file.
Your Answer:

Solution:
#include <stdio.h>
int main () {
FILE *fileIn;
int numLines;
fileIn = fopen ("Document.txt", "r");
numLines = 0;
while (fgets (line, 200, fileIn) != NULL)
numLines++;
}

(b) (6 marks) Write a program to determine if a given “keyword” (taken as input from the user) is
present in the file. If the “keyword” is found, your program must print the first line in the file, that
contains the “keyword.”

Your Answer:

Solution:
int main () {
FILE *fileIn;
int numLines;
fileIn = fopen ("Document.txt", "r");
while (fgets (line, 200, fileIn) != NULL)
if (strstr(line, keyword) != NULL) {
printf ("%s \n", line);
break;
}
fclose(fileIn);

11. (5 marks) The recursive function NCR computes N Cr , where N and r are unsigned integers. Assume
N is nonzero. Complete it.
Solution:

int NCR(int n, int r)


{
if ((n < 0) || (r < 0) || (n == 0) || (n < r))
return (0);
else if ((n == r) || (r == 0))
return (1);
else return (NCR(n-1, r-1) + NCR (n-1, r));
} /* End of NCR End of NCR */

12. (5 marks) Predict the behavior of the following function Function for different nonzero integer values.
Additionally give three examples.

Your Answer:

#include<stdio.h> Solution: It returns the value of 91 for any


int Function(int n) { value of n <= 100.
if (n > 100) return (n-10); Otherwise returns n - 10.
else return (Function(Function(n+11))); G(100) = G(G(111)) = G(101) = 91
} G(99) = G(G(110)) = G(100) = G(G(111))
= G(101) = 91
G(200) = 190

13. (1 mark) Reminder : Filling up the name and roll number correctly. This is worth 1 mark !.

S-ar putea să vă placă și