Write a program to take as input an array of N names, an array of N ids and an array of N salaries where the name, id, and salary of the 𝑖 th index belongs to the same person. Store them in a structure and sort them according to salaries (ascending) and then by ids (ascending). Print the person in the sorted order.
Sample input:
5
Mr. Kamal
Ms. Zara
Nitu Roy
Rakib Hasan
Samira Haque 32 28 29 25 30
165 163 165 178 165
Sample output :
Ms. Zara 163 28
Nitu Roy 165 29
Samira Haque 165 30
Mr. Kamal 165 32
Rakib Hasan 178 25
#include <stdio.h>
#include <string>
#include <stdlib.h>
int main(){
//array of N names, an array of N ids and an array of N salaries
char names[50][50];
char tempName[50];
int ids[50];
int salaries[50];
int N,i,c, d, tempId,tempSalary;
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 id %d: ",(i+1));
scanf("%d",&ids[i]);
printf("Enter salary %d: ",(i+1));
scanf("%d",&salaries[i]);
}
for (c = 0 ; c < N - 1; c++)
{
for (d = 0 ; d < N - c - 1; d++)
{
if (ids[d] > ids[d+1])
{
strcpy(tempName,names[d]);
strcpy(names[d],names[d+1]);
strcpy(names[d+1],tempName);
tempId = ids[d];
ids[d] = ids[d+1];
ids[d+1] = tempId;
tempSalary = salaries[d];
salaries[d] = salaries[d+1];
salaries[d+1] = tempSalary;
}else if (ids[d] == ids[d+1]){
if (salaries[d] > salaries[d+1])
{
strcpy(tempName,names[d]);
strcpy(names[d],names[d+1]);
strcpy(names[d+1],tempName);
tempId = ids[d];
ids[d] = ids[d+1];
ids[d+1] = tempId;
tempSalary = salaries[d];
salaries[d] = salaries[d+1];
salaries[d+1] = tempSalary;
}
}
}
}
printf("\n%-15s%-15s%-15s\n","Names","Ids","Salaries");
for(i=0;i<N;i++){
printf("%-15s%-15d%-15d\n",names[i],ids[i],salaries[i]);
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment