0% found this document useful (0 votes)
103 views

Name of The Course: Data Structures (DS) Assignment - 3: 1602-19-733-080 M. Meghana 19-11-2020

The document describes an assignment to create a program that: 1. Sorts teams participating in a paper presentation contest by their total marks across three criteria (content, presentation, communication skills), using quicksort. 2. If teams have the same total marks, the team that registered first will be ranked higher. 3. The program accepts input details as described and displays the evaluation list ordered by decreasing total marks.

Uploaded by

080Meghana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Name of The Course: Data Structures (DS) Assignment - 3: 1602-19-733-080 M. Meghana 19-11-2020

The document describes an assignment to create a program that: 1. Sorts teams participating in a paper presentation contest by their total marks across three criteria (content, presentation, communication skills), using quicksort. 2. If teams have the same total marks, the team that registered first will be ranked higher. 3. The program accepts input details as described and displays the evaluation list ordered by decreasing total marks.

Uploaded by

080Meghana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Name of the Course: Data structures (DS)

Assignment – 3
1602-19-733-080
M. Meghana
19-11-2020

SET I:

1. You have been hired by the government for their latest project. The country
consists of N cities and M roads. Roads are directional. There may be more
than one roads between some cities. The cities are numbered from 1 to N.You are
given Q queries, each query has two integers U and V. Your task is to print
the shortest path between city U and city V. If it is not possible to go from
city U to city V, then print -1.
InputFirst line consists of three integers N, M and Q.
Next M lines each consists of three integers u, v, and w denoting a bidirectional road
between city u and city v with length w.
Then Q lines follow, each line having two integers U and V.
Output
Print Q lines of output.
For each query print the length of the shortest path between U and V
Notes
1 ≤ N ≤ 200 1 ≤ M, Q, ≤ N × N
Sample Input 0
575
515
321
453
435
533
454
545
12
13
44
51
32
Sample Output 0
-1
-1
0
5
1
Program:
#include<stdio.h>
#include <conio.h>

#define INF 9999


void main()
{
int i,j,k;
int N,M,Q,u,v,w,U,V;
scanf("%d %d %d",&N,&M,&Q);
int a[N+1][N+1],arr[N+1][N+1];
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
a[i][j]=0;
}
}
for(i=1;i<=M;i++)
{
scanf("%d %d %d",&u,&v,&w);
if(w<a[u][v] && a[u][v]!=0)
a[u][v]=w;
else
a[u][v]=w;
}
for ( i = 1 ; i <= N ; i++ )
{
for ( j = 1; j <= N ; j++ )
{
if ( a[i][j] == 0 )
arr[i][j] = INF ;
else
arr[i][j] = a[i][j] ;
}
}
for ( k = 1 ; k <= N ; k++ )
{
for ( i = 1 ; i <= N ; i++ )
{
for ( j = 1 ; j <= N ; j++ )
{
if ( arr[i][j] > arr[i][k] + arr[k][j] )
arr[i][j] = arr[i][k] + arr[k][j];
}
}
}
for(i=0;i<Q;i++)
{
scanf("%d %d",&U,&V);
if(U==V)
printf("0\n");
else if(arr[U][V]!=INF)
printf("%d",arr[U][V]);
else
printf("-1\n");
}

}
Output:

2. Akshay is a new to programming and while learning the programming


language he came to know the following rules:- Each program may start with '{'
and end with '}'.
- Each program must contain only one main function. Main function must
start with '<' and end with '>'.
- A program may or may not contain user defined function(s). There is no limitation
on the number of user defined functions in the program. User defined function
must start with '(' and end with ')'.
- Loops are allowed only inside the functions (this function can be either main
function or user defined function(s)). Every loop must startwith '{' and end with '}'.
- User defined function(s) are not allowed to be defined inside main function or
other user defined function(s).
- Nested loops are allowed.
- Instructions can be anywhere inside the program.
- Number of instructions inside any user defined function must not be more
than 100.
If any of the above conditions is not satisfied, then the program will generate
compilation errors. Today Akshay has written a few programs,but he is
not sure about the correctness of the programs. Your task is to help him
to find out whether his program will compile without any errors or not.
Input Format:
First line starts with T, number of test cases. Each test case will contain a
single line L, where L is a program written by Akshay.
Output Format:
Print "No Compilation Errors" if there are no compilation errors, else print
"Compilation Errors".
Constraints:
1<=T<=100L is a text and can be composed of any of the characters {, }, (, ), <, >and
P, where P will represents the instruction. L, comprised of characters mentioned
above should be single spaced delimited. Number of characters in the text, |L|
< = 10000
Sample Input 0
3
{<>(P)}
{<{}>({}))
{({})}
Sample Output 0
No Compilation
ErrorsCompilation
ErrorsCompilation Errors
Program:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#define MAXSIZE 100
struct Stack {
int top;
int array[MAXSIZE];
} s;
void initialize() {
s.top = -1;
}
int isFull() {
if(s.top >= MAXSIZE-1)
return 1;
else
return 0;
}
int isEmpty() {
if(s.top == -1)
return 1;
else
return 0;
}
void push(int num) {
if (isFull())
printf("Stack is Full...\n");
else {
s.array[s.top + 1] = num;
s.top++;
}
}
int pop() {
if (isEmpty())
printf("Stack is Empty...\n");
else {
s.top = s.top - 1;
return s.array[s.top+1];
}
}
bool BracketsBalanced(char expr[])
{
initialize();
char x,i;
for (i = 0; i < strlen(expr); i++)
{
if (expr[i] == '(' || expr[i] == '<'
|| expr[i] == '{')
{
push(expr[i]);
continue;
}
if (isEmpty())
return false;
switch (expr[i]) {
case ')':
x = s.top;
pop();
if (x == '{' || x == '<')
return false;
break;
case '}':
x = s.top;
pop();
if (x == '(' || x == '<')
return false;
break;

case '>':
x = s.top;
pop();
if (x == '(' || x == '{')
return false;
break;
}
}
return (isEmpty());
}
int main()
{
int n;
scanf("%d",&n);
int i,count,countP=0,j,error=0;
for(i=0;i<n;i++)
{
count=0,error=0;
char str[20];
scanf("%s",str);
if(str[0]=='{')
{
if(str[strlen(str)-1]!='}')
{
printf("Compilation Errors\n");
error=1;
}
}
if(BracketsBalanced(str)==false)
{
printf("Compilation Errors\n");
error=1;
}
if(error==0)
{
for (j = 0; str[j] != '\0'; j++) {
if (str[j] == '<')
count++;
}
if (count == 0 || count>1)
{
printf("Compilation Errors\n");
error=1;
}
for (j = 0; str[j] != '\0'; j++) {
if (str[j] == 'P')
countP++;
}
if (count >100)
{
printf("Compilation Errors\n");
error=1;
}
int pos1,pos2,countO=0,countC=0,k;
for (j = 0; str[j] != '\0'; j++)
{
if(str[j]=='<')
pos1=j;
if(str[j]=='>')
pos2=j;
}
for(j=pos1+1;j<pos2;j++)
{
if(str[j]=='(')
{
printf("Compilation Errors\n");
error=1;
break;
}
}
for (j = 0; str[j] != '\0'; j++)
{
if(str[j]=='(')
{
countO++;
}
if(str[j]==')')
{
countC++;
if(countO==countC)
countO=countC=0;
else
{
printf("Compilation Errors\n");
error=1;
break;
}
}
}
for (j = 1; str[j+1] != '\0'; j++)
{
if(str[j]=='{')
{
for (k = j+1; str[k+1] != '\0'; k++)
{
if(str[k]=='}')
{
int l;
for(l=k+1;str[l+1]!='\0';l++)
{
if(str[l]=='(' || str[l]=='{')
{
printf("Compilation
Errors\n");
error=1;
break;
}
}
}
}
}
}
}
if(error==0)
printf("No compilation errors\n");
}
return 0;
}
Output:
3. Around 25 teams participated in a paper presentation contest. The Evaluators
have awarded marks to teams based on 3 criteria - depth of content,
presentation and communications skills. Each criteria allotted with 10 marks
each. Display the evaluation list in the decreasing order of their total marks
in all the 3 criteria. If the teams have same total mark then which team has
registered first will be placed first than the other one. Accept the input details as
described in the scenario and design a C program to display the evaluation
list. Use Quick Sort.
Program:
#include<stdio.h>
struct team
{
int id;
int total;
};
void swap(int *a, int *b)
{
int t=*a;
*a=*b;
*b=t;

}
int partition (struct team part[], int low, int high)
{
int pivot= part[high].total;
int i = (low - 1);
int j;
for (j = low; j <= high- 1; j++)
{

if (part[j].total < pivot)


{
i++;
swap(&part[i].id, &part[j].id);
swap(&part[i].total, &part[j].total);
}
}
swap(&part[i+1].id, &part[high].id);
swap(&part[i + 1].total, &part[high].total);
return (i + 1);
}
void quickSort(struct team arr[], int low, int high)
{
if (low < high)
{

int pi = partition(arr, low, high);


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
struct team part[25];
int d,p,c,id,n,i;
printf("25 teams participated and 3 criteria to determine winner\n");
n=25;
printf("Enter marks in each criteria for 25 participants\n");
for(i=0;i<25;i++)
{
part[i].id=i;
printf("participant with id=%d: ",i);
scanf("%d %d %d",&d,&p,&c);
part[i].total=d+p+c;
}
quickSort(part,0,24);
int max=part[24].total;
int winner=part[24].id;
for(i=0;i<24;i++)
{
if(part[i].total==max)
{
if(part[i].id<winner)
winner=part[i].id;
}
}
printf("The winner is the participant with id: %d and total: %d",winner,max);
}
Output:

4. Monk is standing at the door of his classroom. There are currently N


students in the class, i 'th student got Ai candies.There are still M more students
to come. At every instant, a student enters the class and wishes to be seated with
a student who has exactlythe same number of candies. For each student,
Monk shouts YES if such a student is found, NO otherwise. (Use BST)
Input:
First line contains an integer T . T test cases follow.First line of each case
contains two space-separated integers N and M.
Second line contains N + M space-separated integers, the candies of the
students.
Output:
For each test case, output Mnew line, Monk's answer to the M students.
Print "YES" (without the quotes) or "NO" (without the quotes)
pertaining to the Monk's answer.
Constraints:
1 ≤ T ≤ 101 ≤ N , M≤ 1050 ≤ Ai≤ 1012
Sample Input:
1
23
3 2 9 11 2
Sample Output:
NO
NO
YES
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
} *root=NULL;
struct node* newNode(int x)
{
struct node *t;
t=(struct node*)malloc(sizeof(struct node));
t->data=x;
t->left=t->right=NULL;
return t;
}
void insert(struct node *root,struct node *temp)
{
if(temp->data<root->data)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}

if(temp->data>root->data)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
struct node* search(struct node* root, int key){
if (root == NULL || root->data == key)
return root;
if (root->data < key)
return search(root->right, key);
return search(root->left, key);
}
int main()
{
struct node* temp;
int n,i,val,N,M;
scanf("%d",&n);
scanf("%d %d",&N,&M);
for(i=0;i<N;i++)
{
scanf("%d",&val);
temp=newNode(val);
if(root==NULL)
root=temp;
else
insert(root,temp);
}
for(i=0;i<M;i++)
{
scanf("%d",&val);
temp=newNode(val);
if(!search(root,val))
{
printf("NO\n");
temp=newNode(val);
insert(root,temp);
}
else
{
printf("YES\n");
}
}
}
Output:

You might also like