Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ESC112A Quiz-4

Jan 31, 2023 Name ..................................


Duration 30 Minutes
Max Points: 15 Roll no. ..................................
Section .....................................
Closed Book and
Closed Notes

Instructions

1. The exam is closed book and closed notes. Use of mobile phones or access to internet
is not allowed.
2. This quiz paper has two sheets or four pages. There are a total of two questions.
3. Write your name, roll no. and section on both this and the third page top, in the
spaces provided.
4. Write your nal answers neatly in the space provided, strike out any rough work.
5. Space for rough work is provided at the bottom of some pages.

rough work below this line -

1
Q1(points 7) Consider the code given below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {char name[16];
char *surname;
double cpi;};
int main(){
struct student s1, s2;
< ... some code involving s1, s2 ...>
/* copy s2 to s1 without using structure assignment s1=s2 */
s1.cpi = s2.cpi;
.........1.........
.........2.........
/* the code between previous comment and this comment
should be equivalent to executing s1=s2 */
< ... some other code involving s1, s2 ...>
}

In this code `structure student' denition is modied a little from what was considered
in the lecture. As you know, structre assignment statement s1=s2; copies s2 to s1. In
this question, we wish to achieve the same eect without using structure assignment.
Instead we may use explicit assignments among elds of s1 and s2 only.
Write in your answer to part (i) below, what should be substituted for ...i... in
the code above so that executing
s1.cpi = s2.cpi; .........1......... .........2.........

is equivalent to executing s1=s2;.


(1)

(2)
Name ................................... Roll no. .................... Section .............
Q2(points 8)

#include <stdio.h>
struct stringWrite {char *string; int i;};
struct stringWrite makeStringWrite(char *s){
struct stringWrite temp;
temp.string = s;
temp.i = 0;
return temp;
}
struct stringWrite write(struct stringWrite s, char* u){
int i=0;
while(u[i])
s.string[s.i++]=u[i++];
s.string[s.i]=0;
return s;
}
char *read(struct stringWrite s){
return (s.string);
}

int main(){
char temp1[50], temp2[50];
struct stringWrite s= makeStringWrite(temp1);
struct stringWrite t= makeStringWrite(temp2);
s=write(s, "abc");
t=write(t, "12345");
s=write(s, "defg");
t=write(t, "6789");
printf("s=%s\nt=%s\n",read(s), read(t));
}
(a) Write below output of the code.

(b) Now, replace temp2 in the third line of the body of function main by temp1.
That is, the third line is changed from
struct stringWrite t= makeStringWrite(temp2);
to
struct stringWrite t= makeStringWrite(temp1);
Write below output of the code after this change.

 rough work below this line -

You might also like