Thread: How to get Highest Value from an Array?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Exclamation How to get Highest Value from an Array?

    Hi I was given an assignment to create a system that allows a user to:
    1. Enter 10 Game Titles
    2. Enter Graphics Score rating for each game title (1-5)
    3. Enter Replay Value Score rating for each game title (1-5)
    4. Find the highest graphic score
    5. Find the highest replay value score
    6. Find the highest combined score.
    7. Exit


    using a string arrays for game titles, and int arrays for graphic & replay scores.


    I've done Steps 1-3 already. But I am now having trouble with how to find the highest graphic score while displaying the name of the game with the highest graphic score. Also if multiple games have the same graphic score it must choose the last highest score in the array.

    This is what I have so far but the " highestGraphicScore" on displays the highest number not the game title with the highest score. Please help!


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    
    
            //String Array For Game Title
            const int SIZE = 5;   // Constant for array size
            string gameName[SIZE];
    
    
            for (int index = 0; index < SIZE; index++)
            {
                cout << "Enter Game "
                    << index + 1 << ": ";
                cin >> gameName[index];
            }
    
    
            cout << "The games you entered are: " << endl;
            for (int index = 0; index < SIZE; index++)
            {
                cout << "Game " << index + 1 << ": " << gameName[index] << endl;
    
    
            }
        
            //Int Array for Graphic Scores of ( 1-5)
        
            int graphicScore[SIZE];
    
    
            for (int index = 0; index < SIZE; index++)
            {
                cout << "Enter A Graphics Score (Between 1 and 5) for: " << gameName[index] << endl;
                cin >> graphicScore[index];
            }
    
    
            cout << "The Graphics scores you entered for each game are: \n" << endl;
            for (int index = 0; index < SIZE; index++)
            {
                cout << "Game " << index + 1 << ": " << graphicScore[index] << endl;
            }
    
    
            //Int Array for Replay Score of (1-5)
    
    
            int replayScore[SIZE];
    
    
            for (int index = 0; index < SIZE; index++)
            {
                cout << "Enter A Replay Score (Between 1 and 5) for: " << gameName[index] << endl;
                cin >> replayScore[index];
            }
    
    
            cout << "The Replay scores you entered for each game are: \n" << endl;
            for (int index = 0; index < SIZE; index++)
            {
                cout << "Game " << index + 1 << ": " << replayScore[index] << endl;
            }
        
            //Find the Highest Graphic Score
    
    
            int highestGraphicScore = 0;
    
    
            for (int index = 0; index < SIZE; index++)
            {
                if (graphicScore[index] > highestGraphicScore)
                    highestGraphicScore = graphicScore[index];
    
    
                cout << " The Highest Graphic Score is: " << highestGraphicScore << endl;
            }
    
    
            //Find the Highest Replay Score
    
    
            int highestReplayScore = 0;
    
    
            for (int index = 0; index < SIZE; index++)
            {
                if (replayScore[index] > highestReplayScore)
                    highestReplayScore = replayScore[index];
            }
    
    
            cout << "The Highest Replay Score is: " << highestReplayScore << endl;
    
    
        return 0;
    
    
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, just like you remember the highest score, you need to remember the title of that highest score game too.

    What do you mean by "combined score"? Is it the highest of the averages or the average of the highest?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Just save the index instead of the value. Note that you need to compare with >= since you want the last game with the highest score in case of a tie.
    Code:
            int highestGraphicScoreIndex = 0;
            for (int index = 1; index < SIZE; index++)
                if (graphicScore[index] >= graphicScore[highestGraphicScoreIndex])
                    highestGraphicScoreIndex = index;
            cout << " The game with the Highest Graphic Score is: "
                 << gameName[highestGraphicScoreIndex] << '\n';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Highest Number in Array
    By nlp412 in forum C++ Programming
    Replies: 1
    Last Post: 08-12-2014, 09:14 PM
  2. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  3. highest int
    By dhuan in forum C++ Programming
    Replies: 2
    Last Post: 08-31-2010, 12:01 AM
  4. Array (range lowest to highest)
    By flutegirl516 in forum C Programming
    Replies: 7
    Last Post: 02-18-2010, 10:20 AM
  5. Sorting an int array highest to lowest
    By switchback in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 03:30 AM

Tags for this Thread