Question: #include #include #include using namespace std; struct Performer { string name; double scores[3]; double final; }; void getandsort(Performer *performer, int &num); void structure(Performer *performer, int
#include
struct Performer { string name; double scores[3]; double final; };
void getandsort(Performer *performer, int &num); void structure(Performer *performer, int num);
int main(void) { Performer *performer = nullptr; int num = 3; getandsort(performer, num); structure(performer, num); delete [] performer; return 0; } // main
void getandsort(Performer *performer, int &num){ performer = new Performer[num]; double temp; //temporary holder for final double tempScores[5]; string tempString; cout << "read in the names;" << endl; for(int i = 0; i < num; i++){ cin >> performer[i].name; } cout << "read in the final scores(decimal)" << endl; for(int i = 0; i < num; i++){ cin >> performer[i].final; } cout << "read in the scores" << endl; for(int i = 0; i < num;i++){ cout << "reading in the " << num + 1 << "scores" << endl; for(int j = 0; j < 3; j++){ cin >> performer[i].scores[j]; } } for (int curr = 1; curr < num; curr++) { //make a copy of the current array temp = performer[curr].final; for (int j = 0; j < 3; j++) { tempScores[j] = performer[curr].scores[j]; } tempString = performer[curr].name; int walk = curr - 1; while (walk >= 0 && temp > performer[walk].final) { performer[walk + 1].final = performer[walk].final; for (int j = 0; j < 3; j++) { performer[walk + 1].scores[j] = performer[walk].scores[j]; } performer[walk + 1].name = performer[walk].name; walk--; } performer[walk + 1].final = temp; for (int j = 0; j < 3; j++) { performer[walk + 1].scores[j] = tempScores[j]; } performer[walk + 1].name = tempString; } for(int i = 0; i < num; i++){ //printing out the results cout << performer[i].final << " " << performer[i].name << " "; for(int j = 0; j < 3; j++){ cout << performer[i].scores[j] << " "; } cout << endl; } }
void structure(Performer *performer, int num){ for(int i = 0; i < num; i++){ //printing out the results cout << performer[i].final << " " << performer[i].name << " "; for(int j = 0; j < 3; j++){ cout << performer[i].scores[j] << " "; } cout << endl; } }
Right now, the code works for the first function, but breaks on the second function with a segmentation fault. How can I fix it so I can access and print out the structure values with the second function?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
