Write a program to take as input the name, height in cm and age of n employees, and
sorts it according to height in descending order. If two employees have the same height, then it sorts
according to age in descending order. Print the employees in the sorted order.
Sample input:
5
Mr. Kamal
165 32
Ms. Zara
163 28
Nitu Roy
165 28
Rakib Hasan
178 25
Samira Haque
165 28
Sample output:
Rakib Hasan 178 25
Mr. Kamal 165 32
Nitu Roy 165 28
Samira Haque 165 28
Ms. Zara 163 28
#include <stdio.h>
#include <string>
#include <stdlib.h>
int main(){
//array of N names, an array of N heights and an array of N ages
char names[50][50];
char tempName[50];
int heights[50];
int ages[50];
int N,i,c, d, tempHeight,tempAge;
printf("Enter N: ");
scanf("%d",&N);
for(i=0;i<N;i++){
fflush(stdin);
printf("Enter name %d: ",(i+1));
gets(names[i]);
printf("Enter heights %d: ",(i+1));
scanf("%d",&heights[i]);
printf("Enter age %d: ",(i+1));
scanf("%d",&ages[i]);
}
for (c = 0 ; c < N - 1; c++)
{
for (d = 0 ; d < N - c - 1; d++)
{
if (heights[d] < heights[d+1])
{
strcpy(tempName,names[d]);
strcpy(names[d],names[d+1]);
strcpy(names[d+1],tempName);
tempHeight = heights[d];
heights[d] = heights[d+1];
heights[d+1] = tempHeight;
tempAge = ages[d];
ages[d] = ages[d+1];
ages[d+1] = tempAge;
}else if (heights[d]== heights[d+1]){
if (ages[d] < ages[d+1])
{
strcpy(tempName,names[d]);
strcpy(names[d],names[d+1]);
strcpy(names[d+1],tempName);
tempHeight = heights[d];
heights[d] = heights[d+1];
heights[d+1] = tempHeight;
tempAge = ages[d];
ages[d] = ages[d+1];
ages[d+1] = tempAge;
}
}
}
}
printf("\n%-15s%-15s%-15s\n","Names","Height","Age");
for(i=0;i<N;i++){
printf("%-15s%-15d%-15d\n",names[i],heights[i],ages[i]);
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment