//How to handle student data using dynamic array in C .
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char *name;
int rol;
};
void enter_nam(char *,int n);
int search_rol(struct student *s,int n);
int search_nam(struct student *s,int n);
void sort_rol(struct student *s,int n);
main()
{
int n=0,loc;
char c,j,f;
struct student *s;
struct student* create(int *n);
void display(struct student *s,int n);
void sort_nam(struct student *s,int n);
void update_rol(struct student *s,int n);
void update_nam(struct student *s,int n);
void insert(struct student **s,int *n);
void remove_data(struct student *s,int *n);
f='\0';
printf("\nMain menu");
printf("\nPlease nake sure that you have first entered the data before performng any operation");
printf("\nPress C to Enter data ");
printf("\nPress G to Enter New data ");
printf("\nPress D to Display data ");
printf("\nPress L to linear search according to rollno ");
printf("\nPress N to linear search according to name ");
printf("\nPress S to Sort according to rollno ");
printf("\nPress T to Sort according to name ");
printf("\nPress U to update data by changing roll number of particular person ");
printf("\nPress V to update data by changing name of particular person ");
printf("\nPress I to Insert new record in sorted data ");
printf("\nPress R to Delete record from the sorted data");
printf("\nPress E to exit");
while(f!='e')
{
printf("\nEnter your choice :");
scanf(" %c",&c);
scanf("%c",&j);
switch(c)
{
case 'c':
case 'C':
case 'g':
case 'G':
s=create(&n);
break;
case 'd':
case 'D':
display(s,n);
break;
case 'e':
case 'E':
f='e';
break;
case 'i':
case 'I':
insert(&s,&n);
break;
case 'L':
case 'l':
loc=search_rol(s,n);
if(loc!=-1)
printf("\nStudent data is saved in the record and student name is \" %s \" ",(s+loc)->name);
else
printf("\nStudent data is not exits in the record ");
printf("\n");
break;
case 'N':
case 'n':
loc=search_nam(s,n);
if(loc!=-1)
printf("\nStudent data is saved in the record and student name is \" %s \" and student roll number is \" %d \" ",(s+loc)->name,(s+loc)->rol);
else
printf("\nStudent data is not exits in the record ");
printf("\n");
break;
case 'R':
case 'r':
remove_data(s,&n);
break;
case 'S':
case 's':
sort_rol(s,n);
break;
case 'T':
case 't':
sort_nam(s,n);
break;
case 'U':
case 'u':
update_rol(s,n);
break;
case 'V':
case 'v':
update_nam(s,n);
break;
default:
printf("\nYou have entered a wrong choice. Please make sure that entered choice is accurate");
}
}
}
struct student* create(int *n)
{
int i,j;
char temp[100],g;
struct student *s;
s=NULL;
printf("How many students data you want to save :");
scanf("%d",n);
s=(struct student *)malloc((*n)*sizeof(struct student));
for(i=0;i<(*n);i++)
{
printf("\nEnter the roll number of %d student :",i+1);
scanf("%d",&((s+i)->rol));
scanf("%c",&g);
printf("\nEnter the name of %d student :",i+1);
enter_nam(temp,100);
j=strlen(temp);
(s+i)->name=(char *)malloc(sizeof(char)*(j+1));
strcpy((s+i)->name,temp);
}
return s;
}
void enter_nam(char *temp,int n)
{
int j=0;
char c;
while(j<n)
{
scanf("%c",&c);
if(c=='\n')
{
temp[j]='\0';
break;
}
else
temp[j]=c;
j++;
}
}
void display(struct student *s, int n)
{
int i=0;
printf("Data is as :");
for(i=0;i<n;i++)
{
printf("\nStudent name is %s and roll number is %d",(s+i)->name,(s+i)->rol);
}
printf("\n");
}
int search_rol(struct student *s,int n)
{
int i=0;
int r,loc=-1;
printf("\nEnter the rollnumber of student :");
scanf("%d",&r);
for(i=0;i<n;i++)
{
if((s+i)->rol==r)
{
loc=i;
break;
}
}
return loc;
}
int search_nam(struct student *s,int n)
{
int i=0,loc=-1,l;
char temp[100],*name;
printf("\nEnter the name of student :");
enter_nam(temp,100);
l=strlen(temp);
name=(char *)malloc(sizeof(char)*(l+1));
strcpy(name,temp);
for(i=0;i<n;i++)
{
if(strcmp((s+i)->name,name)==0)
{
loc=i;
break;
}
}
return loc;
}
void sort_rol(struct student *s,int n)
{
struct student temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((s+i)->rol > (s+j)->rol)
{
temp=*(s+i);
*(s+i)=*(s+j);
*(s+j)=temp;
}
}
}
}
void sort_nam(struct student *s,int n)
{
struct student temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp((s+i)->name,(s+j)->name)>0)
{
temp=*(s+i);
*(s+i)=*(s+j);
*(s+j)=temp;
}
}
}
}
void update_rol(struct student *s,int n)
{
int loc,r;
loc=search_rol(s,n);
if(loc!=-1)
{
printf("\nEnter the new rollnumber for the student :");
scanf("%d",&r);
(s+loc)->rol=r;
}
else
printf("\nStudent data is not exits in the record ");
printf("\n");
}
void update_nam(struct student *s,int n)
{
int loc,l;
char temp[100];
loc=search_nam(s,n);
if(loc!=-1)
{
printf("\nEnter the new name for the student :");
enter_nam(temp,100);
l=strlen(temp);
(s+loc)->name=(char *)realloc((s+loc)->name,sizeof(char)*(l+1));
strcpy((s+loc)->name,temp);
}
else
printf("\nStudent data is not exits in the record ");
printf("\n");
}
void insert(struct student **s,int *n)
{
struct student *q;
int i,r,l,j,m,f;
char temp[100],g;
m=*n+1;
f='n';
q=(struct student *)realloc(*s,(m)*sizeof(struct student));
*s=NULL;
if(q!=NULL)
{
sort_rol(q,*n);
printf("\nEnter the rollnumber of student :");
scanf("%d",&r);
scanf("%c",&g);
printf("\nEnter the name of student :");
enter_nam(temp,100);
*n=*n+1;
for(i=0;i<m-1;i++)
{
if(r<((q+i)->rol))
{
for(j=m-1;j>i;j--)
{
l=strlen((q+(j-1))->name);
(q+j)->name=(char *)realloc((q+j)->name,sizeof(char)*(l+1));
(q+j)->rol=(q+(j-1))->rol;
strcpy((q+j)->name,(q+(j-1))->name);
}
(q+i)->rol=r;
l=strlen(temp);
(q+i)->name=(char *)realloc((q+i)->name,sizeof(char)*(l+1));
strcpy((q+i)->name,temp);
f='o';
break;
}
}
if(f!='o')
{
(q+(m-1))->rol=r;
l=strlen(temp);
(q+(m-1))->name=(char *)malloc(sizeof(char)*(l+1));
strcpy((q+(m-1))->name,temp);
}
*s=q;
q=NULL;
}
else
{
printf("Compiler does not find a needed memory try again");
}
}
void remove_data(struct student *s,int *n)
{
int i,r,l,loc;
char *temp;
sort_rol(s,*n);
loc=search_rol(s,*n);
if(loc!=-1)
{
r=(s+loc)->rol;
l=strlen((s+loc)->name);
temp=(char *)malloc((l+1)*sizeof(char));
strcpy(temp,(s+loc)->name);
printf("\nStudent \"%s\" and roll number \"%d\" is removed from the list",temp,r);
for(i=loc;i<*n-1;i++)
{
l=strlen((s+i+1)->name);
(s+i)->name=(char *)realloc((s+i)->name,sizeof(char)*(l+1));
(s+i)->rol=(s+i+1)->rol;
strcpy((s+i)->name,(s+i+1)->name);
}
printf("\n");
*n=*n-1;
s=(struct student *)realloc(s,(*n)*sizeof(struct student));
}
else
{
printf("\nStudent data is not exits in the record ");
printf("\n");
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char *name;
int rol;
};
void enter_nam(char *,int n);
int search_rol(struct student *s,int n);
int search_nam(struct student *s,int n);
void sort_rol(struct student *s,int n);
main()
{
int n=0,loc;
char c,j,f;
struct student *s;
struct student* create(int *n);
void display(struct student *s,int n);
void sort_nam(struct student *s,int n);
void update_rol(struct student *s,int n);
void update_nam(struct student *s,int n);
void insert(struct student **s,int *n);
void remove_data(struct student *s,int *n);
f='\0';
printf("\nMain menu");
printf("\nPlease nake sure that you have first entered the data before performng any operation");
printf("\nPress C to Enter data ");
printf("\nPress G to Enter New data ");
printf("\nPress D to Display data ");
printf("\nPress L to linear search according to rollno ");
printf("\nPress N to linear search according to name ");
printf("\nPress S to Sort according to rollno ");
printf("\nPress T to Sort according to name ");
printf("\nPress U to update data by changing roll number of particular person ");
printf("\nPress V to update data by changing name of particular person ");
printf("\nPress I to Insert new record in sorted data ");
printf("\nPress R to Delete record from the sorted data");
printf("\nPress E to exit");
while(f!='e')
{
printf("\nEnter your choice :");
scanf(" %c",&c);
scanf("%c",&j);
switch(c)
{
case 'c':
case 'C':
case 'g':
case 'G':
s=create(&n);
break;
case 'd':
case 'D':
display(s,n);
break;
case 'e':
case 'E':
f='e';
break;
case 'i':
case 'I':
insert(&s,&n);
break;
case 'L':
case 'l':
loc=search_rol(s,n);
if(loc!=-1)
printf("\nStudent data is saved in the record and student name is \" %s \" ",(s+loc)->name);
else
printf("\nStudent data is not exits in the record ");
printf("\n");
break;
case 'N':
case 'n':
loc=search_nam(s,n);
if(loc!=-1)
printf("\nStudent data is saved in the record and student name is \" %s \" and student roll number is \" %d \" ",(s+loc)->name,(s+loc)->rol);
else
printf("\nStudent data is not exits in the record ");
printf("\n");
break;
case 'R':
case 'r':
remove_data(s,&n);
break;
case 'S':
case 's':
sort_rol(s,n);
break;
case 'T':
case 't':
sort_nam(s,n);
break;
case 'U':
case 'u':
update_rol(s,n);
break;
case 'V':
case 'v':
update_nam(s,n);
break;
default:
printf("\nYou have entered a wrong choice. Please make sure that entered choice is accurate");
}
}
}
struct student* create(int *n)
{
int i,j;
char temp[100],g;
struct student *s;
s=NULL;
printf("How many students data you want to save :");
scanf("%d",n);
s=(struct student *)malloc((*n)*sizeof(struct student));
for(i=0;i<(*n);i++)
{
printf("\nEnter the roll number of %d student :",i+1);
scanf("%d",&((s+i)->rol));
scanf("%c",&g);
printf("\nEnter the name of %d student :",i+1);
enter_nam(temp,100);
j=strlen(temp);
(s+i)->name=(char *)malloc(sizeof(char)*(j+1));
strcpy((s+i)->name,temp);
}
return s;
}
void enter_nam(char *temp,int n)
{
int j=0;
char c;
while(j<n)
{
scanf("%c",&c);
if(c=='\n')
{
temp[j]='\0';
break;
}
else
temp[j]=c;
j++;
}
}
void display(struct student *s, int n)
{
int i=0;
printf("Data is as :");
for(i=0;i<n;i++)
{
printf("\nStudent name is %s and roll number is %d",(s+i)->name,(s+i)->rol);
}
printf("\n");
}
int search_rol(struct student *s,int n)
{
int i=0;
int r,loc=-1;
printf("\nEnter the rollnumber of student :");
scanf("%d",&r);
for(i=0;i<n;i++)
{
if((s+i)->rol==r)
{
loc=i;
break;
}
}
return loc;
}
int search_nam(struct student *s,int n)
{
int i=0,loc=-1,l;
char temp[100],*name;
printf("\nEnter the name of student :");
enter_nam(temp,100);
l=strlen(temp);
name=(char *)malloc(sizeof(char)*(l+1));
strcpy(name,temp);
for(i=0;i<n;i++)
{
if(strcmp((s+i)->name,name)==0)
{
loc=i;
break;
}
}
return loc;
}
void sort_rol(struct student *s,int n)
{
struct student temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((s+i)->rol > (s+j)->rol)
{
temp=*(s+i);
*(s+i)=*(s+j);
*(s+j)=temp;
}
}
}
}
void sort_nam(struct student *s,int n)
{
struct student temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp((s+i)->name,(s+j)->name)>0)
{
temp=*(s+i);
*(s+i)=*(s+j);
*(s+j)=temp;
}
}
}
}
void update_rol(struct student *s,int n)
{
int loc,r;
loc=search_rol(s,n);
if(loc!=-1)
{
printf("\nEnter the new rollnumber for the student :");
scanf("%d",&r);
(s+loc)->rol=r;
}
else
printf("\nStudent data is not exits in the record ");
printf("\n");
}
void update_nam(struct student *s,int n)
{
int loc,l;
char temp[100];
loc=search_nam(s,n);
if(loc!=-1)
{
printf("\nEnter the new name for the student :");
enter_nam(temp,100);
l=strlen(temp);
(s+loc)->name=(char *)realloc((s+loc)->name,sizeof(char)*(l+1));
strcpy((s+loc)->name,temp);
}
else
printf("\nStudent data is not exits in the record ");
printf("\n");
}
void insert(struct student **s,int *n)
{
struct student *q;
int i,r,l,j,m,f;
char temp[100],g;
m=*n+1;
f='n';
q=(struct student *)realloc(*s,(m)*sizeof(struct student));
*s=NULL;
if(q!=NULL)
{
sort_rol(q,*n);
printf("\nEnter the rollnumber of student :");
scanf("%d",&r);
scanf("%c",&g);
printf("\nEnter the name of student :");
enter_nam(temp,100);
*n=*n+1;
for(i=0;i<m-1;i++)
{
if(r<((q+i)->rol))
{
for(j=m-1;j>i;j--)
{
l=strlen((q+(j-1))->name);
(q+j)->name=(char *)realloc((q+j)->name,sizeof(char)*(l+1));
(q+j)->rol=(q+(j-1))->rol;
strcpy((q+j)->name,(q+(j-1))->name);
}
(q+i)->rol=r;
l=strlen(temp);
(q+i)->name=(char *)realloc((q+i)->name,sizeof(char)*(l+1));
strcpy((q+i)->name,temp);
f='o';
break;
}
}
if(f!='o')
{
(q+(m-1))->rol=r;
l=strlen(temp);
(q+(m-1))->name=(char *)malloc(sizeof(char)*(l+1));
strcpy((q+(m-1))->name,temp);
}
*s=q;
q=NULL;
}
else
{
printf("Compiler does not find a needed memory try again");
}
}
void remove_data(struct student *s,int *n)
{
int i,r,l,loc;
char *temp;
sort_rol(s,*n);
loc=search_rol(s,*n);
if(loc!=-1)
{
r=(s+loc)->rol;
l=strlen((s+loc)->name);
temp=(char *)malloc((l+1)*sizeof(char));
strcpy(temp,(s+loc)->name);
printf("\nStudent \"%s\" and roll number \"%d\" is removed from the list",temp,r);
for(i=loc;i<*n-1;i++)
{
l=strlen((s+i+1)->name);
(s+i)->name=(char *)realloc((s+i)->name,sizeof(char)*(l+1));
(s+i)->rol=(s+i+1)->rol;
strcpy((s+i)->name,(s+i+1)->name);
}
printf("\n");
*n=*n-1;
s=(struct student *)realloc(s,(*n)*sizeof(struct student));
}
else
{
printf("\nStudent data is not exits in the record ");
printf("\n");
}
}