Question

Write a program that stores lists of names (the last name first) and ages in parallel...

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

Homework Answers

Answer #1

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 ===>

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
11. First and Last Design a program that asks the user for a series of names...
11. First and Last Design a program that asks the user for a series of names (in no particular order). After the final person's name has been entered, the program should display the name that is first alphabetically and the name that is last alphabetically. For example, if the user enters the names Kristin, Joel, Adam, Beth, Zeb, and Chris, the program would display Adam and Zeb. I need help with creating a python code for this using a sentinel...
1. Create a PHP program containing an multi dimensional array of all the first and last...
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order. 2. Split the array from #1 into two arrays; one containing first names, the other containing last names.
DATA STRUCTURE /C++ Write a program that allows the user to enter the last names offive...
DATA STRUCTURE /C++ Write a program that allows the user to enter the last names offive candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follows: Candidate Johnson Miller Duffy Robinson Sam Total Votes Received 5000 4000 6000...
Write a program that allows the user to enter the last names of five candidates in...
Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follows: Candidate Johnson Miller Duffy Robinson Sam Votes Received 5000 4000 6000 2500 1800 19300...
Create a program that allows the user to input a list of first names into one...
Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of emial addresses where the address is of the following form: [email protected] Declare FirstName[100] as String Declare LastName[100] as String Declare email as String Declare K as Integer Declare index as Integer Write "Enter first and last name." Write...
PLEASE USE IDLE PYTHON: Question (Arrays/Lists) A teacher uses 2   arrays (or Lists) to keep track...
PLEASE USE IDLE PYTHON: Question (Arrays/Lists) A teacher uses 2   arrays (or Lists) to keep track of his students. One is used to store student names and the other stores his grade (0-100). Write a program to create these two arrays (or lists) that are originally empty, and do the following using a menu: 1. Add a new student and his/her grade. 2. Print the name and grade of all current students, one student per line. 3. Display the number...
Write a complete Java program which prompts the user of the program to input his/her first...
Write a complete Java program which prompts the user of the program to input his/her first name, then prompts the user for the middle initial and finally prompts the user for the last name. As indicated, there are three prompts to the user. The program should output the user’s name with the first name first, followed by the middle initial, and the last name last, all on one line (with appropriate spacing). If you could also pinpoint exactly where to...
Write a program in Python to declare two empty lists one is for name and one...
Write a program in Python to declare two empty lists one is for name and one for salaries. Within the for loop ask for employees name and their salaries and append them into the list accordingly. Find out the total salary using accumulation concept within the for loop. Output : Both of the lists with their items and the total salary.
Today we are practicing our ABC's. Write a program that will accept three separate names as...
Today we are practicing our ABC's. Write a program that will accept three separate names as inputs (ex: "billy"). Then display the names in alphabetical order. The names will always use all lowercase letters. For example: given the input "lauren" "rodger" "bob" The output should be "bob" "lauren" "rodger" Note: Think carefully about the logic needed to solve this problem and remember there are only three names that need to be sorted. P.S. The computer language we are using is...
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other The first letter A and the last letter O