Write a program that stores lists of names (the last name first) and ages in parallel arrays and sorts the names into alphabetical order keeping the ages with the correct names. Sample output:
PROGRAM: C
C PROGRAM =====>
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, n,value;
printf("Enter the value of n \n");
scanf("%d", &n);
int age[n],tage[n],temp_age;
char name[n][15], tname[n][15], temp[8];
for (i = 0; i < n; i++)
{
printf("Enter %d names : ",i+1 );
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
printf("Enter %d age : ",i+1 );
scanf("%d", &value);
age[i] = value;
tage[i] = value;
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
temp_age = age[i];
age[i] = age[j];
age[j] = temp_age;
}
}
}
printf("\nBefore Sort : \n");
printf("\n----------------------------------------\n");
printf(" Names age\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%d\n", tname[i], tage[i]);
}
printf("------------------------------------------\n\n");
printf("After Sort : \n");
printf("\n----------------------------------------\n");
printf("Sorted Names age\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%d\n", name[i], age[i]);
}
printf("------------------------------------------\n");
return 0;
}
OUTPUT SCREENSHOT ===>
Get Answers For Free
Most questions answered within 1 hours.