Develop a program that will read and store the details of a list of students in the format
Roll No. Name Marks obtained
............... ............... .....................
............... ............... .....................
............... ............... .....................
and produce the follwing output lists:
(a) Alphabetical list of names, roll numbers and marks obtained.
(b) List sorted on roll numbers.
(c) List sorted on marks (rank-wise list)
#include <stdio.h>
#include <string.h>
void main()
{
char Name[50][20],dummy[20];
int i,j,n,roll_no[50],marks[50],temp;
clrscr();
printf("Enter the Number of students:");
scanf("%d",&n);
printf("Enter Roll no. , Name and Marks of all the Students\n");
printf("Press Enter after Name and than enter marks\n");
for(i=1;i<=n;i++)
{
scanf("%d",&roll_no[i]);
gets(Name[i]);
scanf("%d",&marks[i]);
}
clrscr();
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(Name[i],Name[j])>0)
{
strcpy(dummy,Name[i]);
strcpy(Name[i],Name[j]);
strcpy(Name[j],dummy);
temp=roll_no[i];
roll_no[i]=roll_no[j];
roll_no[j]=temp;
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
}
}}
printf("List Sorted on Alphabetical Ordered Name\n");
printf("Roll no. Name Marks Obtained\n");
for(i=1;i<=n;i++)
printf("%5d %-16s %3d\n",roll_no[i],Name[i],marks[i]);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(roll_no[i]>roll_no[j])
{
temp=roll_no[i];
roll_no[i]=roll_no[j];
roll_no[j]=temp;
strcpy(dummy,Name[i]);
strcpy(Name[i],Name[j]);
strcpy(Name[j],dummy);
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
}
}}
printf("\n\nList Sorted on Roll number\n");
printf("Roll no. Name Marks Obtained\n");
for(i=1;i<=n;i++)
printf("%5d %-16s %3d\n",roll_no[i],Name[i],marks[i]);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(marks[i]<marks[j])
{
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
strcpy(dummy,Name[i]);
strcpy(Name[i],Name[j]);
strcpy(Name[j],dummy);
temp=roll_no[i];
roll_no[i]=roll_no[j];
roll_no[j]=temp;
}
}}
printf("\n\nList Sorted on marks(raked-wise list)\n");
printf("Roll no. Name Marks Obtained\n");
for(i=1;i<=n;i++)
printf("%5d %-16s %3d\n",roll_no[i],Name[i],marks[i]);
getch();
}
Roll No. Name Marks obtained
............... ............... .....................
............... ............... .....................
............... ............... .....................
and produce the follwing output lists:
(a) Alphabetical list of names, roll numbers and marks obtained.
(b) List sorted on roll numbers.
(c) List sorted on marks (rank-wise list)
#include <stdio.h>
#include <string.h>
void main()
{
char Name[50][20],dummy[20];
int i,j,n,roll_no[50],marks[50],temp;
clrscr();
printf("Enter the Number of students:");
scanf("%d",&n);
printf("Enter Roll no. , Name and Marks of all the Students\n");
printf("Press Enter after Name and than enter marks\n");
for(i=1;i<=n;i++)
{
scanf("%d",&roll_no[i]);
gets(Name[i]);
scanf("%d",&marks[i]);
}
clrscr();
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(Name[i],Name[j])>0)
{
strcpy(dummy,Name[i]);
strcpy(Name[i],Name[j]);
strcpy(Name[j],dummy);
temp=roll_no[i];
roll_no[i]=roll_no[j];
roll_no[j]=temp;
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
}
}}
printf("List Sorted on Alphabetical Ordered Name\n");
printf("Roll no. Name Marks Obtained\n");
for(i=1;i<=n;i++)
printf("%5d %-16s %3d\n",roll_no[i],Name[i],marks[i]);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(roll_no[i]>roll_no[j])
{
temp=roll_no[i];
roll_no[i]=roll_no[j];
roll_no[j]=temp;
strcpy(dummy,Name[i]);
strcpy(Name[i],Name[j]);
strcpy(Name[j],dummy);
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
}
}}
printf("\n\nList Sorted on Roll number\n");
printf("Roll no. Name Marks Obtained\n");
for(i=1;i<=n;i++)
printf("%5d %-16s %3d\n",roll_no[i],Name[i],marks[i]);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(marks[i]<marks[j])
{
temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
strcpy(dummy,Name[i]);
strcpy(Name[i],Name[j]);
strcpy(Name[j],dummy);
temp=roll_no[i];
roll_no[i]=roll_no[j];
roll_no[j]=temp;
}
}}
printf("\n\nList Sorted on marks(raked-wise list)\n");
printf("Roll no. Name Marks Obtained\n");
for(i=1;i<=n;i++)
printf("%5d %-16s %3d\n",roll_no[i],Name[i],marks[i]);
getch();
}
No comments:
Post a Comment