Week 6 Assignment
Week 6 Assignment
Week 6 Assignment
Q1. Create a database of students using structures, where in each entry of the
database will have the following fields:
1. if a student 'A' has lower marks in physics than a student 'B', then A's data is
listed before B.
2. If A and B have the same physics marks and A has lower chemistry marks than
B, then A is listed before B.
3. If A and B have the same marks in physics and chemistry, and A has lower
marks in mathematics than B, then A is listed before B.
4. If all marks are equal and A's name precedes B's name in the dictionary order,
then A is listed before B.
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.
Sol. #include<stdio.h>
#include<stdlib.h>
struct student{
char name[20];
int phy,che,math;
};
typedef struct student student;
void print(student *s,int n){
int i;
for(i=0;i<n;i++){
printf("%s\t",s[i].name);
printf("%d\t",s[i].phy);
printf("%d\t",s[i].che);
printf("%d\n",s[i].math);
}
}
int main(){
int i,n;
scanf("%d",&n);
student *student_info= (student *)malloc(sizeof(student)*n);
for(i=0;i<n;i++){
scanf("%s",student_info[i].name);
scanf("%d",&student_info[i].phy);
scanf("%d",&student_info[i].che);
scanf("%d",&student_info[i].math);
}
qsort((void *)student_info,n,sizeof(student),comparator);
print(student_info,n);
return 0;
}
Q.2 A graph is abstractly a collection of vertices which are labelled by
non-negative integers, and a collection of edges. A graph called an
undirected graph if we talk of merely the presence of an edge between
vertices i and j, rather than its direction.
Input
Output
You have to output the list of nodes to which n has an edge, in the
order in which the edges were input, one line for each vertex.
Sol #include <stdio.h>
#include <stdlib.h>
struct node{
int vertex;
struct node *next;
};
struct list_entry{
struct node *head;
struct node *tail;
};
struct list_entry list_entries[34000];
void init_list_entries()
{
int i;
for ( i=0 ; i<100 ; i++ ){
list_entries[i].head =
list_entries[i].tail =
NULL;
}
}
struct node * make_node ( int data )
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp -> vertex = data;
temp -> next = NULL;
return temp;
}
void insert_at_end(int a, int b)
{
struct node *node1;
struct node *node2;
node1 = make_node (a);
if(list_entries[b].head == NULL){
list_entries[b].head = node1;
list_entries[b].tail = node1;
}else{
list_entries[b].tail->next = node1;
list_entries[b].tail = node1;
}
node2 = make_node(b);
if(list_entries[a].head == NULL){
list_entries[a].head = node2;
list_entries[a].tail = node2;
}else{
list_entries[a].tail->next = node2;
list_entries[a].tail = node2;
}
return;
}
void print_adjacent_vertices_of(int n)
{
struct node *current = list_entries[n].head;
while(current != NULL){
printf("%d\n",current->vertex);
current = current->next;
}
return;
}
int main()
{
int num_edges;
int a;
int b;
int n;
int i=0;
scanf("%d", &num_edges);
for ( i=0; i<num_edges ; i++) {
scanf ( "%d", & a);
scanf ( "%d", & b);
insert_at_end(a,b);
}
scanf("%d",&n);
print_adjacent_vertices_of(n);
}
Q.3 You are given a sequence of integers terminated with a -1. The -1 is
not part of the input sequence.
Next, you are given a positive number N.
You have to create a linked list with the input sequence of integers
as entries. You can use the following structure.
struct node{
int data;
struct node *next;
};
Now, you have to delete all but the last N elements from the linked
list, and print the resulting list. (i.e. The resulting list will
consist of only the last N elements from the list.)
If N is longer than the length of the linked list, you must print -1.
Sample Input
------------
3 4 5 6 -1
1
Sample Output
-------------
6
You are given a sequence of integers terminated with a -1. The -1 is
not part of the input sequence.
You have to create a linked list with the input sequence of integers
as entries. You can use the following structure.
struct node{
int data;
struct node *next;
};
Now, you have to delete all but the last N elements from the linked
list, and print the resulting list. (i.e. The resulting list will
consist of only the last N elements from the list.)
If N is longer than the length of the linked list, you must print -1.
Sample Input
------------
3 4 5 6 -1
1
Sample Output
-------------
6
int main()
{
int number;
int pruned_length=0;
struct node *head;
head = create_list ( head );
scanf("%d", &pruned_length);
if(pruned_length > list_length){
printf ( "-1\n" );
return 0;
}else{
head=delete_first(list_length-pruned_length, head);
print_list(head);
}
return 0;
}