Question

Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

Hi this is C++, I'm really struggle with it please help me....

************************

Here is the original high score program,:

Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score.

The output from your program should look exactly like this:

Enter the name for score #1: Suzy
Enter the score for score #1: 600
Enter the name for score #2: Kim
Enter the score for score #2: 9900
Enter the name for score #3: Bob
Enter the score for score #3: 1012
Enter the name for score #4: Armando
Enter the score for score #4: 8000
Enter the name for score #5: Tim
Enter the score for score #5: 514

Top Scorers:
Kim: 9900
Armando: 8000
Bob: 1012
Suzy: 600
Tim: 514

Additional Requirements:

The data must be stored in two arrays: an array of strings named names, and an array of ints named scores. These arrays must be declared in the main function.

All of the user input should be done in a function named initializeArrays(). It should have the following signature:

void initializeArrays(string names[], int scores[], int size)

You must also write two more functions: one to sort both arrays, and one to display the final list of names and scores. They should have the following signatures.

void sortData(string names[], int scores[], int size)
void displayData(const string names[], const int scores[], int size)

The main function should be very short. It should just declare the arrays and then invoke these three functions.

****************************

Another additional requirements:

This high scores program should start out by asking the user how many scores will be entered. It should allocate appropriate arrays.

******************************

Here is the latest assignment requirement.

Rewrite your most recent high scores program so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of the original high scores program. Your new program should meet the following requirements:

The highscore struct should have two fields:

an int named score

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.)

The data should be stored in a single array, a dynamically allocated array of highscore structs.

Your program should use three functions that accept the array of highscore structs:

void initializeData(highscore scores[], int size)
void sortData(highscore scores[], int size)
void displayData(const highscore scores[], int size)

You may use any sort algorithm, but I would recommend using the selection sort from lesson 9.6. Don't use C++'s sort() function, but you can use the swap() function.

Note that when you swap your array elements, you can swap the entire struct. You don't need to swap the name and the score separately.

You may assume that the user enters names that are 23 characters or less. Getting this to work correctly if the user enters names that are too long -- that is, making it so that you put the first 23 characters in the name variable and ignore the remaining characters on the line -- is complicated. You can do this as an extra challenge if you want, but it's not required.

*************************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Requirements are not given clearly, so solution is prepared based on the understanding Solution Additional Requirements

// method to initialize the array void initializeArrays (string names [, int scores[l, int size) for (int li 0; li < size; li

cout < Top Scorers:<< endl; for (int li = 0; Ii < size; li++) cout << name s [11] << : << scores [11] << endl; Result: E

// main method to take names and scores from the user, sorting the scores using a selection sort int main() int ARRAY SIZE; c

for (int li 0; li <size 1 litt) highinax = 11; highvalscores [li]; if (scores [j > highval) highinax = ]; highvalscores [j if

Enter the score for score #4: 9999 Enter the name for score #5: Pranya Enter the score for score #5: 99999 Enter the name for

void sortData (highscore scores Il, int size) int largestIndex; highscore tempRecord: for (int cnt 0 cnt < size 1 cnt++) larg

Result How many scores will you enter? 5 Enter the name for score #1: Karl Enter the score for score #1: 123 Enter the name f

Copyable code:

Additional Requirements:

// include required header files

#include <iostream>

#include <string>

using namespace std;

// Declare a constant variable for array size

const int ARRAY_SIZE = 5;

// Declare the function protocol

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);

// main method to take names and scores from the user, sorting

// the scores using a selection sort

int main()

{

     // declare the array

    string names[ARRAY_SIZE];

    int scores[ARRAY_SIZE];

    // call the methods and perform the operation

    initializeArrays(names, scores, ARRAY_SIZE);

    sortData(names, scores, ARRAY_SIZE);

    displayData(names, scores, ARRAY_SIZE);

    system("pause");

    return 0;

}

// method to initialize the array

void initializeArrays(string names[], int scores[], int size)

{

    for (int li = 0; li < size; li++) {

        cout << "Enter the name for score #" << li + 1 << ": ";

        cin >> names[li];

        cout << "Enter the score for score #" << li + 1 << ": ";

        cin >> scores[li];

    }

}

// method to sort the scores

void sortData(string names[], int scores[], int size)

{

    int highval, highindx;

    string tmpstr;

    for (int li = 0; li < size - 1; li++) {

        highindx = li;

        highval = scores[li];

        for (int j = li + 1; j < size; j++)

            if (scores[j] > highval) {

                highindx = j;

                highval = scores[j];

            }

        if (highindx > li) {

            scores[highindx] = scores[li];

            scores[li] = highval;

            tmpstr = names[li];

            names[li] = names[highindx];

            names[highindx] = tmpstr;

        }

    }

}

// method to print the score

void displayData(const string names[], const int scores[], int size)

