Introduction to programming in C Week 7
Course Name: Introduction to programming in C
Course Link: Click Here
These are Introduction to programming in C Assignment 7 Answers
Question 1
Create a database of students using structures, where in each entry of the database will have the following fields:
- A name, which is a string with at most 128 characters
- Marks in physics which is an int between 0 and 100
- Marks in chemistry which is an int number between 0 and 100
- Marks in mathematics which is an int number between 0 and 100
You have to output a list of students in the following order.
- If a student ‘A’ has higher marks in physics than a student ‘B’, then A’s data is listed before B.
- If A and B have the same physics marks and A has higher chemistry marks than B, then A is listed before B.
- If A and B have the same marks in physics and chemistry, and A has higher marks in mathematics than B, then A is listed before B.
You may assume that each student has a different mark in mathematics.
These are Introduction to programming in C Assignment 7 Answers
Input Format :
First line contains the number of students n, where 1<=n<=100.
In following n lines each line contains(space separated) a name and their respective marks in physics, chemistry, maths, where 0<=marks<=100.
Output Format :
Sorted database of n lines.
Hint: You can use qsort function in C to sort the elements in the structure array.
Sample Input
3
Dave 83 99 77
Alice 92 88 57
Evan 83 99 64
Sample Output
Alice 92 88 57
Dave 83 99 77
Evan 83 99 64
Explanation
Alice has the more marks in Physics than everyone.
Dave has same marks as Evan in Physics and Chemistry but more in mathematics.
These are Introduction to programming in C Assignment 7 Answers
Solution
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[128];
int phy;
int chem;
int maths;
};
struct student data[100];
int n;
int cmp(const void* a, const void* b)
{
struct student *ia = (struct student *)a;
struct student *ib = (struct student *)b;
if (ia->phy == ib->phy)
{
if (ia->chem == ib->chem)
{
return (ib->maths - ia->maths);
}
else
{
return (ib->chem - ia->chem);
}
}
else
{
return (ib->phy - ia->phy);
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%s %d %d %d", data[i].name, &data[i].phy, &data[i].chem, &data[i].maths);
}
qsort(data, n, sizeof(struct student), cmp);
for (int i = 0; i < n; i++)
{
printf("%s %d %d %d\n", data[i].name, data[i].phy, data[i].chem, data[i].maths);
}
return 0;
}
These are Introduction to programming in C Assignment 7 Answers
Question 2
You have to complete the code for performing the following operations in a priority queue linked list:
- Create and return a node e with given id and value val
struct node * create_node(int id, int val) - Add an node e to the beginning of the list. Return the new list.
struct node * append(struct node * list, struct node * e); - Search for a node e with id inside the list. Return a pointer to e if found, else return NULL
struct node * search(struct node * list, int id); - Change the value of an element e, (if found) in the list to the new value val.
void change_priority((struct node *list, struct node* e, int val);
These are Introduction to programming in C Assignment 7 Answers
Each element in the linked list is a structure of the following format:
struct node{
int id;
int priority;
struct node *next;};
The field priority denotes the priority of an element inside the list, the higher the value of the field, the more the priority.
Element with the highest priority is returned by extract_max.
Note: The code for manipulating the input as well as output is given to you. You only have to write code for the incomplete functions.
Input
——-
A set of lines, each lines containing a character representing the operation and its inputs.
The operation can be one of the following:
These are Introduction to programming in C Assignment 7 Answers
- A <id> <val>
Add an node with id and val to the list, at the start of the list. - C <id> <val>
Change the value of the element with id to val.
If an element with this id is not found, do nothing. - S <id>
If an element with the id is in the list print the id and the value and a newline.
Else, print the id and -1 and a newline. - E
End of input, exit from the program
These are Introduction to programming in C Assignment 7 Answers
Output
———
The output of search queries.
Sample input
—————–
A 1 10
A 2 20
S 2
S 3
C 2 30
S 2
E
Sample Output
———————
2 20
3 -1
2 30
Explanation
—————
The list is initially empty
Add an element 1 with value 10
list : (1,10) -> NULL
Add an element 2 with value 20
list : (2,20) -> (1,10) -> NULL
Search for element with id 2, print
2 20
Search for element with id 3, print
3 -1
Change priority of 2 to 30
list : (2,30) -> (1,10) -> NULL
Search for element with id 2, print
2 30
End of input
These are Introduction to programming in C Assignment 7 Answers
Solution
#include <stdio.h>
#include <stdlib.h>
struct node
{
int id;
int value;
struct node *next;
};
struct node *create_node(int id, int val)
{
struct node *e = (struct node *) malloc(sizeof(struct node));
e->id = id;
e->value = val;
e->next = NULL;
return e;
}
struct node *append(struct node *list, struct node *e)
{
if (list == NULL)
{
return e;
}
else if (e->value > list->value)
{
e->next = list;
return e;
}
else
{
list->next = append(list->next, e);
return list;
}
}
struct node *search(struct node *list, int id)
{
struct node *current = list;
while (current != NULL)
{
if (current->id == id)
{
return current;
}
current = current->next;
}
return NULL;
}
void change_value(struct node *list, int id, int val)
{
struct node *e = search(list, id);
if (e != NULL)
{
e->value = val;
}
}
int find_value(struct node *list, int id)
{
struct node *e = search(list, id);
if (e != NULL)
return e->value;
return -1;
}
int main()
{
char op;
int id, val;
struct node *list = NULL;
int flag = 1;
do
{
scanf("%c ", &op);
switch (op)
{
case 'A':
scanf("%d %d", &id, &val);
list = append(list, create_node(id, val));
break;
case 'S':
scanf("%d", &id);
printf("%d %d\n", id, find_value(list, id));
break;
case 'C':
scanf("%d %d", &id, &val);
change_value(list, id, val);
break;
case 'E':
flag = 0;
}
}
while (flag == 1);
return 0;
}
These are Introduction to programming in C Assignment 7 Answers
More Weeks of Introduction to programming in C: Click Here
More Nptel courses: https://progiez.com/nptel
These are Introduction to programming in C Assignment 7 Answers

This content is uploaded for study, general information, and reference purpose only.