0% found this document useful (0 votes)
24 views6 pages

Compre

Uploaded by

aishwaryim
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)
24 views6 pages

Compre

Uploaded by

aishwaryim
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/ 6

CSF111–Exam II (12/07/23) 100 marks

Name: BITSID:

ˆ The exam has 8 questions on three separate sheets (6 pages). Write your ID on all pages
and submit all sheets.
ˆ Answers written anywhere except in the designated space on the question paper will not
be graded. Use the given answer-books as rough sheets and turn them in.
ˆ Assume all the relevant variables are declared and all relevant header files are included.
Where not specified, assume the code is within main .
ˆ If a question has a typo, don’t bother the invigilators. Answer it with ‘This question has
a typo’ and get full marks.

Part A 2. Refute question. Consider this function:


/* *
1. Refute question. Assume the function * @brief Reports whether the given
int is_prime ( int k ) correctly determines array of length n has any
if k is prime. Now consider this function: duplicate elements .
/* * * Requires : n is the correct
* @brief Counts the pairs (p , p +2) length of the given array
where both p and p +2 are prime * Examples :
and 1 <= p < p + 2 <= n * 0 duplicate ([4 , 5] , 2)
* Requires : n > 0 * 1 duplicate ([4 , 4] , 2)
* Examples : */
* 1 prime_pairs (5) -> (3 ,5) int duplicate ( int arr [] , int n )
* 2 prime_pairs (8) -> (3 ,5) and {
(5 ,7) for ( int i = 0; i < n ; ++ i )
*/ {
int prime_pairs ( int n ) int flag = 0;
{ for ( int j = 0; j < n ; ++ j )
int ans = 0; if ( arr [ i ] == arr [ j ])
for ( int i = 2; i <= n ; ++ i ) flag ++;
if ( is_prime ( i ) ) if ( flag == 2)
if ( is_prime ( i + 2) ) return 1;
++ ans ; }

return ans ; return 0;


} }
[3M ] (a) This implementation of prime_pairs has [3M ] (a) This implementation of duplicate has
bugs, but it works correctly for some in- bugs, but it works correctly for some in-
puts, e.g., 13. Trace it for n = 13 and puts. Trace it for arr = {7 , 4 , 7} and
write the return value. write the return value.
3 1
[3M ] (b) Write a value of n for which this function [3M ] (b) Write a value of arr for which this func-
returns an incorrect result. (Hint: you can tion returns an incorrect result. (Hint: you
find a counter example under 20.) can find a counter example with n <5 .)
Any one value → any array with more than 2 copies of the same value
3, 4, 5, 6, 11, 12, 17, or 18 e.g., 4, 4, 4
[2M ] (c) What is the expected (correct) return [2M ] (c) What is the expected (correct) return
value for the input you provided? value for the input you provided?
Corresponding values →
00112233 1
[2M ] (d) What is the actual (incorrect) return value [2M ] (d) What is the actual (incorrect) return value
for the input you provided? for the input you provided?
Corresponding values →
0
11223344

Page 1 of 6
CSF111–Exam II (12/07/23) BITSID:

3. What does the C code in each subquestion [3M ] (d) int n1 = 2 , n2 = 4;


print on the console?
int foo ( int * a1 , int * a2 ) {
[3M ] (a) int fun ( char *s , char c , int k ) int flag = 1;
{ int i = 0;
int ans = -1; while ( flag && i < n1 )
if (* s != ‘\0 ’) { {
if (* s == c ) int j ;
ans = k ; for ( j = 0; j < n2 ; ++ j )
else if ( a1 [ i ] == a2 [ j ])
ans = fun (++ s , c , ++ k ) ; break ;
}
return ans ; flag = j < n2 ;
} ++ i ;
}
int main () return flag ;
{ }
char s [] = " bits ";
char c = ‘t ’; int main () {
printf ( " % d \ n " , fun (s , c , 0) ) ; int a [] = {2 , 3};
return 0; int b [] = {4 , 3 , 2 , 5};
} printf ( " % d " , foo (a , b ) ) ;
return 0;
2 }

1
[6M ] (b) void test ( char * str )
[6M ] (e) void g ( int ** y )
{
{
printf ( " % c " , * str ) ;
* y = malloc ( sizeof ( int ) ) ;
if (* str == ‘C ’)
}
return ;
if (* str >= ‘A ’ && * str <= ‘Z ’)
void f ( int x , int *y , int ** z )
test ( str + 2) ;
{
else
g (& y ) ;
test (++ str ) ;
** z += 2;
printf ("% c " , * str ) ;
* z = malloc ( sizeof ( int ) ) ;
}
** z = 40;
* y = 30;
int main ()
** z += 4;
{
x += 10;
char str [] = " ProgRamInC ";
* y += 3;
test ( str ) ;
printf ( " %d , %d , % d \ n " ,
return 0; 2 marks PogRmIC x , *y , ** z ) ;
} 4 marks IIRRgP }

PogRmICIIRRgP int main ()


{
int x = 50;
[3M ] (c) (Assume the file alpha . txt has two lines:
int * y = & x ;
the first line contains all uppercase letters int ** z = & y ;
A-Z, and the second all lowercase letters f (x , y , z ) ;
a-z in that order without space.) printf ( " %d , %d , % d " ,
FILE * fp ; x , *y , ** z ) ;
fp = fopen ( " alpha . txt " , " r " ) ; return 0;
char line [80]; }
fgets ( line , 10 , fp ) ;
printf ( " % s " , line ) ;
60, 33, 44
52, 44, 44
ABCDEFGHI

1 mark each, but must have 6 values on two lines


Many will write upto J which is wrong
Page 2 of 6
CSF111–Exam II (12/07/23) BITSID:

Part B 5. Answer whether each statement is TRUE or


FALSE in the context of C programming.
4. What does the C code in each subquestion [1M ] (a) Enums can be used as fields of a structure.
print on the console?
[2M ] (a) char s1 [] = " exam " ; True
char s2 [] = { ‘ E ’ ,‘X ’ ,‘A ’ ,‘M ’ }; [1M ] (b) You can use pointers to receive multiple
printf ( " %d , % d \ n " ,
results from a function.
sizeof ( s1 ) ,
sizeof ( s2 ) ) ; True
1 mark each, but must have 2 values[1M ] (c) Accessing the k -th element in a k -length
5, 4
missing commas is fine array leads to a runtime error.
[6M ] (b) int main () -1 for any additional characters
True ; But we are also considering False
{
depending on the interpretation
int n1 = 3 , n2 = 1 , n3 = 4;
[1M ] (d) Structures let you create new types beyond
switch ( n1 ) the built in types like int , etc.
{
case 1: True
n2 = 2;
[1M ] (e) If you pass an array of 10 integers to a
break ;
function f from g , 10 integer values are
case 3: copied to the data area of g to that of f .
for ( int n3 = 1; False
n3 <= n1 ;
There’s a typo, it should be ‘copied from’.
n3 ++)
{
If they write ‘typo’, give full marks
if ( n2 == n3 ) 6. Code completion.
continue ;
else
[3M ] (a) Write the expression to replace e1 so that
{
n2 += 3; the function matches its description.
break ; /* *
} * @brief Computes and puts
} the reverse of string s
in ans .
case 2: *
n3 += 3; * Requires : sizeof ( s ) = sizeof
( ans ) > 0
default : *
while ( n3 >= 0) * Examples :
{ * s = " a " -> ans = " a "
int n1 = 2; * s = " ab " -> ans = " ba "
if ( n3 % 2) * s = " abc " -> ans = " cba "
{ */
n3 - -; void rev_str ( char *s , char * ans )
break ; {
} int n = strlen ( s ) ;
n3 - -; for ( int i = 0; i < e1 ; ++ i )
} {
} ans [ i ] = s [ n - i - 1];
ans [ n - i - 1] = s [ i ];
printf ( " %d , %d , % d " , }
n1 , n2 , n3 ) ; ans [ n ] = ‘\0 ’;
return 0; }
}

e1 n/2+1
3, 4, 6
2 marks for each, but must have 3 values n / 2 will not work for strings with odd lenght
missing commas is fine
Page 3 of 6
-1 for any additional characters
CSF111–Exam II (12/07/23) BITSID:

