Thread: Help with Program Assignment

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Unhappy Help with Program Assignment

    The following is code from a homework assignment using arrays. I am having trouble with function FindHighScore. Any help would be helpful. The format below is required, so just need help on figuring out how to get highest score... Thanks




    // THIS PROGRAM FINDS THE HIGHEST SCORE
    // OF 20 SCORES. A FILE ON THE "A"
    // DRIVE IS USED TO PROVIDE INPUT
    // (SCORES) TO THE ARRAY.
    #include <iostream.h>
    #include <fstream.h>

    // FUNCTION PROTOTYPES
    //
    // FUNCTION PROTOTYPE FOR USER COMMENTS
    void usercomments();
    //
    // FUNCTION PROTOTYPE TO FIND HIGHEST SCORE
    short FindHighScore(short [], short);
    //
    void main()
    {
    //DECLARE AND INITIALIZE VARIABLE
    short number = 0;

    // DECLARE AND INITIALIZE NUMERIC ARRAY
    // THE ARRAY NAME IS scores AND IT CONTAINS 20 NUMBERS
    // THE DATA TYPE IS SHORT.
    // INITIALIZE ALL ELEMENTS TO BE ZERO
    short scores[20];
    //
    // CALL usercomment FUNCTION
    usercomments();
    //
    // OPEN FILE FOR INPUT
    ifstream inFile;
    inFile.open("a:scores8.txt", ios::in);

    // VERIFY FILE WAS OPENED SUCCESSFULLY
    if (!inFile.fail())
    {
    // ENTER SCORES INTO THE ARRAY
    // USE A FOR LOOP
    for (int x=0; x<=19; x++)

    // INPUT THE NUMBERS FROM THE FILE TO THE ARRAY
    inFile >> scores[x];
    x = x + 1;
    //
    // CLOSE THE FILE
    inFile.close();
    //
    // CALL FindHighScores FUNCTION
    number = FindHighScore(scores, 20);
    //
    // OUTPUT HIGHEST SCORE
    cout << "\n" << "The highest score is: " << number << "\n\n";
    }
    else
    cout << "Error opening file." << endl;
    // END IF
    //
    cout << "\n\n\nProgram has terminated.\n\n\n\n\n";
    } // END OF MAIN

    // PROGRAMMER-DEFINED FUNCTION DEFINITIONS
    //
    // FindHighScore FUNCTION
    short FindHighScore(short nums[], short)
    {
    // THIS FUNCTION FINDS THE HIGHEST SCORE
    // IN THE ARRAY
    short high = 0; // COUNTER
    short number = 0;
    for ( int x = 0; x > number; x++)
    {
    if(nums[x] > number)
    high = high + 1;
    }
    //
    return number;
    } // END OF FindHighScore FUNCTION
    //
    // Usercomments FUNCTION
    void usercomments()
    {
    cout << "This program finds the highest score"
    << " of 20 scores. A file on the A-drive\n";
    cout << "is used to provide input (scores) to the array.\n\n";
    } // END OF usercomment FUNCTION

  2. #2
    Unregistered
    Guest
    You want to pass in the address of the array of scores and the number of scores

    ex:

    Code:
    short FindHighest(short score[], short numscores)
    {
        short high = 0;
    
        for (int i=0; i<numscores; ++i)
            if (score[i] > high) high = score[i];
    
        return high;
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    THANK YOU

    THANK YOU SO MUCH>>>> YOU ARE A LIFE SAVER>>>THANK YOU AGAIN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a C program assignment. Please?
    By Loopah in forum C Programming
    Replies: 3
    Last Post: 11-16-2005, 08:37 PM
  2. Help with Programming Assignment
    By JHaney in forum C++ Programming
    Replies: 18
    Last Post: 11-08-2005, 03:45 AM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM