Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,980 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,286 people online right now. Registration is fast and FREE... Join Now!




Finding the average

 
Reply to this topicStart new topic

Finding the average, with two variables

Novasplit
14 Dec, 2007 - 06:32 PM
Post #1

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 5


My Contributions
Hi, my assignment is pretty much done. Everything is working except finding the average. It keeps returning zero...
What I need to do is take the sum of all the weighted averages and divide by the number of students.
So the way I have it set up is that sumAverage will keep track of all the weighted averages calculated. and then divide by totalStudents. totalStudents will increase every time the while loop is run which is once per student.
I cout'd each of the variables and they are increasing, but for some reason they wont divide...

help me!

CODE
while (!ins.eof())
{  
    //calculates the weighted avg
    weightedAvg = (weight1 * score1) + (weight2 * score2) + (weight3 * score3);
    //finds the students grade
    if (weightedAvg >= 90)
        studentGrade = "A";
    if (weightedAvg >= 80 && weightedAvg < 90)
        studentGrade = "B";
    if (weightedAvg >= 70 && weightedAvg < 80)
        studentGrade = "C";
    if (weightedAvg >= 60 && weightedAvg < 70)
        studentGrade = "D";
    if (weightedAvg >= 50 && weightedAvg < 60)
        studentGrade = "F";
    if (weightedAvg < 50)
        studentGrade = "F";


    //determines the highest score
    if (weightedAvg <= lowScore)
        lowScore = weightedAvg;
    if (weightedAvg >= highScore)
        highScore = weightedAvg;
    //keeps track of the amount of students processed
    totalStudents++;
    
    sumAverage += weightedAvg;
//    averageWeight = sumAverage / totalStudents; // this has been blocked out -another way i was trying it

//writes the info to a txt called outFile.txt

    outs << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
        << score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
//also displays in the editor
    cout << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
        << score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
//restarts the loop
    ins >> weight1 >> weight2 >> weight3 >> score1 >> score2 >> score3 >> studentID;
    
}
    averageWeight = sumAverage / totalStudents; //trying to find average


This post has been edited by Novasplit: 15 Dec, 2007 - 05:05 PM
User is offlineProfile CardPM
+Quote Post

Moezzie
RE: Finding The Average
15 Dec, 2007 - 03:03 AM
Post #2

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 44


My Contributions
I cant seem to find the problem eather.
On the other hand, why do you keep this code in a loop? Are you reading the values from a file?
User is offlineProfile CardPM
+Quote Post

salman
RE: Finding The Average
15 Dec, 2007 - 03:12 AM
Post #3

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 19


My Contributions
see the changes

CODE
while (!ins.eof())
{  
    //calculates the weighted avg
    weightedAvg = (weight1 * score1) + (weight2 * score2) + (weight3 * score3);
    //finds the students grade
    if (weightedAvg >= 90)
        printf ("studentGrade = "A");
    if (weightedAvg >= 80 && weightedAvg < 90)
        printf (studentGrade = "B");
    if (weightedAvg >= 70 && weightedAvg < 80)
        printf (studentGrade = "C");
    if (weightedAvg >= 60 && weightedAvg < 70)
        printf(studentGrade = "D");
    if (weightedAvg >= 50 && weightedAvg < 60)
        printf (studentGrade = "F");
    if (weightedAvg < 50)
        printf (studentGrade = "F");


    //determines the highest score
    if (weightedAvg <= lowScore)
        lowScore = weightedAvg;
    if (weightedAvg >= highScore)
        highScore = weightedAvg;
    //keeps track of the amount of students processed
    totalStudents++;
    
    sumAverage += weightedAvg;
//    averageWeight = sumAverage / totalStudents; // this has been blocked out -another way i was trying it

//writes the info to a txt called outFile.txt

    outs << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
        << score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
//also displays in the editor
    cout << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
        << score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
//restarts the loop
    ins >> weight1 >> weight2 >> weight3 >> score1 >> score2 >> score3 >> studentID;
    
}
    averageWeight = sumAverage / totalStudents; //trying to find average


User is offlineProfile CardPM
+Quote Post

Novasplit
RE: Finding The Average
15 Dec, 2007 - 11:54 AM
Post #4

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 5


My Contributions
Moezzie: Yes Its reading all the data from a file. And inputing it into another so it will read from this file:
CODE

0.35 0.25 0.40
100 76 88 1014
0.35 0.25 0.40
100 100 25 9999
0.35 0.25 0.40
60 60 70 95 1337


and the output will look like this(in a file and the program window):

CODE

Student ID: 1014
Test Scores: 100 76 88
Weighted Average: 89.2
Grade: B

Student ID: 9999
Test Scores: 100 100 25
Weighted Average: 70
Grade: C

Student ID: 95
Test Scores: 60 60 70
Weighted Average: 64
Grade: D


salman: thank you for posting some code, but i cant seem to find your changes?


also I forgot to put in my posted code that I am trying to post the average outside of the loop. So where the } is the end of the loop. Then I would display the averageWeight in a cout.

