I need help on this C++ assignement. Need to rewrite the following program with
ID: 3678808 • Letter: I
Question
I need help on this C++ assignement. Need to rewrite the following program with the set of rules in the picture.Please make sure it exuctes properly. Thank you,.
Code to be edited:
#include
#include
#include
using namespace std;
// function prototypes
void initializeArrays(string names[], int scores[], int size);
void sortData(string names[], int scores[], int size);
void displayData(const string names[], const int scores[], int size);
int main()
{
int size;
string *names;
names = new string[size];
// string names[SIZE];
int *scores;
scores = new int[size];
// int scores[SIZE];
cout << "How many scores will you be entering?: ";
cin >> size;
initializeArrays(names, scores, size);
sortData(names, scores, size);
displayData(names, scores, size);
// Free dynamically allocated memory
delete[] scores;
delete[] names;
}
// This function takes user input and stores it in a names array and a scores array respectivly. The size integer limits the size of the names and scores array.
void initializeArrays(string names[], int scores[], int size)
{
string tempName;
for (int i = 0; i < size; i++){
cout << "Enter the name for score #" << i + 1 << ": ";
cin >> names[i];
cout << "Enter " << names[i] << "'s score: ";
cin >> scores[i];
}
}
// This function take the names array, scores array and the size integer as input. It sorts both arrays based on a dual selection sort algorithm. The arrays are sorted in descending order and maintain syncronization. Output is the two sorted arrays.
void sortData(string names[], int scores[], int size)
{
int startScan, maxIndex;
string tempId;
double maxValue;
for (startScan = 0; startScan < (size - 1); startScan++){
maxIndex = startScan;
maxValue = scores[startScan];
tempId = names[startScan];
for (int index = startScan + 1; index < size; index++){
if (scores[index] > maxValue){
maxValue = scores[index];
tempId = names[index];
maxIndex = index;
}
}
scores[maxIndex] = scores[startScan];
names[maxIndex] = names[startScan];
scores[startScan] = maxValue;
names[startScan] = tempId;
}
}
// This function takes the sorted names array, sorted scores array, and the size int as input. Using a simple for loop it displays the both arrays, in descending order, in an easy to read format.
void displayData(const string names[], const int scores[], int size)
{
cout << "Top Scorers:" << endl;
for (int i = 0; i < size; i++){
cout << setw(8) << names[i] << ":" << setw(8) << scores[i] << endl;
}
}
" The instuctions are in the picture below.
Assignment 8.1 [45 points] Rewrite your most recent high scores program so that each name/score pair is stored in a struct named highscore. Your program should meet the following requirements. 1. The highscore struct should have two fields: an int named score o and a char array named name. The char array should have 24 elements, making the maximum length of the name 23. (If you prefer to use a char pointer and a dynamically allocated array, that is fine as well. However, this may result in a number of complications, so be prepared for the challenge.) 2. The data should be stored in a single array, a dynamically allocated array of highscore structs. 3. Your program should use three functions that accept the array of highscore structs: vold initialleeData(highscore scores int aize) void sortbata(highscore scoren int aize) void diaplaybata (const highacore eorean, int size>Explanation / Answer
Please find the re-written code below :
#include<stdio.h>
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
struct highscore{
int score;
char name[24];
};
// function prototypes
void initializeArrays(highscore scores[], int size);
void sortData(highscore scores[], int size);
void displayData(const highscore scores[], int size);
int main()
{
int size;
// int scores[SIZE];
cout << "How many scores will you be entering?: ";
cin >> size;
highscore scores[size];
initializeArrays(scores, size);
sortData(scores, size);
displayData(scores, size);
// Free dynamically allocated memory
delete[] scores;
}
// This function takes user input and stores it in a names array and a scores array respectivly. The size integer limits the size of the names and scores array.
void initializeArrays(highscore scores[], int size)
{
string tempName;
for (int i = 0; i < size; i++){
cout << "Enter the name for score #" << i + 1 << ": ";
cin >> scores[i].name;
cout << "Enter " << scores[i].name << "'s score: ";
cin >> scores[i].score;
}
}
// This function take the names array, scores array and the size integer as input. It sorts both arrays based on a dual selection sort algorithm. The arrays are sorted in descending order and maintain syncronization. Output is the two sorted arrays.
void sortData(highscore scores[], int size)
{
int startScan, maxIndex;
char tempId[24];
double maxValue;
for (startScan = 0; startScan < (size - 1); startScan++){
maxIndex = startScan;
maxValue = scores[startScan].score;
strcpy (tempId,scores[startScan].name);
for (int index = startScan + 1; index < size; index++){
if (scores[index].score > maxValue){
maxValue = scores[index].score;
strcpy (tempId,scores[index].name);
maxIndex = index;
}
}
scores[maxIndex].score = scores[startScan].score;
strcpy (scores[maxIndex].name,scores[startScan].name);
scores[startScan].score = maxValue;
strcpy (scores[startScan].name,tempId);
}
}
// This function takes the sorted names array, sorted scores array, and the size int as input. Using a simple for loop it displays the both arrays, in descending order, in an easy to read format.
void displayData(const highscore scores[], int size)
{
cout << "Top Scorers:" << endl;
for (int i = 0; i < size; i++){
cout << setw(8) << scores[i].name << ":" << setw(8) << scores[i].score << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.