Drop lowest score?

This is a discussion on Drop lowest score? within the C++ Programming forums, part of the General Programming Boards category; What code can i use to drop the lowest score and average it out? Code: #include <iostream> #include <iomanip> using ...

  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
    404
    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
    20,954
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:24 PM
  2. Replies: 22
    Last Post: 11-13-2009, 04: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, 06:02 PM
  5. Lowest Score Drop
    By naya22 in forum C++ Programming
    Replies: 16
    Last Post: 04-29-2007, 12:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21