Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3629793 • Letter: W

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 two sample programs from pages 442 to 448 in the textbook. 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.

Here is the code I have so far:***Please Help*** I would need to set-up the three files. Would I need to separate this progam?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRSIZ 30
#define MAXAPP 50

int alpha_first(char *list[], int min_sub, int max_sub);
void select_sort_str(char *list[],int*numlist[], int n);


void main(void)
{
char applicants[MAXAPP][STRSIZ];
int ages [MAXAPP];
char *alpha[MAXAPP];
int *numeric[MAXAPP];
int num_app,i,j;
char one_char;

/* Gets applicant list */
printf("Enter number of people (0 . . %d) > ", MAXAPP);
scanf("%d", &num_app);

for (i = 0; i < num_app; ++i)

{
j=0;
printf(" Enter name %d (Lastname, firstname): ", i + 1);
fflush(stdin);
while((one_char=(char)getchar())!=' ')
applicants[i][j++]=one_char;
applicants[i][j]='';
printf("Enter age %d: ",i+1);
scanf("%d",&ages[i]);
}
for (i = 0; i < num_app; ++i)
{
alpha[i] = applicants[i];
numeric[i] =&ages[i];
}
select_sort_str(alpha, numeric, num_app);

printf(" %-30s ", "Original List");
printf ("------------- ");


for (i = 0; i < num_app; ++i)
printf("%-30s%d ", applicants[i],ages[i]);
printf(" %-30s ", "Alphabetized list");
printf ("------------- ");

for (i = 0; i < num_app; ++i)
printf("%-30s%d ", alpha[i],*numeric[i]);
printf(" %-30s ", "Original List");
printf ("------------- ");

for (i = 0; i < num_app; ++i)
printf("%-30s%d ", applicants[i],ages[i]);
fflush(stdin);
getchar();

}

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;

}

}

}

Explanation / Answer

Most of the C code can be used in C++. You just need to wrap your code into methods of the Student class and change some of your global variables into attributes. To compile, you compile the Student.cpp and Lab05.cpp files separately and then link them: g++ -c Student.cpp g++ -c Lab05.cpp g++ -o lab05 Lab05.o Student.o // ----------------------------------------------------------------------------- // -- The code below goes in Student.h // ----------------------------------------------------------------------------- #ifndef _STUDENT_H_ #define _STUDENT_H_ class Student { public: Student(); int alpha_first(); private: char names[MAXAPP][STRSIZ]; // name array int ages[MAXAPP]; // age array char* namePointers[MAXAPP]; // pointers to ages in sorted array int* agePointers[MAXAPP]; // pointers to the ages in sorted array }; #endif //_STUDENT_H_ // ----------------------------------------------------------------------------- // -- The code below goes in Student.cpp // ----------------------------------------------------------------------------- #include "Student.h" Student::Student() { // provide some initialization here } int Student::alpha_first() { // implementation of alpha_first } // ----------------------------------------------------------------------------- // -- The code below goes in Lab5.cpp // ----------------------------------------------------------------------------- #include "Student.h" int main() { // You can use the same code for reading input here Student s; // create a student object to use return 0; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote