Note : DO NOT USE POINTERS. I will award lifesaver only if you do not use pointe
ID: 3641299 • Letter: N
Question
Note : DO NOT USE POINTERS. I will award lifesaver only if you do not use pointers.
I want the code in C language. - dont provide me code for C++ .
Write a program that stores lists of names (the last name first) and agesin parallel arrays and sorts the names into alphabetical order keeping theages.
Sample Output:
Original list
- - - - - - - - - - - - - - - - - - -
Ryan, Elizabeth 62
McIntyre, Osborne 84
Alphabetized list
- - - - - - - - - - - - - - - - - - - -
McIntyre, Osborne 84
Ryan, Elizabeth 62
Explanation / Answer
Please rate...
It sorts throughout the name and ages that is if the last name is same then it sorts according to
the first names and if those are also same then it sorts according to the ages...
#include<stdio.h>
#include<string.h>
#define length 100
void main()
{
char fname[40][length];
char lname[40][length];
char t[40];
int age[length];
int n,i,te,j;
printf("Enter the number of people to be entered: ");
scanf("%d",&n);
printf("Enter the details of first person ");
for(i=0;i<n;i++)
{
fflush(stdin);
printf("Last name: ");
scanf("%s",lname[i]);
printf("First name: ");
fflush(stdin);
scanf("%s",fname[i]);
fflush(stdin);
printf("Age: ");
scanf("%d",&age[i]);
}
printf(" Original list");
printf(" ------------------------ ");
for(i=0;i<n;i++)
{
printf("%s",lname[i]);
printf(", %s",fname[i]);
printf(" %d ",age[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<(n-1);j++)
{
if(strcmp(lname[j],lname[j+1])>0)
{
strcpy(t,lname[j]);
strcpy(lname[j],lname[j+1]);
strcpy(lname[j+1],t);
strcpy(t,fname[j]);
strcpy(fname[j],fname[j+1]);
strcpy(fname[j+1],t);
te=age[j];
age[j]=age[j+1];
age[j+1]=te;
}
else if(strcmp(lname[j],lname[j+1])==0)
{
if(strcmp(fname[j],fname[j+1])>0)
{
strcpy(t,fname[j]);
strcpy(fname[j],fname[j+1]);
strcpy(fname[j+1],t);
te=age[j];
age[j]=age[j+1];
age[j+1]=te;
}
else if(strcmp(fname[j],fname[j+1])==0)
{
if(age[j]>age[j+1])
{
te=age[j];
age[j]=age[j+1];
age[j+1]=te;
}
}
}
}
}
printf(" Alphabetized list");
printf(" ----------------------- ");
for(i=0;i<n;i++)
{
printf("%s",lname[i]);
printf(", %s",fname[i]);
printf(" %d ",age[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.