Thread: Drop lowest score?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Drop lowest score?

    What code can i use to drop the lowest score and average it out?
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    const int SIZE = 5, LENGTH = 25, SCORES = 4;
    
    void getNames(char [][LENGTH]);
    void getScores(char [][LENGTH], char [], double [][SCORES]);
    void display(char [][LENGTH], char [], double [][SCORES]);
    
    int main()
    {
    char students[SIZE][LENGTH];
    char grades[SIZE];
    double score[SIZE][SCORES];
    
    getNames(students);
    getScores(students,grades,score);
    display(students,grades,score);
    system("pause"); 
    return 0;
    }
    
    void getNames(char students[][LENGTH])
    {
    for(int i = 0; i < SIZE; i++)
    {
    cout << "Please enter student " << (i+1) << " name: ";
    cin.getline(students[i],LENGTH);
    }
    }
    
    void getScores(char students[][LENGTH], char grade[], double score[][SCORES])
    {
    int avg = 0;
    
    for(int i = 0; i < SIZE; i++)
    {
    
    for(int j = 0; j < SCORES; j++)
    {
    cout << "Please enter " << students[i] << " grade ";
    cout << (j+1) << ": ";
    cin >> score[i][j];
    avg += score[i][j];
    }
    
    avg = avg / SCORES;
    
    if(avg > 89)
    grade[i] = 'A';
    else if(avg > 79)
    grade[i] = 'B';
    else if(avg > 69)
    grade[i] = 'C';
    else if(avg > 59)
    grade[i] = 'D';
    else
    grade[i] = 'F';
    }
    
    }
    
    void display(char s[][LENGTH], char g[], double sc[][SCORES])
    {
    int avg = 0;
    
    for(int i = 0; i < SIZE; i++)
    {
    cout << s[i] << endl;
    
    for(int j = 0; j < SCORES; j++)
    {
    cout << sc[i][j] << endl;
    avg += sc[i][j];
    }
    
    cout << g[i] << endl;
    }
    }

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    avg = sum(all grades)/numberofgrades

    Your teacher should have taught you an if statement by now. If not it looks like this
    Code:
    if(condition)
    {
      dosomething();
    }
    So you see if the score is lowest using operators

    < (less than)
    > (greater than)
    == (equal to)
    <= (lessthan or equal to)
    >= (greater than or equal to)

    Those are your tools you know you got this.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use std::sort() to sort from lowest to highest, then use std::accumulate() on all the scores except the first one, then divide by the number of scores - 1.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Use std::sort() to sort from lowest to highest, then use std::accumulate() on all the scores except the first one, then divide by the number of scores - 1.
    std::sort would be overkill. It would suffice to use std::min_element to find the lowest score, then swap it with the last score, and then use std::accumulate on all the scores except the last one, then divide by the number of scores - 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    std::sort would be overkill. It would suffice to use std::min_element to find the lowest score, then swap it with the last score, and then use std::accumulate on all the scores except the last one, then divide by the number of scores - 1.
    Yeah, that would definitely be better.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. Replies: 22
    Last Post: 11-13-2009, 05:51 PM
  3. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  4. Lowest Score Drop - C++
    By getName(C-Dub) in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2008, 07:02 PM
  5. Lowest Score Drop
    By naya22 in forum C++ Programming
    Replies: 16
    Last Post: 04-29-2007, 12:48 AM