[go: up one dir, main page]

0% found this document useful (0 votes)
77 views5 pages

UE17CS151: Pes University

This document contains a sample exam for a problem solving with C course. It includes 5 sections with multiple choice, short answer, and coding questions. Section 1 covers expressions, rectangles, loops, and finding squares controlled by a queen on a chess board. Section 2 involves recursion, bitwise XOR, string replication, and pointer arithmetic. Sections 3-5 contain additional questions on arrays, structures, functions, sorting, binary search, file I/O, macros, and command line arguments.

Uploaded by

KB SCOUTS
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)
77 views5 pages

UE17CS151: Pes University

This document contains a sample exam for a problem solving with C course. It includes 5 sections with multiple choice, short answer, and coding questions. Section 1 covers expressions, rectangles, loops, and finding squares controlled by a queen on a chess board. Section 2 involves recursion, bitwise XOR, string replication, and pointer arithmetic. Sections 3-5 contain additional questions on arrays, structures, functions, sorting, binary search, file I/O, macros, and command line arguments.

Uploaded by

KB SCOUTS
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/ 5

SRN

PES UNIVERSITY UE17CS151


End Semester Assessment (ESA) B. Tech. 2nd SEMESTER – Jan - May-2018
UE17CS151 - PROBLEM SOLVING WITH C
Time: 3 Hrs. Answer All Questions Max Marks: 100
1 a Find the values of expression and/or variables or indicate errors if any. 5
i) 10 / 3 * 3
ii) 10 == 10 == 10
iii) int a = 10; a ++ ++;
iv) int a = 1; int res = a-- == 0 || 2;
v) ~~5
b A rectangle is defined by co-ordinates of the bottom left corner (x1, y1) and of the top right corner 4
(x2, y2).
i) Given a point (x, y), write an expression in C which returns true if the point lies within the
rectangle otherwise false.
ii) find the length of the diagonal of the rectangle
c int c = 0; 2+ 2+2
for(int x = n; x; x -= 2)
c++;
printf("c : %d\n", c);

Study the code and answer the following questions.


i) what happens if n = 10 ?
ii) what happens if n = 11 ?
iii) what is the semantics of c?
d A chess board has 8 rows and 8 columns. A queen in chess can move rowwise, columnwise and 5
diagonally. If a queen is placed at position (r, c), 1 <= r <= 8 and 1 <= c <= 8, write a C program to
find all squares which are under control of the queen. Express each square as a pair of numbers – row
number and column number.
2 a int foo(int n) 3+1
{
if(n == 0)
return 0;
else
return 1 + foo(n / 10);
}
What does the function return for the following? Show the stackframes.
foo(123)?
What does the function do?
b int what(int a[], int n) 4+1
{
int res = 0;
for(int i = 0; i < n; ++i)
{
res = res ^ a[i];
}
return res;
}
SRN
Find the output.
int a[] = {11, 44, 11, 33, 44};
printf(“%d”, what(a, 5));
What does the function do?
c Write a function to replicate a given string n times. Assume that the given string has enough space. 4
You may use functions in the string library.
void str_replicate(char* s, int n)
{
// ToDo
}
d i) State the three pointer arithmetic operations allowed. 3 +2+
ii) What is the output and what will change? 2
int a[] = {11, 22, 33, 44}; int *p = a + 2; printf("%d", (*p)++);
iii) What is the output and what will change?
int a[] = {11, 22, 33, 44}; int *p = a + 2; printf("%d", *p++);

3 a int is_magic(int a[][20], int n, int sum) 5


{
// TODO
}
Write a function to check whether given two dimensional square matrix(n X n) is magic square
where each row, each column and every diagonal add upto the same sum.
Return 1 if it is a magic square, 0 otherwise.
b struct person 6
{
char name[20];
int age;
};
typedef struct person person_t;
int are_equal(const person_t* x, const person_t* y)
{
// to fill
}
We would like to compare the two structures for equality. Comment about the following possibilities.
i) return x == y;
ii) return x→name == y→name && x→age == y→age
iii) return strcmp(x→name, y→name) == 0 && x→ age == y→age;
c struct person 4
{
char name[20];
int age;
};
typedef struct person person_t;
double find_average(const person_t a[], int n)
{
// TODO
}
Write a function to find the average age of n persons.
d struct node 2+3
SRN
{
int key;
struct node* link;
};
typedef struct node node_t;
node_t* find(node_t* head, int key)
{
while(head->key != key && head != NULL)
{
head++;
}
return head;
}
The above code is supposed to find the leftmost occurrence of key in a linked list.
i) This function has two logical errors. Indicate and correct them
ii) Change this to find the rightmost occurrence of the key.

4 a int is_sorted(int a[], int n) 4


{
int sorted = 0;
for(int i = 0; i < n; ++i)
sorted = a[i] < a[i + 1];
return sorted;
}
This function is supposed to find whether the array is sorted in non-decreasing order. Comment and
correct if there are logical errors.
b struct Rect 4
{
int length;
int breadth;
};
typedef struct Rect rect_t;
void sort_rect(rect_t r[], int n, int (*compare)(const rect_t*, const rect_t*));
The function sort_rect sorts an array of rectangles based on the callback compare.

Write functions which can be passed as the argument for the callback compare of sort_rect which will
arrange the rectangles
i) in the increasing order of area
ii) in the order of length and if lengths are same, in the order of breadth
c struct student 6
{
char name[20];
unsigned int p : 8;
unsigned int c : 8;
unsigned int m : 8;
unsigned int b : 8;
};
typedef struct student student_t;
SRN
student_t find_highest(const student_t s[], int n)
{
// TODO
}
The student structure holds the name and the marks in subjects p, c, m and b. The last 4 are stored in
bitfields to save space.
Complete the function to find the student with the total highest score.
d An array has 0s and 1s and has been sorted. Write a function to find the rightmost 0 using the concept 6
of binary search.
5 a Find the outputs or undefined behavior when the following block is executed. 6
{
int a = 10;
int *p = &a;
int b = 20;
{
int a = 30; int c = 50;
printf("%d %d %d\n", a, b, *p);
p = &c; b = c;
}
printf("%d %d %d\n", a, b, *p);
}
b You are given a file in.dat which has a few integers. 5
Write a program to process this file, read the integer values and write them back in reverse to a file
called out.dat. Example: if in.dat contains the following
in.dat : 10 40 20 50 30
then when the program is executed, the out.dat should contain
out.dat: 30 50 30 40 10
c #define mul(x, y) x * y 2+2+
#define MAX 10 1
#define MIN 5
#define TEMP MAX + MIN
int main()
{
printf(“val : %d\n”, mul(MAX + 1, MIN – 1));
#define MAX 20
printf(“val : %d\n”, mul((MAX + 1), MIN – 1));
printf(“temp : %d\n”, TEMP);
}
d This file x.c is compiled as follows. 4
$ gcc x.c -o x
int main(int argc, char* argv[])
{
// has some code
}
and is run as
$ ./x we love all
i) what type is argv[0] and what is its value?
ii) What are the values of argc and argv[argc]?
SRN

You might also like