Hello, I have some of the program working however am having difficulties getting
ID: 3564603 • Letter: H
Question
Hello,
I have some of the program working however am having difficulties getting the lists to display correctly. I'd like to see a working program so that I can see where I went wrong and apply it to my own program.
Problem Question:
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. The original arrays of names and ages should remain no changes. Therefore, you need to create an array of character pointers to store the addresses of the names in the name array initially. Apply the selection sort to this array of pointers so that the corresponding names are in alphabetical order. Use the pointer to arrays example from CIS 236 as reference. You should use another array of pointers to age array to make sure the age is corresponding to the correct name. Please see the additional program requirements at the bottom of this page.
Hint: Create a class named Student which contains name array and age array.
Problem Requirement:
a) You must create three separate files: Student.h, Student.cpp, and Lab5.cpp files.
b) Your program should run correctly with the same inputs and outputs as given in the sample run.
Problem Output:
Enter number of people <0 . . 50>
>6
Enter name 1 (last name, first name): Ryan, Elizabeth
Enter age 1: 62
Enter name 2 (last name, first name): Mcintyre, Osborne
Enter age 2: 84
Enter name 3 (last name, first name): DuMond, Kristin
Enter age 3: 18
Enter name 4(last name, first name): Larson, Lois
Enter age 4: 42
Enter name 5(last name, first name): Thorpe, Trinity
Enter age 5: 15
Enter name 6(last name, first name): Ruiz, Pedro
Enter age 6: 35
Original List
----------------------------------
Ryan, Elizabeth 62
McIntyre, Osborne 84
DuMond, Kristin 18
Larson, Lois 42
Thorpe, Trinity 15
Ruiz, Pedro 35
Alphabetized List
--------------------------
DuMond, Kristin 18
Larson, Lois 42
MicIntyre, Osborn 84
Ruiz, Pedro 35
Ryan, Elizabeth 62
Thorpe, Trinity 15
Original List
------------------------
Ryan, Elizabeth 62
McIntyre, Osborne 84
DuMond, Kristin 18
Larson, Lois 42
Thorpe, Trinity 15
Ruiz, Pedro 35
Any assistance is greatly appreciated.
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int size1= 30;
const int size2= 50;
int alpha_first(char *list[], int min_sub, int max_sub)
{
int first, i;
first = min_sub;
for (i = min_sub + 1; i <= max_sub; ++i)
{
if (strcmp(list[i], list[first]) <0)
first = i;
}
return (first);
}
void select_sort_str(char *list[], int *numList[], int n)
{
int fill,index_of_min;
char *temp;
int *numTemp;
for (fill = 0; fill < n - 1; ++fill)
{
index_of_min = alpha_first(list, fill, n-1);
if (index_of_min != fill)
{
temp = list[index_of_min];
list[index_of_min] = list[fill];
list[fill] = temp;
numTemp =numList[index_of_min];
numList[index_of_min]=numList[fill];
numList[fill]=numTemp;
}
}
}
int main()
{
char appli[size1][size2];
int age[size1];
char *alp[size1];
int *nume[size1],num,i;
cout<<"Enter number of applicants : ";
cin>>num;
for (i=0; i < num; ++i)
{
cout<<"Enter name <lastname>, <firstname>: "<< i + 1 <<" : ";
cin.ignore();
cin.getline(appli[i],size2);
cout<<"Enter age : ";
cin>>age[i];
}
for (i = 0; i< num; ++i)
{
alp[i] = appli[i];
nume[i] =&age[i];
}
select_sort_str(alp, nume, num);
cout<< "Application order"<<" "<< "Alphabetical order"<<endl;
for (i = 0; i < num; ++i)
{
cout << appli[i] <<" "<< age[i]<<" "<< alp[i] <<" "<< *nume[i]<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.