[6M ] (b) The following program represents a Rock- [2M ] (d) Write the expression to replace e5 so that
Paper-Scissors game: r defeats s , p defeats the program prints mickey when executed
r , and s defeats p . as
Write the expressions to replace e2 and e3 ./ cli mickey donald
so that the function matches its descrip- Assume the code is in a file cli . c that is
tion. compiled as:
typedef enum { r , p , s } move ; gcc cli . c -o cli
int main ( int argc , char ** argv )
struct rps { move p1 , p2 ; };
{
printf ( " % s \ n " , e5 ) ;
/* *
return 0;
* @brief Returns 1 if P1 wins ,
}
* 2 if P2 wins ,
* 0 if drawn .
*/ argv[1]
e5
int win ( struct rps g ) {
int ans = 0;
if ( e2 ) e2-> g.p1 - g.p2 == 1 [6M ] (e) You are given the following struct s:
ans = 1; || g.p1 - g.p2 == -2
if ( e3 )
OR typedef struct point
ans = 2; (g.p1 == r && g.p2 == s) ||
{
return ans ; (g.p1 == p && g.p2 == r) || double x , y ;
} (g.p1 == s && g.p2 == p) } point ;
e3 -> g.p2 - g.p1 == 1 typedef struct circle
|| g.p2 - g.p1 == -2 {
e2 OR point ctr ;
(g.p2 == r && g.p1 == s) || double r ;
(g.p2 == p && g.p1 == r) || } circle ;
e3 (g.p2 == s && g.p1 == p) and a function
[3M ] (c) Write the expression to replace e4 so that double dist ( point * p , point * q )
the function matches its description. As- that computes the distance between the
sume it is initially called with z = 1 . two points p and q .
/* * Write the expressions to replace e6 and
* @brief Puts the lowest e7 so that the function below determines
common multiple of x and y whether the given circles interesect each
in z . other.
* Requires : x > 0 , y > 0
* int intersect ( circle * c1 ,
* Examples : circle * c2 )
* lcm (4 , 5 , z ) -> z = 20 {
* lcm (4 , 6 , z ) -> z = 12 double d = dist ( e6 ) ;
*/ return e7 ;
void lcm ( int x , int y , int * ans ) }
{
if ( e4 ) &c1→ctr, &c2→ctr
{
2 marks each = 4
* ans = * ans + 1; e6
lcm (x , y , ans ) ;
} d <= c1→r + c2→r
}
e7 < is also fine 2 marks
(*ans % x !=0) || (*ans % y != 0)
Or some variation of this
In both, (*p).x can be used in place of p→x
e4 Note- the above expression is correct
even without the parentheses

Page 4 of 6
CSF111–Exam II (12/07/23) BITSID:

Part C
[5M ] 7. (a) Implement this C function to match the purpose/contract. Do not create any helper functions.
/* *
* @brief Returns the number of words in the given file .
*
* Requires : The file contains one word per line and
each line has at most 80 characters
*/
int word_count ( FILE * fp )

One possible solution: line (the buffer variable) must be more than 80,
any length is fine
int ans = 0;
char line[100]; fgets is the easiest way, some may write a loop
while (fgets(line, 81, fp)) with fgetc which will look like
ans++;
return ans; int c;
while ((c = fgetc(fp)) != EOF) {
if (c == ‘\n’)
ans++;
}

[8M ] (b) Implement this C function to match the purpose/contract. Do not create any helper functions.
/* *
* @brief Returns the first n words in the file as an array of strings .
*
* Requires : fp points to a file that has at least n words one per line and
each line has at most 80 characters
*/
char ** words ( FILE * fp , int n )

char line[100];
char **ans = calloc(n, sizeof(char *));
int i = 0;
while (i < n)
{
fgets(line, 81, fp); Here again, they can use fgetc, in which case,
int k = strlen(line); they must add ‘\0’ manually
ans[i] = malloc((k+1) * sizeof(char));
strcpy(ans[i], line);
++i;
}
return ans;

Page 5 of 6
CSF111–Exam II (12/07/23) BITSID:

8. You are given the following code:

# define INF 100 E100

typedef struct point { double x , y ; } point ;

[5M ] (a) Implement this C function to match the purpose/contract. Do not create any helper functions.
/* *
* @brief Returns the slope of the line segment formed by the given points
*
* slope = INF , if the two points have the same x - coordinate
* = ( diff in y coordinates ) /( diff in x coordinates ) , otherwise
*
* Examples :
* 3 slope ({2 ,1} , {4 ,7})
* 0.5 slope ({8 ,6} , {2 ,3})
* INF slope ({0 ,0} , {0 ,1})
*/
double slope ( point * p1 , point * p2 )

One possible solution: Rubric:


2 - correct use of →. Any p→x can be written as (*p).x
double ans = INF; 2 - correctly calculating slope, including assigning INF
double dx = p2→x - p1→x; 1 - correct return
if (dx != 0)
ans = (p2→y - p1→y) / dx; -1 one or more syntax errors
return ans; -1 wrong types

[8M ] (b) Implement this C function to match the purpose/contract. Do not create any helper functions. You
must call slope declared above.
/* *
* @brief Returns an (n -1) element array containing slopes of line segments
formed by consecutive points in the given array . The ( k ) th element in
the resulting array is the slope of the segment formed by the ( k ) th and
( k +1) th element in the input array .
*
* Requires : n is the length of the given array and n > 1
*
* Examples :
* [0.5] slopes ([{8 , 6} , {2 , 3}])
* [0.5 , 0.75] slopes ([{2 , 3} , {8 , 6} , {0 , 0}])
*/
double * slopes ( point pts [] , int n )

One possible solution:

double *ans = calloc(n - 1, sizeof(double));


for (int i = 0; i < n - 1; ++i)
ans[i] = slope(&pts[i + 1], &pts[i]);

return ans;

Page 6 of 6

You might also like