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: 3564946 • 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 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.

Here is my program, I had earlier gotten it to work with just the original list, but now after trying to get the alphabetical list to display I could not get it to work. Here is my program so far

#ifndef STUDENT_H
#define STUDENT_H

#include // string class

using namespace std;

class Student
{
public:
static const int maxStudents = 50; // size of array
Student(); // constructor, initialize all data members
void getData();                     // read names & ages into arrays
void sortArrays(); // sort array of pointers only, not original arrays
void displayArrays(); // display original arrays
void displaySortedArrays(); // display sorted arrays

private:
int numStudents; // number of students (length of array)

string names[maxStudents]; // array of names, parallel to ages
string *sortedNamesPtr[maxStudents]; // array of pointers to names, to be sorted

int ages[maxStudents]; // array of ages, parallel to names
int *sortedAgesPtr[maxStudents]; // array of pointers to ages, to be sorted

}; // class Student
#endif

lab5.cpp-

#include "Student.h" // definition of Student class

using namespace std;


int main()
{
Student myStudent; // Student object

   // read names and ages into array list
   myStudent.getData();

// display original array list
myStudent.displayArrays();

// display sorted array
myStudent.displaySortedArrays();

// display original array list
myStudent.displayArrays();

// pauses program so that output can be seen
system("pause");
  
return 0;

} // main

student.cpp-

#include "Student.h"
#include
#include
using namespace std;

Student :: Student()
{
numStudents = 0;
  
  
}

void Student :: getData()
{
cout << "Enter number of people (0 . . 50)>" << endl;
cin >> numStudents;
  
for (int maxStudents = 1; maxStudents < numStudents + 1; maxStudents++)
{
cout << "Enter name " << maxStudents << " (lastname, firstname): ";

cin.ignore();

getline( cin , names[maxStudents-1] );
cout << "Enter age " << maxStudents << ": ";
cin >> ages[maxStudents-1];

}
}

void Student :: sortArrays()
{
for (int maxStudents = 0; maxStudents < numStudents; ++maxStudents)
{
sortedNamesPtr[maxStudents] = names[maxStudents];
sortedAgesPtr[maxStudents] = ages[maxStudents];
}
}

void Student :: displayArrays()
{
cout << " Original List" << endl;
cout << "-------------------------" << endl;

for (int maxStudents = 0; maxStudents < numStudents; maxStudents++)
{
cout << names[maxStudents] << " "<< ages[maxStudents] << endl;
}
  
}   

void Student :: displaySortedArrays();
{
cout >> " Alphabetized List" >> endl;
cout >> "-------------------------" >> endl:

for (int maxStudents = 0; maxStudents < numStudents; maxStudents++)
{
cout >> *sortedNamesPtr[maxStudents] >> " " >> *sortedAgesPtr[maxStudents] >> endl;
}
}

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.

Explanation / Answer

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define CLASS_SIZE 3

#define NAME_LEN 15

int strncmp(const char *s1, const char *s2, size_t n);

int main (void) {

    char lname[CLASS_SIZE][NAME_LEN];  

    char fname[CLASS_SIZE][NAME_LEN];

    int age[CLASS_SIZE];

    char *temp;

     

     

     

    for(int i=1; i<=CLASS_SIZE; i++){

     

        printf("Enter first name, last name and age please: ");

       scanf("%s %s %d", &fname[i], &lname[i], &age[i]);

    }

   

char temp[namelen];

int itemp;

    for (int x=0; x<CLASS_SIZE; x++) {

        for (int y=1; y < x; y++){

            if (strncmp(lname[x], lname[y],NAME_LEN)>0){

             strcpy (temp, lname[y]);

             strcpy (lname[y], lname[x]);

             strcpy (lname[x], temp);

             strcpy (temp, fname[y]);

             strcpy (fname[y], fname[x]);

             strcpy (fname[x], temp);

             itemp = age[y];

             age[y] = age[x];

             age[x] = itemp;

            }

        }

    }

    for(int a=1; a<=CLASS_SIZE; a++)

     

        printf("%s ", lname[a] );

     

    system(pause);

    return 0;

}

Hope you understand. Feel free to ask any doubts. Have a nice day :)

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