{

    cout << "Top Scorers:" << endl;

    for (int li = 0; li < size; li++)

        cout << names[li] << ": " << scores[li] << endl;

}

Another additional requirement:

// include required header files

#include <iostream>

#include <string>

using namespace std;

// Declare the function protocol

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);

// main method to take names and scores from the user, sorting the scores using a selection sort

int main()

{

     int ARRAY_SIZE;

     cout<<"How many scores will you enter?:";

     cin>>ARRAY_SIZE;

     // declare the array

    string *names=new string[ARRAY_SIZE];

    int *scores=new int[ARRAY_SIZE];

  

    // call the methods and perform the operation

    initializeArrays(names, scores, ARRAY_SIZE);

    sortData(names, scores, ARRAY_SIZE);

    displayData(names, scores, ARRAY_SIZE);

system("pause");

    return 0;

}

// method to initialize the array

void initializeArrays(string names[], int scores[], int size)

{

    for (int li = 0; li < size; li++) {

cout << "Enter the name for score #" << li + 1 << ": ";

          cin >> names[li];

          cout << "Enter the score for score #" << li + 1

<< ": ";

        cin >> scores[li];

    }

}

// method to sort the scores

void sortData(string names[], int scores[], int size)

{

    int highval, highindx;

    string tmpstr;

    for (int li = 0; li < size - 1; li++) {

        highindx = li;

        highval = scores[li];

        for (int j = li + 1; j < size; j++)

            if (scores[j] > highval) {

                highindx = j;

                highval = scores[j];

            }

        if (highindx > li) {

            scores[highindx] = scores[li];

            scores[li] = highval;

          tmpstr = names[li];

            names[li] = names[highindx];

            names[highindx] = tmpstr;

        }

    }

}

// method to print the score

void displayData(const string names[], const int scores[], int size)

{

    cout << "Top Scorers:" << endl;

    for (int li = 0; li < size; li++)

        cout << names[li] << ": " << scores[li] << endl;

}

Latest assignment requirement:

// include required header files

#include <iostream>

#include <iostream>

#include<string>

#include <algorithm>

using namespace std;

// structure highscore

struct highscore

{

    char names[24];

    int score;

};

// function protocol definition

void getArraySize(int& size);

void initializeData(highscore scores[], int size);

void sortData(highscore scores[], int size);

int indexOfLargest(const highscore scores[], int startingIndex, int size);

void displayData(const highscore scores[], int size);

// main method

int main()

{

     // create a dynamic array

    highscore* scores;

    int size;

    getArraySize(size);

    scores = new highscore[size];

     // call functions to perform initialozartion and sorting

    initializeData(scores, size);

    sortData(scores, size);

    displayData(scores, size);

    system("pause");

}

// method to get array size

void getArraySize(int& size)

{

    cout << "How many scores will you enter?: ";

    cin >> size;

}

// method to initialize the array

void initializeData(highscore scores[], int size)

{

    for(int indx = 0; indx < size; indx++)

    {

        cout << "Enter the name for score #" << (indx + 1)

<< ": ";

        cin >> scores[indx].names;

        cout << "Enter the score for score #" << (indx + 1)

   << ": ";

        cin >> scores[indx].score;

    }

    cout << endl;

}

// method to sort the data

void sortData(highscore scores[], int size)

{

    int largestIndex;

    highscore tempRecord;

    for (int cnt = 0; cnt < size - 1; cnt++)

    {

        largestIndex = indexOfLargest(scores, cnt, size);

        tempRecord = scores[largestIndex];

        scores[largestIndex] = scores[cnt];

        scores[cnt] = tempRecord;

    }

}

// method to find the index of largest value

int indexOfLargest(const highscore scores[], int stIndx, int size)

{

    int targetindx = stIndx;

    for (int cnt = stIndx + 1; cnt < size; cnt++)

    {

        if (scores[cnt].score > scores[targetindx].score)

        {

            targetindx = cnt;

        }

    }

    return targetindx;

}

// method to display the data

void displayData(const highscore scores[], int size)

{

    cout << "Top Scorers: " << endl;

    for(int indx = 0; indx < size; indx++)

    {

        cout << scores[indx].names << ": " << scores[indx].score

   << endl;

    }

}

Add a comment
Know the answer?
Add Answer to:
Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a...

    #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a list of up to 10 players and their high scores in the computer's memory. Use two arrays to manage the list. One array should store the players' names, and the other array should store the players' high scores. Use the index of the arrays to correlate the names with the scores. Your program should support the following features: a. Add a new player and...

  • C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is...

    C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is where the shapes are being stored. I'm assuming an array, but I'm not sure how sizing would work. Any help is appreciated, thanks! I have also attached the .h file we must use. Prompt: The goal of HW2 is to implement classes representing shapes. A given program will use this class to create shapes at arbitrary locations in the x-y plane and move them....

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT