0% found this document useful (0 votes)
60 views9 pages

CS151 Sem2 ISA2Morning Solns

The program reads data from an input file line by line. For each line read, it appends a comma to the end and writes the modified line to an output file. This continues until all lines from the input file are read and copied to the output file with commas appended at the end of each line.

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)
60 views9 pages

CS151 Sem2 ISA2Morning Solns

The program reads data from an input file line by line. For each line read, it appends a comma to the end and writes the modified line to an output file. This continues until all lines from the input file are read and copied to the output file with commas appended at the end of each line.

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/ 9

SRN

PES University, Bengaluru UE20CS151


(Established under Karnataka Act No. 16 of 2013)

JULY 2021: IN SEMESTER ASSESSMENT B.TECH II SEMESTER


TEST – 2
UE20CS151 – PROBLEM SOLVING WITH C

Time: 2 Hrs Scheme and Solution Max Marks: 70

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

Binary representation plus accessing 5 bits is 2 marks


-8 is stored as 2’s complement of 8, i.e.,
8 --- 00001000
1s complement --- 11110111
2s complement --- 11111000
But only 5 bits are accessible for data1. So the decimal value of 11000 is printed  24
b) i) List three differences between enumeration and macro. 3+2
ii) Find the output of below code M
#define DATA 0
#include<stdio.h>
enum data
{ DATA, DATA_1, DATA_2 };
int main()
{ printf("%d",DATA); return 0; }
Solution:
i) 1 mark each
• Macro doesn’t have a type and enum constants have a type int.
• Macro is substituted at pre-processing stage and enum constants are not.
• Macro can be redefined using #define but enum constants cannot be redefined.
However assignment operator on a macro results in error
ii) Compiletime Error // 2 marks
Q7 a) What is the output of below codes. 5M
i) #include<stdio.h>
int main()
{ const float f1 = 3.14; const float f2 = 6.28;
const float *p = &f1; p = &f2; printf("%f",*p); return 0;
}
ii) #include<stdio.h>
int main()
{ float f1 = 3.14; const float f2 = 6.28; float * const p = &f1;
p = &f2; printf("%f",*p); return 0; }
iii) #include<stdio.h>
int main()
{ short a = 10; long b = 20; printf("%d",a); return 0; }
iv) #include<stdio.h>
void fun_static();
int main()
{ fun_static(); fun_static(); return 0; }
void fun_static()
{ static int a; a--; printf("%d\t",a); }
Solution: 1 marks each
i) 6.280000
ii) Compiletime Error
iii) 10
iv) -1 -2
b) i) Write a program in C to receive 3 words in command line excluding the executable name. 3+2
SRN
Concatenate these to form a string and print the concatenated string. M
ii) Explain the usage of setenv with an example code snippet.
Solution:
i) #include<string.h>
#include<stdio.h>
int main(int argc,char *argv[]) // 1 mark
{
char a[1000];
strcpy(a,argv[1]); // usage of strcpy once and strcat twice, 2 marks
strcat(a,argv[2]);
strcat(a,argv[3]);
printf("%s",a);
return 0;
}
ii) setenv is used to set the value of a environment variable if name already exist. Else, it
creates the name with the value specified. - 1 mark
setenv(name,value,overwrite); //syntax or code with the description about the last argument, 1
mark
last argument if non-zero, name will be replaced with the value given
setenv(“PATH”,”new value”,1);
setenv(“HOME”,”VALUE”, 1);

You might also like