Answer to Question #264680 in C for robin

Question #264680

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


1
Expert's answer
2021-11-11T17:33:03-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS