Average score

This is a discussion on Average score within the C++ Programming forums, part of the General Programming Boards category; I want to calculate the actual average score from these two input test files, one is called StudentsDay.txt and the ...

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    Average score

    I want to calculate the actual average score from these two input test files, one is called StudentsDay.txt and the other one is called StudentsEvening.txt
    StudentsDay.txt look like this:
    Code:
    s1012 Anne Bennet       45 77 62 18 79
    s1236 Tony Bridges       81 83 89 95 63
    s2341 Michael Butcher   72 83 48 26 75
    StudentsEvening.txt look like this:
    Code:
    s5454 Joe Sunshine      66 89 78 74 85
    s5466 Vanessa White   62 77 44 88 79
    s5553 Jim Sunder         66 74 48 53 95
    I have already got their individual average score from each of the student and now I only want to get the actual average from the 6 students average score. So this is what I meant:
    Code:
    Anne Bennet average is 56.20
    Tony Bridges average is 82.20
    Michael Butcher average is 60.80
    Joe Sunshine average is 78.40
    Vanessa While average is 70.00
    Jim Sunder average is 67.20
    The average of 6 students is 69.13
    Here's the code:
    Code:
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream inFileDay;
        ifstream inFileEvening;
        ofstream outFile;
    
        string studentdayId;
        string studenteveningId;
        string givenname, surname;
        int score1, score2, score3, score4, score5;
        int studentcounter;
        double AverageDay, AverageEvening;
        string grade;
        
        float maxscore;
        float minscore;
        float allaverage;
        char maxTime='D';
        char minTime='D';
        string maxGrade;
        string minGrade;
        string maxSID;
        string minSID;
        string maxName;
        string minName;
    
        maxscore=-1;
        minscore=101;
    
        inFileDay.open("studentsDay.txt");
        inFileEvening.open("studentsEvening.txt");
        outFile.open("report_regular.txt",std::ios::trunc);
    
        outFile.setf(ios::fixed, ios::floatfield);
        outFile.setf(ios::showpoint);
        outFile<<setprecision(2);
    
        outFile<<endl<<"+++++Section 1 Student List+++++"<<endl<<endl;
    
        while (!inFileDay.eof() )
        {
            inFileDay>>studentdayId;
            outFile<<studentdayId<<"  ";
            inFileDay>>givenname>>surname;
            outFile<<surname<<", ";
            outFile<<givenname<<" ";
            outFile<<"D ";
    
            inFileDay>>score1>>score2>>score3>>score4>>score5;
    
            AverageDay=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
    
            outFile<<AverageDay<<"  ";
            outFile.setf(ios::right);
    
            if(AverageDay>=85 && AverageDay<=100)
                grade="HD";
            else if(AverageDay>=75 && AverageDay<=85)
                grade="D ";
            else if(AverageDay>=65 && AverageDay<=75)
                grade="Cr";
            else if(AverageDay>=55 && AverageDay<=65)
                grade="P ";
            else
                grade="F ";
                
            outFile<<grade<<endl;
                
            if(maxscore<AverageDay)
            {
                maxscore=AverageDay;
                maxSID=studentdayId;
                maxName=givenname+" "+surname;
                maxGrade=grade;
            }
            if(minscore>AverageDay)
            {
                minscore=AverageDay;
                minSID=studentdayId;
                minName=givenname+" "+surname;
                minGrade=grade;
            }
    
            studentcounter++;
        }
    
        while (!inFileEvening.eof() )
        {
            inFileEvening>>studenteveningId;
            outFile<<studenteveningId<<"  ";
            inFileEvening>>givenname>>surname;
            outFile<<surname<<", ";
            outFile<<givenname<<" ";
            outFile<<"E ";
            inFileEvening>>score1>>score2>>score3>>score4>>score5;
    
            AverageEvening=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
            
            outFile<<setw(2)<<AverageEvening<<" ";
            outFile.setf(ios::right);
    
            if(AverageEvening>=85 && AverageEvening<=100)
                grade="HD";
            else if(AverageEvening>=75 && AverageEvening<=85)
                grade="D ";
            else if(AverageEvening>=65 && AverageEvening<=75)
                grade="Cr";
            else if(AverageEvening>=55 && AverageEvening<=65)
                grade="P ";
            else
                grade="F ";
                
            outFile<<grade<<endl;
                
            if(maxscore<AverageEvening)
            {
                maxscore=AverageEvening;
                maxSID=studentdayId;
                maxName=givenname+" "+surname;
                maxGrade=grade;
                maxTime='E';
            }
            if(minscore>AverageEvening)
            {
                minscore=AverageEvening;
                minSID=studentdayId;
                minName=givenname+" "+surname;
                minGrade=grade;
                minTime='E';
            }
    
            studentcounter++;
            allaverage = (AverageDay++ + AverageEvening++)/6;
        }
        
        
        
         
        outFile<<"\n+++++Section 2 The Maximum+++++\n\n"
            <<maxName<<" ("<<maxSID<<", "<<maxTime<<") is my best student, with "
            <<"a score of "<<maxscore<<" ("<<maxGrade<<")\n"<<minName<<" ("<<minSID
            <<", "<<minTime<<"), you need to drop this class... you only got a "
            <<minscore<<" ("<<minGrade<<')'<<std::flush;
        outFile<<"The average of 6 students is" << allaverage << endl;
        return 0;
    }
    The purple text is the part I try to get the actual results from the 6 students average but I still could not get it. How should it be done?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Post all the lines of code which you think calculate the average score for each individual student

  3. #3
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Is it this?

    Code:
    inFileDay>>score1>>score2>>score3>>score4>>score5;
    
            AverageDay=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
    
            outFile<<AverageDay<<"  ";
    
    inFileEvening>>score1>>score2>>score3>>score4>>score5;
    
            AverageEvening=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
            
            outFile<<setw(2)<<AverageEvening<<" ";

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > while (!inFileDay.eof() )
    Read the FAQ to find out why using eof to control a loop is bad.

    > while (!inFileDay.eof() )
    > ...
    > while (!inFileEvening.eof() )
    Both these blocks look exactly the same to me. Consider using a function which you pass a filename to, and a few references to where you want results stored.

    > allaverage = (AverageDay++ + AverageEvening++)/6;
    What did you want those ++ post-increments to do?
    At the moment, they're doing nothing at all (except making people wonder what are they for)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is it this?
    Yes, good. Now, figure out which variable has the average student score in it. Then, create another variable called TotalAverage above the first while loop, and everytime a student's average is computed inside a while loop, add the student's average to TotalAverage.

    Then, after both while loops are finished, TotalAverage will contain the sum of the 12 student averages. So, if you divide TotalAverage by 12, you will have the average grade for all the students.

  6. #6
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I'm sorry if I done it wrong but the variable that has the average student score in it are AverageDay and AverageEvening.

    Code:
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream inFileDay;
        ifstream inFileEvening;
        ofstream outFile;
    
        string studentdayId;
        string studenteveningId;
        string givenname, surname;
        int score1, score2, score3, score4, score5;
        int studentcounter;
        double AverageDay, AverageEvening;
        string grade;
        
        float maxscore;
        float minscore;
        float TotalAverage;
        char maxTime='D';
        char minTime='D';
        string maxGrade;
        string minGrade;
        string maxSID;
        string minSID;
        string maxName;
        string minName;
    
        maxscore=-1;
        minscore=101;
    
        inFileDay.open("studentsDay.txt");
        inFileEvening.open("studentsEvening.txt");
        outFile.open("report_regular.txt",std::ios::trunc);
    
        outFile.setf(ios::fixed, ios::floatfield);
        outFile.setf(ios::showpoint);
        outFile<<setprecision(2);
    
        outFile<<endl<<"+++++Section 1 Student List+++++"<<endl<<endl;
    
        while (!inFileDay.eof() )
        {
            inFileDay>>studentdayId;
            outFile<<studentdayId<<"  ";
            inFileDay>>givenname>>surname;
            outFile<<surname<<", ";
            outFile<<givenname<<" ";
            outFile<<"D ";
    
            inFileDay>>score1>>score2>>score3>>score4>>score5;
    
            AverageDay=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
    
            TotalAverage = AverageDay++
    
    
    
            outFile<<AverageDay<<"  ";
            outFile.setf(ios::right);
    
            if(AverageDay>=85 && AverageDay<=100)
                grade="HD";
            else if(AverageDay>=75 && AverageDay<=85)
                grade="D ";
            else if(AverageDay>=65 && AverageDay<=75)
                grade="Cr";
            else if(AverageDay>=55 && AverageDay<=65)
                grade="P ";
            else
                grade="F ";
                
            outFile<<grade<<endl;
                
            if(maxscore<AverageDay)
            {
                maxscore=AverageDay;
                maxSID=studentdayId;
                maxName=givenname+" "+surname;
                maxGrade=grade;
            }
            if(minscore>AverageDay)
            {
                minscore=AverageDay;
                minSID=studentdayId;
                minName=givenname+" "+surname;
                minGrade=grade;
            }
    
            studentcounter++;
        }
    
        while (!inFileEvening.eof() )
        {
            inFileEvening>>studenteveningId;
            outFile<<studenteveningId<<"  ";
            inFileEvening>>givenname>>surname;
            outFile<<surname<<", ";
            outFile<<givenname<<" ";
            outFile<<"E ";
            inFileEvening>>score1>>score2>>score3>>score4>>score5;
    
            AverageEvening=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
            
           TotalAverage = AverageEvening++
    
            outFile<<setw(2)<<AverageEvening<<" ";
            outFile.setf(ios::right);
    
            if(AverageEvening>=85 && AverageEvening<=100)
                grade="HD";
            else if(AverageEvening>=75 && AverageEvening<=85)
                grade="D ";
            else if(AverageEvening>=65 && AverageEvening<=75)
                grade="Cr";
            else if(AverageEvening>=55 && AverageEvening<=65)
                grade="P ";
            else
                grade="F ";
                
            outFile<<grade<<endl;
                
            if(maxscore<AverageEvening)
            {
                maxscore=AverageEvening;
                maxSID=studentdayId;
                maxName=givenname+" "+surname;
                maxGrade=grade;
                maxTime='E';
            }
            if(minscore>AverageEvening)
            {
                minscore=AverageEvening;
                minSID=studentdayId;
                minName=givenname+" "+surname;
                minGrade=grade;
                minTime='E';
            }
    
            studentcounter++;
            TotalAverage = (AverageDay+ AverageEvening)/6
        }
        
        
        
         
        outFile<<"\n+++++Section 2 The Maximum+++++\n\n"
            <<maxName<<" ("<<maxSID<<", "<<maxTime<<") is my best student, with "
            <<"a score of "<<maxscore<<" ("<<maxGrade<<")\n"<<minName<<" ("<<minSID
            <<", "<<minTime<<"), you need to drop this class... you only got a "
            <<minscore<<" ("<<minGrade<<')'<<std::flush;
        outFile<<"The average of 6 students is" << TotalAverage << endl;
        return 0;
    }
    I think I done wrong.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    float TotalAverage;
    When you create a variable like that, it will contain a junk value. The basic rule of programming is: "always initialize your variables". Initializing your variables means assign them a value when you declare them. Since TotalAverage is going to be used to sum up the individual student averages, you should initialize the variable with 0:

    float TotalAverage = 0;

    You should initialize all your other variables as well.
    ----

    TotalAverage = AverageDay++;
    That statement does two things:

    1) First, the variable TotalAverage is set equal to the value of the AverageDay variable.
    2) After that occurs, 1 is added to the variable AverageDay.

    What you want to do is add the value in the variable AverageDay to the value that is already stored in TotalAverage.
    ----

    Code:
        TotalAverage = (AverageDay+ AverageEvening)/6
     }<---- end of while loop
    Then, after both while loops are finished, TotalAverage will contain the sum of the 12 student averages. So, if you divide TotalAverage by 12, you will have the average grade for all the students.
    Last edited by 7stud; 04-30-2005 at 05:26 AM.

  8. #8
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    So I put float
    Code:
    TotalAverage = 0;
    Then change what?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You need to do this:
    What you want to do is add the value in the variable AverageDay to the value that is already stored in TotalAverage.
    Post how you would assign the value 60 to the TotalAverage variable.

  10. #10
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Code:
    float TotalAverage = 60;

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Ok, but pretend this is after you declared the variable already. Assuming you already have this line:

    float TotalAverage = 0;

    you would do this:

    TotalAverage = 60;

    Now post two other things:

    1) Write a line of code that will assign the value 50 to TotalAverage.
    2) Write a line of code that will add 50 to TotalAverage so that it is equal to 110
    Last edited by 7stud; 04-30-2005 at 06:40 AM.

  12. #12
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    TotalAverage = 50;
    TotalAverage + 60 = TotalAverage;

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    TotalAverage + 60 = TotalAverage;
    Pretty close. In computer programming, the variable that is being assigned the value goes on the left.

    You need to do the same thing in your program with TotalAverage. Inside the while loop add the student average to TotalAverage. Then, everytime the loop executes, the student average will get added to TotalAverage.

    Finally, after both while loops end, TotalAverage will have the sume of 12 student averages stored in it. So, after both while loops in your program, if you divide TotalAverage by 12, you will have the average student grade for all the students. You can then write that to the file.

  14. #14
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I only got 6 students 3 from day and 3 from evening. So is it like this:

    Code:
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream inFileDay;
        ifstream inFileEvening;
        ofstream outFile;
    
        string studentdayId;
        string studenteveningId;
        string givenname, surname;
        int score1, score2, score3, score4, score5;
        int studentcounter;
        double AverageDay, AverageEvening;
        string grade;
        
        float maxscore;
        float minscore;
        float TotalAverage = 0, TotalAverage1, TotalAverage2;
        char maxTime='D';
        char minTime='D';
        string maxGrade;
        string minGrade;
        string maxSID;
        string minSID;
        string maxName;
        string minName;
    
        maxscore=-1;
        minscore=101;
    
        inFileDay.open("studentsDay.txt");
        inFileEvening.open("studentsEvening.txt");
        outFile.open("report_regular.txt",std::ios::trunc);
    
        outFile.setf(ios::fixed, ios::floatfield);
        outFile.setf(ios::showpoint);
        outFile<<setprecision(2);
    
        outFile<<endl<<"+++++Section 1 Student List+++++"<<endl<<endl;
    
        while (!inFileDay.eof() )
        {
            inFileDay>>studentdayId;
            outFile<<studentdayId<<"  ";
            inFileDay>>givenname>>surname;
            outFile<<surname<<", ";
            outFile<<givenname<<" ";
            outFile<<"D ";
    
            inFileDay>>score1>>score2>>score3>>score4>>score5;
    
            AverageDay=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
    
            outFile<<AverageDay<<"  ";
            outFile.setf(ios::right);
    
            if(AverageDay>=85 && AverageDay<=100)
                grade="HD";
            else if(AverageDay>=75 && AverageDay<=85)
                grade="D ";
            else if(AverageDay>=65 && AverageDay<=75)
                grade="Cr";
            else if(AverageDay>=55 && AverageDay<=65)
                grade="P ";
            else
                grade="F ";
                
            outFile<<grade<<endl;
                
            if(maxscore<AverageDay)
            {
                maxscore=AverageDay;
                maxSID=studentdayId;
                maxName=givenname+" "+surname;
                maxGrade=grade;
            }
            if(minscore>AverageDay)
            {
                minscore=AverageDay;
                minSID=studentdayId;
                minName=givenname+" "+surname;
                minGrade=grade;
            }
    
            studentcounter++;
            
            TotalAverage + AverageDay = TotalAverage1;
        }
    
        while (!inFileEvening.eof() )
        {
            inFileEvening>>studenteveningId;
            outFile<<studenteveningId<<"  ";
            inFileEvening>>givenname>>surname;
            outFile<<surname<<", ";
            outFile<<givenname<<" ";
            outFile<<"E ";
            inFileEvening>>score1>>score2>>score3>>score4>>score5;
    
            AverageEvening=static_cast<double>(score1+score2+score3+score4+score5)/5.0;
            
            outFile<<setw(2)<<AverageEvening<<" ";
            outFile.setf(ios::right);
    
            if(AverageEvening>=85 && AverageEvening<=100)
                grade="HD";
            else if(AverageEvening>=75 && AverageEvening<=85)
                grade="D ";
            else if(AverageEvening>=65 && AverageEvening<=75)
                grade="Cr";
            else if(AverageEvening>=55 && AverageEvening<=65)
                grade="P ";
            else
                grade="F ";
                
            outFile<<grade<<endl;
                
            if(maxscore<AverageEvening)
            {
                maxscore=AverageEvening;
                maxSID=studentdayId;
                maxName=givenname+" "+surname;
                maxGrade=grade;
                maxTime='E';
            }
            if(minscore>AverageEvening)
            {
                minscore=AverageEvening;
                minSID=studentdayId;
                minName=givenname+" "+surname;
                minGrade=grade;
                minTime='E';
            }
    
            studentcounter++;
    
            TotalAverage + AverageEvening = TotalAverage2;
            }
        
        TotalAverage = (TotalAverage1 + TotalAverage2)/6;
        
         
        outFile<<"\n+++++Section 2 The Maximum+++++\n\n"
            <<maxName<<" ("<<maxSID<<", "<<maxTime<<") is my best student, with "
            <<"a score of "<<maxscore<<" ("<<maxGrade<<")\n"<<minName<<" ("<<minSID
            <<", "<<minTime<<"), you need to drop this class... you only got a "
            <<minscore<<" ("<<minGrade<<')'<<std::flush;
        outFile<<"The average of 6 students is" << TotalAverage << endl;
        return 0;
    }

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)
    TotalAverage + 60 = TotalAverage;

    Pretty close. In computer programming, the variable that is being assigned the value goes on the left.
    To assign a variable a value, you do this:

    var1 = 20;
    var2 = 30 + 20 - 10;
    var3 = var1 + var2;

    Notice how the variable is always on the left of the equals sign when you assign it a value, AND it is the only thing on the left.

    2) What values are stored in TotalAverage1 and TotalAverage2? Post the lines of code where they are assigned values. Remember for a variable to be assigned a value it has to be on the left side of the equals sign:

    Left side <-- = ---> right side
    Last edited by 7stud; 04-30-2005 at 07:46 AM.

Page 1 of 2 12 LastLast
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. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 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. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  5. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM

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