Compre
Compre
Compre
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.
Page 1 of 6
CSF111–Exam II (12/07/23) BITSID:
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 }
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:
[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 )
[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 )
return ans;
Page 6 of 6