Assignment 3
Name Anushka Shivade
U no UEC2023156
Problem Create a database of students using array of structures with attributes; roll
no, name, program,
course, marks obtained for different subjects with their total and average.
Implement the
following operations on the database:
a) Display the database in a tabular form.
b) Modify (should be able to modify each field of the database)
c) Append (add a new record to the existing database)
d) Search for a particular record from the database.
e) Sort the records in the database.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
int roll_no,DE ,SS,DSA,ECA;
char name[20];
float avg;
};
float avg(struct student s[] ,int i){
float avg= (s[i].DE +s[i].SS+s[i].DSA)/4;
return avg;
}
void display( struct student s[],int n){
printf("DATABASE OF STUDENT\n");
printf("Roll no\t Name\t\t\t\tDE\tSS\tDSA\tECA\tAvg\n");
for(int i=0; i<n;i++){
s[i].avg=avg(s,i);
printf("%d\t %s\t\t\t\t%d\t%d\t%d\t%d\t%0.2f\n", s[i].roll_no, s[i].name, s[i].DE, s[i].SS,
s[i].DSA,s[i].ECA,s[i].avg);
}
void struct_input(struct student s[], int intial ,int final){
for(int i=intial ;i<final;i++){
printf("Student %d", i+1);
printf("\nroll number: ");
scanf("%d",&s[i].roll_no);
printf("name of student: ");
scanf("%s", s[i].name);
printf("Marks:\n");
printf("DE , SS , DSA , ECA: ");
scanf("%d%d%d%d",&s[i].DE,&s[i].SS,&s[i].DSA,&s[i].ECA);
s[i].avg=avg(s, i);
printf("\n");
}
void sort(struct student s[],int n){
for(int i=0;i<n-1;i++){
short int didSwap= 0;
for(int j=0;j<n-i-1;j++){
if(s[j].roll_no>s[j+1].roll_no){
int temp;
temp=s[j+1].roll_no;
s[j+1].roll_no=s[j].roll_no;
s[j].roll_no=temp;
didSwap=1;
}
if(didSwap== 0) break;
}
}
int searching(struct student s[],int n ,char search[]){
int a ;
for(int i=0;i<n;i++){
a=strcmp(search , s[i].name);
if(a==0) {
return i; //store index of the element
}
}
return -1;
}
int search_rno(struct student s[],int n,int rno){
for(int i=0;i<n ;i++){
if(s[i].roll_no==rno){
return i;
}
}
return -1;
}
void modify(struct student s[] ,int mod ,int n){
int rno ,updated_no ,index ,sub ,mks;
switch(mod){
case 1://roll no
printf("Enter the roll no to be modified: " );
scanf("%d",&rno);
printf("Enter the updated roll no: ");
scanf("%d",&updated_no);
index=search_rno(s , n ,rno);
if(index==-1) printf("Data not found\n");
else s[index].roll_no=updated_no;
break;
case 2://name
printf("Enter the roll no : ");
scanf("%d",&rno);
index =search_rno(s,n ,rno);
if(index==-1) printf("Data not present\n");
else{
printf("Enter the updated name: ");
scanf("%s",s[index].name);
}
break;
case 3://Marks
printf("Enter the roll no : ");
scanf("%d",&rno);
index=search_rno(s,n ,rno);
if(index==-1) printf("Data not present\n");
else{
printf("\nWhich subject marks would you like to
update?\n1.DE\n2.SS\n3.DSA\n4.ECA\nEnter choice : ");
scanf("%d", &sub);
switch(sub){
case 1://DE
printf("Enter the updated DE marks: ");
scanf("%d",&mks);
s[index].DE=mks;
break;
printf("Marks updated\n");
case 2://SS
printf("Enter the updated SS marks: ");
scanf("%d",&mks);
s[index].SS=mks;
printf("Marks updated\n");
break;
case 3://DSA
printf("Enter the updated DE marks: ");
scanf("%d",&mks);
s[index].DSA=mks;
printf("Marks updated\n");
break;
case 4://ECA
printf("Enter the updated DE marks: ");
scanf("%d",&mks);
s[index].ECA=mks;
printf("Marks updated\n");
break;
}
}
}
int main(){
int n , mod;
printf("Enter the no of students: ");
scanf("%d",&n);
struct student s[n];
printf("\nENTER THE FOLLOWING DATA\n\n");
//Input in the array
struct_input(s,0,n);
printf("\n");
int ch;
do{
printf("\n***************MENU******************\n");
printf("1 Search\n2 Append\n3 Modify\n4 Sort\n5 Display\n6 exit\n");
printf("Enter the choice: ");
scanf("%d", &ch);
int m;
char c;
switch (ch){
case 1:// Search
printf("Enter the name to search: ");
char search[20];
scanf("%s", search);
int index= searching( s, n , search);
if(index==-1) printf("Not found");
else printf("Found at index %d",index);
break;
case 2://Append
printf("Number of entries to add: ");
scanf("%d",&m);
struct_input(s, n ,n+m);
n+=m;
break;
case 3:// modify
do{
printf("Enter the fields to change: ");
printf("\n1.Roll no\n2.Name\n3.Marks\n4.Dont modify\n");
printf("Enter the choice: ");
scanf("%d",&mod);
if(mod==4) break;
modify(s ,mod , n);
printf("Do you want to modify any other field?(Y/N) ");
scanf(" %c",&c);
}while(c =='Y'||c =='y');
break;
case 4: //Sort the record
sort( s,n);
break;
case 5 :display( s,n);
break;
case 6 : exit(0);
break;
default: printf("Enter valid input\n");
}
}while(ch!=6);
return 0;
Output:
PS D:\Anushka\college> cd "d:\Anushka\college\DSA\" ; if ($?) { gcc structure.c -o structure } ; if ($?) { .\structure }
Enter the no of students: 3
ENTER THE FOLLOWING DATA
Student 1
roll number: 1
name of student: anu
Marks:
DE , SS , DSA , ECA: 54 77 68 33
Student 2
roll number: 2
name of student: sara
Marks:
DE , SS , DSA , ECA: 77 64 58 33
Student 3
roll number: 8
name of student: apurv
Marks:
DE , SS , DSA , ECA: 87 14 96 33
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 1
Enter the name to search: apurv
Found at index 2
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 1
Enter the name to search: kaka
Not found
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 2
Number of entries to add: 1
Student 4
roll number: 4
name of student: akash
Marks:
DE , SS , DSA , ECA: 78 99 44 99
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 3
Enter the fields to change:
1.Roll no
2.Name
3.Marks
4.Dont modify
Enter the choice: 1
Enter the roll no to be modified: 2
Enter the updated roll no: 5
Do you want to modify any other field?(Y/N) y
Enter the fields to change:
1.Roll no
2.Name
3.Marks
4.Dont modify
Enter the choice: 2
Enter the roll no : 1
Enter the updated name: arav
Do you want to modify any other field?(Y/N) y
Enter the fields to change:
1.Roll no
2.Name
3.Marks
4.Dont modify
Enter the choice: 3
Enter the roll no : 1
Which subject marks would you like to update?
1.DE
2.SS
3.DSA
4.ECA
Enter choice : 1
Enter the updated DE marks: 98
Do you want to modify any other field?(Y/N) n
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 4
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 4
***************MENU******************
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 5
DATABASE OF STUDENT
Roll no Name DE SS DSA ECA Avg
1 arav 98 77 68 33 85.00
5 sara 77 64 58 33 79.00
8 apurv 87 14 96 33 98.00
***************MENU******************
1 Search
1 Search
2 Append
3 Modify
4 Sort
5 Display
6 exit
Enter the choice: 6
PS D:\Anushka\college\DSA>