CS151 Sem2 ISA2Morning Solns
CS151 Sem2 ISA2Morning Solns
Q1 a) Consider the data file having the details of 10 Mobiles(Model_id, Price in Rs and the Number 6M
of quantities available). Use the C code to display the total Value of that model if the price of
each model per unit is less than 10K. Total value = Price per unit * Number of quantities.
Use redirection operator to provide data file as an input to the executable.
1 6499 650
10 7999 800
3 8999 900
6 11990 1199
5 10990 1099
4 9990 999
7 19990 1999
8 36990 3699
9 36990 3699
2 25990 2599
Solution:
#include<stdio.h>
int main()
{
int i = 0; // variable declarations 1 mark
int id;
int price;
int num_quant;
printf("reading from the sata file\n");
while(i<=10) // any loop, 1 mark
{
scanf("%d %d %d",&id,&price,&num_quant); // 1 mark
if(price<10000) // 1 mark
{
printf("Model id is %d and ",id);
printf("total price for %d quatities is
%d\n",num_quant,num_quant*price); // 1 mark
}
i++; // 1 mark
}
return 0;
}
b) Write a neat diagram to describe the Program development life cycle of a C program. Also 4M
mention the output of each phase in the same diagram.
Solution:
SRN
Q2 a) Write a function to find whether the sum of all the integers in an array is divisible by 10 or not. 6M
Test this function in the client code and display appropriate message.
Solution:
#include<stdio.h>
int find_sum_check_divisibility(int *a,int n);
int main()
{
int a[] = {2,3,5,7,2,1};
int n = sizeof(a)/sizeof(*a);
int result = find_sum_check_divisibility(a,n);
if(!result) // printing appropriate msg 2 marks
printf("the sum is divisible");
else
printf("the sum is not divisible by 10");
}
int find_sum_check_divisibility(int *a,int n)
{
int sum = 0; int i; // variable declaration 1 mark
for(i = 0;i < n; i++) // any loop with proper body 2 marks
sum += a[i];
return sum%10; // return value 1 mark
}
b) i) Mention the two ways of invoking the gdb. 4M
ii) What is the output of below code snippet?
char s[] = {'D','A','T','A'};
printf("%lu\n",sizeof(s));
printf("%d\n",strlen(s));
Solution:
i) any two, 2 marks
gdb and then press enter key
gdb executable and then press enter key
gdb -p process_id and then press enter key
gdb -q OR gdb --quiet or gdb –silent and then press enter key
ii) 2 marks
4
Any undefined value
Q3 a) Fill the implementation of Deque function to perform Ascending Priority Queue. Client code is 6M
given to understand the interface of the function.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
SRN
struct component
{ char details[20];
int priority;
}; typedef struct component compo_t;
struct node
{ compo_t c;
struct node *link;
}; typedef struct node node_t;
struct priority_queue
{
struct node *head;
}; typedef struct priority_queue prio_t;
void init_queue(prio_t* pq);
void enqueue(prio_t* pq,compo_t* c);
void dequeue(prio_t* pq);
void display(const prio_t* pq);
int main()
{ prio_t q;
init_queue(&q);
compo_t c;
int choice,ele;
printf("enter ua choice\n1.insert 2.delete 3.display 4 exit\n");
scanf("%d",&choice);
do
{
switch(choice)
{ case 1: printf("enter the detail and priority\n");
scanf("%s %d",c.details, &(c.priority));
enqueue(&q,&c); break;
case 2: dequeue(&q); break;
case 3: display(&q); break;
}
printf("enter ua choice\n1.insert 2.delete 3.display 4 exit\n");
scanf("%d",&choice);
}while(choice<4);
return 0;
}
void init_queue(prio_t* pq)
{ pq->head = NULL; }
void enqueue(prio_t* pq,compo_t* c)
{ // code to make the node using the component and insert this node to list is available here }
void dequeue(prio_t* pq)
{ // Fill this implementation }
void display(const prio_t* pq)
{ // code to display all the nodes in the list is available here }
Solution:
void dequeue(prio_t* pq)
{
if(pq->head == NULL) // 1 mark
printf("no elements to delete\n");
else
{
SRN
node_t* present = pq->head; // initially three pointers declarations 1 mark
node_t* prev = NULL;
node_t* prev_max = NULL;
int max = 0;
while(present != NULL)
{
if(present->c.priority<=max) // 2 marks for this while with if
{
max = present->c.priority;
prev_max = prev;
}
prev = present;
present = present->link;
}
compo_t compo;
if(prev_max != NULL) // 1 mark for checking prev_max and freeing temp
{
node_t* temp = prev_max->link;
prev_max->link = temp->link;
strcpy(compo.details,temp->c.details);
compo.priority = temp->c.priority;
free(temp);
}
else
{
node_t* temp = pq->head;
pq->head = pq->head->link; // changing head and freeing temp1 mark
strcpy(compo.details,temp->c.details);
compo.priority = temp->c.priority;
free(temp);
}
printf("deleted node detail is %s with priority
%d\n",compo.details,compo.priority);
}
b) Expected output of below code execution is 10 and Peter separated by a space. But this code is 4M
resulting in compile time Errors. Please detect and correct the errors to get the expected output.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct EMPLOYEE
{ int id; char name[100]; };
void display(const struct EMPLOYEE*);
int main()
{ struct EMPLOYEE *e = (struct EMPLOYEE)malloc(sizeof(struct EMPLOYEE));
e.id = 10; strcpy(e.name,"Peter");
display(e); return 0;
}
void display(const struct EMPLOYEE* a)
SRN
{ a->id = 101; printf("%d %s",a->id,a->name); }
Solution: 1 marks each
1. * missing in type casting
EMPLOYEE *e = (struct EMPLOYEE*)malloc(sizeof(struct EMPLOYEE));
2. -> must be used because e is a pointer
e->id = 10;
3. -> must be used because e is a pointer
strcpy(e->name,"Peter");
4. const is used with the parameter. We cannot change a
a->id = 101 please remove
Q4 a) Write a C program to copy the contents of one file to another with a comma appended at the 6M
end of each line. Sample input and output files are provided for clarity of the requirement.
Sample input file:
Digital image processing is the use of a digital computer
to process digital images through an algorithm. It has many advantages
over analog image processing
The generation and development of digital image processing are mainly affected by three
factors
The development of computers
The development of mathematics
The demand for a wide range of applications in environment,agriculture, military, industry and
medical science
Sample output file:
Digital image processing is the use of a digital computer,
to process digital images through an algorithm. It has many advantages,
over analog image processing,
The generation and development of digital image processing are mainly affected by three
factors,
The development of computers,
The development of mathematics,
The demand for a wide range of applications in environment,agriculture, military, industry and
medical science,
Solution:
#include<stdio.h>
int main()
{
FILE *fpr = fopen("sample_data.txt","r"); // read mode 1 mark
FILE *fpw = fopen("sample_output.txt","w"); // write mode 1mark
char data[1000];
while(fgets(data,1000,fpr) != NULL) // 1mark
{
fputs(data,fpw); // 1 mark
fseek(fpw,-2,SEEK_END); // 1 mark
fputc(',',fpw); // 1 mark
fputc('\n',fpw);
}
fclose(fpr); fclose(fpw); return 0;
}
b) What is the output of below code execution? 4M
#include<stdio.h>
void f1();
int fun(int n, void (*f)());
void f2();
int main()
SRN
{ fun(10,f1); fun(100,f2); }
void f1()
{ printf("in f1\n"); }
void f2()
{ printf("in f2\n"); }
int fun(int n, void (*f)())
{ f(); printf("%d\n",n); return 0; }
Solution: 1 marks each
In f1
10
In f2
100
Q5 a) Contents of Player.txt are as below. 5M
xyz 4 60 2019
pqr 7 100 2018
xyz 4 47 2020
abc 3 90 2018
def 8 97 2019
xyz 4 70 2018
The first column represents the Name of the player. The second column represents the ID. The
third column represents the score and the last column represents the year in which the player
has scored that particular score. All these columns are separated by a tab.
Write a C code to read the contents of Player.txt and store these details of players in a user
defined type called PLAYER. Also print the number of records.
Solution:
typedef struct Player // structure declaration 1 mark
{
char name[50];
int id;
int score;
int year;
}PLAYER;
int main()
{
FILE *fr=fopen("Player.txt","r");
char a[200];
char *item;
PLAYER p[100];
int i=0;
while(fgets(a,200,fr)) // loop 1 mark
{
item=strtok(a," "); // strtok +NULL 1 mark
strcpy(p[i].name,item);
item=strtok(NULL," ");
p[i].id = atoi(item); // atoi OR typecasting is 1 mark
item=strtok(NULL," ");
p[i].score = atoi(item);
item=strtok(NULL," ");
p[i].year = atoi(item);
i++;
}
int n = i; // 1 mark
printf("Number of records is %d\n",n);
fclose(fr);
}
b) Write the implementations of sort and swap functions to sort the contents of Player.txt based on 5M
the ID of the player using selection sort and array of pointers.
int main()
{ PLAYER p[100]; // Array of structures which contains the data from the file Player.txt
PLAYER *ap[100]; // Array of Pointers to structures
SRN
init_ptr(p, ap, n); printf("before sorting\n"); disp(ap,n);
sort(ap,n); printf("after sorting\n"); disp(ap,n); return 0;
}
void init_ptr(PLAYER p[], PLAYER *ap[], int n)
{ int i;
for(i = 0;i<n;i++)
ap[i] = &p[i];
}
void swap( PLAYER** p1, PLAYER **p2)
{ //Fill this implementation }
void sort(PLAYER *ap[],int n)
{ // fill this implementation }
void disp(PLAYER *ap[],int n)
{ int i;
for(i = 0;i<n;i++)
printf("%s\t%d\t%d\t%d\n",ap[i]->name,ap[i]->id,ap[i]->score, ap[i]->year);
}
Solution:
void swap( PLAYER** p1, PLAYER **p2)
{
PLAYER* temp = *p1; // 1 mark for swap function body
*p1 = *p2;
*p2 = temp;
}
void sort(PLAYER *ap[],int n)
{
int i,pos,j;
for(i = 0;i<n-1;i++) // 2 loops 2marks
{
pos = i;
for(j = i+1;j<n;j++)
{
if((ap[pos]->id) > (ap[j]->id)) // 1 mark
pos = j;
}
if(pos != i) // 1 mark
swap(&ap[pos],&ap[i]);
}
}
Q6 a) If the size of integer is 4 bytes, what is the output of below code? Explain briefly. 5M
#include<stdio.h>
struct DATA
{ unsigned int data1:5;
unsigned int data2:28;
};
int main()
{ printf("Size of structure is %lu\n",sizeof(struct DATA));
struct DATA d; d.data1 = -8; printf("%d",d.data1); return 0;
}
Solution: 2 marks for output and 3 marks for brief explanation
8
SRN
24
Explanation:
Here, bit fields are used. Hence the size is 5+28 = 33. This is more than 4 bytes and hence 8
bytes are allocated by the compiler and padding done in remaining bits for alignment
purposes.—1 mark