Amlan has listed down the details of the students randomly,ie their names and physics marks.Now,he wants to rearrange the students details in alphabetical order keeping the constraint of the physics marks in descending order.provide a solution in c to him.
#include <stdio.h>
void display(char array[][30],int Marks[], int n)
{
for(int i=0; i<n; i++){
printf("\n%10d%10s, \tMarks = %d ", i+1,array[i],Marks[i]);
}
printf("\n");
}
int main()
{
char Names[][30] = { "ABC", "LMN", "JKL","XYZ","QAZ","QWERT" };
int n = sizeof(Names) / sizeof(Names[0]);
int Marks[] = {78, 65, 87, 94, 60, 57};
int i,j;
char temp[30];
int u;
printf("Before Sorting: "); display(Names,Marks,n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1-i; j++)
{
if(strcmp(Names[j], Names[j+1]) > 0)
{
strcpy(temp, Names[j]);
u = Marks[j];
strcpy(Names[j], Names[j+1]);
strcpy(Names[j+1], temp);
Marks[j] = Marks[j+1];
Marks[j+1] = u;
}
}
}
printf("\nAfter Sorrting Alphabatically: ");
display(Names,Marks,n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1-i; j++)
{
if(Marks[j]<Marks[j+1])
{
u = Marks[j];
Marks[j] = Marks[j+1];
Marks[j+1] = u;
strcpy(temp, Names[j]);
strcpy(Names[j], Names[j+1]);
strcpy(Names[j+1], temp);
}
}
}
printf("\nAfter Sorrting using Marks: ");
display(Names,Marks,n);
return 0;
}
Comments
Leave a comment