CODE

}
    averageWeight = sumAverage / totalStudents;



Ill just post the full code:
CODE
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;

#define inFile "c:InFile.txt"
#define outFile "c:outFile.txt"
int main()
{

ifstream ins;
ofstream outs;

float weight1, weight2, weight3, score1, score2, score3, studentID, weightedAvg;
float lowScore = 100, highScore = 0, avgWeightAvg = 0, totalStudents = 0, sumAverage, averageWeight;
string studentGrade;

ins.open(inFile);

if (ins.fail())
{
    cerr << "Error opening " << inFile << endl;
    return EXIT_FAILURE;
}


outs.open(outFile);

if (outs.fail())
{
    cerr << "Error opening " << inFile << endl;
        return EXIT_FAILURE;
}





ins >> weight1 >> weight2 >> weight3 >> score1 >> score2 >> score3 >> studentID;

while (!ins.eof())
{
//////////////////////////////////////////////////////////    
    weightedAvg = (weight1 * score1) + (weight2 * score2) + (weight3 * score3);
//////////////////////////////////////////////////////////    
    if (weightedAvg >= 90)
        studentGrade = "A";
    if (weightedAvg >= 80 && weightedAvg < 90)
        studentGrade = "B";
    if (weightedAvg >= 70 && weightedAvg < 80)
        studentGrade = "C";
    if (weightedAvg >= 60 && weightedAvg < 70)
        studentGrade = "D";
    if (weightedAvg >= 50 && weightedAvg < 60)
        studentGrade = "F";
    if (weightedAvg < 50)
        studentGrade = "F";
//////////////////////////////////////////////////////////////


    if (weightedAvg <= lowScore)
        lowScore = weightedAvg;
    if (weightedAvg >= highScore)
        highScore = weightedAvg;
//////////////////////////////////////////////////////////////
    totalStudents++;
    
    sumAverage += weightedAvg;
//    averageWeight = sumAverage / totalStudents;

    ///////////////////////////////////////////////////////////////
    outs << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
        << score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
    cout << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
        << score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;

    ins >> weight1 >> weight2 >> weight3 >> score1 >> score2 >> score3 >> studentID;
    
}
    averageWeight = sumAverage / totalStudents;

cout << "Your results have been written to: " << outFile << endl;

cout << "Low score: " << lowScore << "    HighScore: " << highScore << endl;
cout << "Total Students: " << totalStudents << "  Averages of the weights: " << avgWeightAvg << endl;
ins.close();
outs.close();

return 0;
}




I put a bunch of ////////// in there to break up the code. I like to make sure everything works in main before I split it up into functions..
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Finding The Average
15 Dec, 2007 - 07:22 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,016



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You have a typo.

This:
CODE

cout << "Total Students: " << totalStudents << "  Averages of the weights: " << avgWeightAvg << endl;

Should read:
CODE

cout << "Total Students: " << totalStudents << "  Averages of the weights: " << averageWeight << endl;


Also, and this is just style for this one, your code could be easier to follow if you use a few functions. e.g.
CODE

char getStudentGrade(const float &weightedAvg) {
    if (weightedAvg >= 90) { return 'A'; }
    if (weightedAvg >= 80) { return 'B'; }
    if (weightedAvg >= 70) { return 'C'; }
    if (weightedAvg >= 60) { return 'D'; }
    return 'F';
}
...
outs << "Grade: " << getStudentGrade(weightedAvg) << endl;


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Novasplit
RE: Finding The Average
15 Dec, 2007 - 09:01 PM
Post #6

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 5


My Contributions
Thank you Bavvgai! You found my error. smile.gif I also had to initialize two other variables to zero for them to work... And now that its all working I am going to split it all off into separate functions. Thanks again.
User is offlineProfile CardPM
+Quote Post

wowhesawesome
RE: Finding The Average
17 Dec, 2007 - 09:12 AM
Post #7

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 14


My Contributions
When i created my variation of a program similar to this, my problem was with variable declaration placement within the program. I found that if i allocated the variables within the loop i constructed it no longer returned a zero with the average total.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month