Earlier in this course you created an application for your b

Earlier in this course, you created an application for your boss which displayed three scores from three different players. For your assignment, you are going to improve this code and add a capture and print function. Using the code from your previous assignment (see below code), make several improvements including:

Using two arrays (one for Name and one for Score).

Capture the Name and score for a total of 10 players.

Print the arrays in descending score.

Print the arrays in ascending alphabetical order by name.

Important Using the string sort function combine the two arrays into one then sort.

#include \"stdafx.h\"

#include <iostream>

#include <string>

using namespace std;

int main()

{

int scoreOne;

int scoreTwo;

int scoreThree;

int scoreHighest;

cout << \"Type in the score for player one: \";

cin >> scoreOne;

cout << \"Type in the score for player two: \";

cin >> scoreTwo;

cout << \"Type in the score for player three: \";

cin >> scoreThree;

if (scoreOne > scoreTwo) {

scoreHighest = scoreOne; }

else { scoreHighest = scoreTwo; }

if (scoreThree > scoreHighest) {

scoreHighest = scoreThree; }

cout << \"The highest score is \" << scoreHighest << endl;

system(\"pause\");

return 0;

}

Solution

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

//function to capture input from user
void capture(string name[10],int scores[10])
{
    for(int i=0;i<10;i++)
    {
        cout<<\"Enter name: \";
        cin>>name[i];
        cout<<\"Enter score: \";
        cin>>scores[i];
    }
}

//display names and scores
void print(string name[10],int scores[10])
{
    for(int i=0;i<10;i++)
    {
        cout<<endl;
        cout<<\"Name: \"<<name[i];
        cout<<\"\\tScore: \"<<scores[i];
    }
}

//function to sort scores in descdening order
void sortScores(string name[10],int scores[10])
{
     /* sorting begins ... */
     int tmpScore;
     string tmpName;
     //loop through array
    for (int i = 0; i < 10; ++i)
    {
        for (int j = i + 1; j < 10; ++j)
        {
            //find if there is any score less than the one in \"i\" position
            //if found exchange scores and names positions
            if (scores[i] < scores[j])
            {
                tmpScore = scores[i];
                scores[i] = scores[j];
                scores[j] = tmpScore;
              
                 tmpName = name[i];
                 name[i] = name[j];
                 name[j] = tmpName;
            }
        }
    }
}

//function to sort names in aescending order
void sortNames(string name[10],int scores[10])
{
     /* sorting begins ... */
     int tmpScore;
     string tmpName;
    for (int i = 0; i < 10; ++i)
    {
        for (int j = i + 1; j < 10; ++j)
        {
             //find if there is any name than the one in \"i\" position
            //if found exchange scores and names positions
            if (name[i] > name[j])
            {
                tmpScore = scores[i];
                scores[i] = scores[j];
                scores[j] = tmpScore;
              
                 tmpName = name[i];
                 name[i] = name[j];
                 name[j] = tmpName;
            }
        }
    }
}

//defintion to hold names and scores in a singlke entity
struct result
{
    string name;
    int score;
};

//function used by sort function
bool sort_by_name( const result & lhs, const result & rhs )
{
   return lhs.name < rhs.name;
}


//fucntion to combine arrays and then sort using string sort function
void combineArrays(string name[10],int scores[10],result combine[10])
{
    //map each name and score into one array
     for(int i = 0; i < 10; ++i)
    {
        combine[i].name = name[i];
        combine[i].score = scores[i];
    }
  
    //call OOTB sort funciton
    sort(combine,combine+9,sort_by_name);
  
    //dipslay the elemtns after sorting
    for(int i=0;i<10;i++)
    {
         cout<<endl;
        cout<<\"Name: \"<<combine[i].name;
        cout<<\"\\tScore: \"<<combine[i].score;
    }
    cout<<endl;
}

int main()
{
  
    //declar variables
   string name[10];
    int scores[10];
    //array to hold combine data
    result combine[10];
    //prompt user for input
    capture(name,scores);
  
    //sort array based on scores in desccending order
    cout<<\"\ ******Sorting scores in Descending order*******\";
sortScores(name,scores);
//print arrays
   print(name,scores);

   //sort array based on names in aescending order
   cout<<\"\ ******Sorting names in Aescending order*******\";
   sortNames(name,scores);
   //display aray elemnts
print(name,scores);

//combine array and print
cout<<\"\ ******Combine arraya and sorting them names in Aescending order*******\";
    combineArrays(name,scores,combine);
    return 0;
}

Sample Output:
Enter name: John
Enter score: 56
Enter name: Dave
Enter score: 21
Enter name: Bill
Enter score: 98
Enter name: Ottawa
Enter score: 6
Enter name: Scott
Enter score: 67
Enter name: Stefan
Enter score: 4
Enter name: Damon
Enter score: 99
Enter name: D
Enter score: 48
Enter name: Ezra
Enter score: 38
Enter name: Clare
Enter score: 76

******Sorting scores in Descending order*******

Name: Damon     Score: 99
Name: Bill      Score: 98
Name: Clare Score: 76  
Name: Scott     Score: 67
Name: John      Score: 56
Name: D Score: 48  
Name: Ezra      Score: 38
Name: Dave      Score: 21
Name: Ottawa    Score: 6
Name: Stefan    Score: 4                                                                                                        

******Sorting names in Aescending order*******

Name: Bill      Score: 98
Name: Clare     Score: 76
Name: D      Score: 48
Name: Damon Score: 99  
Name: Dave     Score: 21
Name: Ezra      Score: 38
Name: John      Score: 56
Name: Ottawa      Score: 6
Name: Scott    Score: 47
Name: Stefan    Score: 4

******Combine arraya and sorting them names in Aescending order*******

Name: Bill      Score: 98
Name: Clare     Score: 76
Name: D      Score: 48
Name: Damon Score: 99  
Name: Dave     Score: 21
Name: Ezra      Score: 38
Name: John      Score: 56
Name: Ottawa      Score: 6
Name: Scott    Score: 47
Name: Stefan    Score: 4

Earlier in this course, you created an application for your boss which displayed three scores from three different players. For your assignment, you are going t
Earlier in this course, you created an application for your boss which displayed three scores from three different players. For your assignment, you are going t
Earlier in this course, you created an application for your boss which displayed three scores from three different players. For your assignment, you are going t
Earlier in this course, you created an application for your boss which displayed three scores from three different players. For your assignment, you are going t
Earlier in this course, you created an application for your boss which displayed three scores from three different players. For your assignment, you are going t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site