Thread: stumped on array

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    stumped on array

    I'm a rookie when it comes to C++. I'm trying to write a program that prompts for seven scores, eliminate the high and low score (only one of them), and total the five remaining scores to give an overall score using an array. I'm stuck on what the code is to eliminate the high and low score. Here is what I've got so far:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    float score[7],overall;
    int i;
    
    overall=0.;
    
    for (i=0; i<=6; i++)
    {
    cout<<"Enter a score"<<i+1<<":";
    cin>>score[i];
    
    
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Some issues with your existing code sample:

    Code:
    #include <iostream>  // No .h extension
    #include <cstdlib>    // No .h extension, add the "c" in front
    using namespace std;  // Add this
    
    int main()
    {
        float score[7],overall;
        int i;
    
        overall=0.;
    
        for (i=0; i<7; i++)
        {
            cout << "Enter a score " << i+1 << ":";
            cin >> score[i ];
        } // Missing this?
    
        system("PAUSE");
        return 0;
    }
    You can do this many ways. One is that you can keep track of the highest and lowest values entered into the array as you are getting the values from the user. You can also wait till the end and find the lowest and highest elements. To do that you can search through the array, or sort the array and pick off the elements at the bounds of the array as the lowest and highest, or you can make use of the min_element and max_element templated STL functions defined in the <algorithm> header. Once you've got the highest and lowest scores, you then just sum up everything in all the positions and then subtract the highest and lowest values from this total.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    #include <algorithm>
    ...
    std::sort(&score[0],&score[7]);
    score[0] is lowest and score[6] is the highest
    I'll leave the rest for you to complete

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    overall=0.;
    -->
    Code:
     overall=0;

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm stuck on what the code is to eliminate the high and low score.
    1) After you read in all the values, you can loop through your array with a for-loop(or as mentioned you can do it while reading in the values).

    2) Create two variables just before your for-loop: one called 'high' and the other called 'low'. Initialize each one to score[0].

    3) In the for-loop, use an if-statement to compare each element of the array to 'low'. If the element of the array is lower, assign its value to 'low'. After looping through the whole array, you will have the lowest value stored in 'low'. Follow a similar procedure for 'high'.

    4) Also, create a variable called 'total' just before your for-loop and initialize it with 0. In the for-loop add each element of the array to total. When the loop ends, you will have the sum of all the elements in the array. You can then subtract 'high' and 'low' from the total to get the overall score.
    Last edited by 7stud; 05-11-2005 at 11:00 AM.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Ok...thanks for all your help. All your replies make perfect sense but being a rookie, I'm still doing something wrong. Any advice? Here is what I did:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    float score[7],maxscore, minscore, highest, lowest, sum;
    int i;
    
    for (i=0; i<=7; i++)
    {
    cout<<"Enter a score"<<i+1<<":";
    cin>>score[i];
    }
    
    highest=0;
    for (i=1; i<7; i++)
    {
      if score[highest]< score[i])
         highest=i;
    
       maxscore=score[i];
    }
    
    lowest=0;
    for (i=1; i>0; i++)
    {
      if score[lowest] > score[i])
         lowest=i;
    
       minscore=score[i];
    }
    
    sum = 0;
    for(i=0; i<7; i++)
    {
      sum=sum+score[i]-minscore-maxscore;
    }
    
    cout << "\n\n";
    
    cout<<"The overall score is: " << sum << "\n\n";
    
    
          system("PAUSE");
          return 0;
    }

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    maxscore=score[highest];
    minscore=score[lowest];

    for efficiency's sake, I'd also move them outside the for ;oops, but they will work inside them.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    you need to check your syntax.

    Code:
    if score[lowest] > score[i])
    should be...

    Code:
    if (score[lowest] > score[i])

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    if score[highest]< score[i])
         highest=i;
    Besides the syntax error, you've gotten the loop counter i, the elements of the array, and the variable 'highest' all mixed up. Your array should look like this:

    score[0] = 54.6
    score[1] = 78.8
    score[2] = 83.1
    ...
    ...
    score[6] = 73.1

    So, your array has seven variables: score[0], score[1], ..., score[6] that all have a value assigned to them.

    You also declared a variable called 'highest' to which you want to assign one or more array variables. To assign one of the array variable to highest, you would do this:

    highest = score[2];

    To examine each of your array variables, you want to use a for-loop with the loop counter 'i' to step through all the array variables. That means you will have the term:

    score[i]

    somewhere inside your loop. Then, as i counts upward you will be accessing the variables score[0], score[1], score[2], etc. You want to compare those variables to 'highest', and then assign one of those variables to highest.
    Last edited by 7stud; 05-12-2005 at 03:01 AM.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Ok...the program works now and I didn't get any compile errors. The only thing that happens now is the overall score isn't adding up right.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    float score[7],maxscore, minscore, sum;
    int i, highest, lowest;
    
    for (i=0; i<7; i++)
    {
    cout<<"Enter a score"<<i+1<<":";
    cin>>score[i];
    }
    
    highest=0;
    for (i=1; i<7; i++)
    {
      if (score[highest]< score[i])
      {
        highest=i;
        maxscore=score[i];
       }
    }
    
    lowest=0;
    for (i=1; i<7; i++)
    {
      if (score[lowest] > score[i])
        {
         lowest=i;
         minscore=score[i];
        }
    }
    
    sum = 0;
    for(i=0; i<7; i++)
    {
      sum=sum+score[i];
    }
    
    
    sum=sum -(maxscore+minscore);
    
    
    cout << "\n\n";
    
    cout<<"The overall score is: " << sum << "\n\n";
    
    
          system("PAUSE");
          return 0;
    }

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can do some things to eliminate some of the loops. The sum (before the max and min elements are eliminated) can be calculated during the first loop. Also, the finding of the min and max elements themselves can be done in a single loop. You can also eliminate some of the variables, you don't need to keep track of both the indicies and the min/max values at those indicies (for example):

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
        float score[7],maxscore, minscore, sum;
        int i;
    
        sum = 0;
        for (i=0; i<7; i++)
        {
            cout << "Enter a score" << i+1 << ":";
            cin >> score[i ];
            sum += score[i ];
        }
    
        maxscore = minscore = score[0];
        for (i=1; i<7; i++)
        {
            if( score[i ] < minscore )
                minscore = score[i ];
            if( score[i ] > maxscore )
                maxscore = score[i ];
        }
    
        sum -= (maxscore + minscore);
    
        cout << "\n\n";
    
        cout<<"The overall score is: " << sum << "\n\n";
    
        system("PAUSE");
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Thank you...much better. Thanks everyone for the assistance.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    hk_mp5kpdw,

    Presumably you figured no one else responding to the thread knew how to write the program so you would enlighten everyone by posting your code? Now that you did, I'm puzzled why you think you need two for-loops?

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    One to get input one to sort high-low.
    Woop?

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    One to get input one to sort high-low.
    Why not one loop to get input, one loop to get high, one loop to get low, and one loop for the heck of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM