tags:

views:

53

answers:

1

Greetings,

I'm just looking for a bit of help here. Here's the prompt:

For research purposes the admissions officers of your local university wants to know how well female and male students perform in certain courses. The user enters the number of courses to be considered. The student's data for each course is provided as a GPA followed by a letter code representing the gender of the student in this format: each line of input consists of a real number representing the student's GPA followed by the letter code f (for female students); m (for male students). The number of entries (students) for each course is not known and the number 0.0 followed by the letter O indicates the end of data for specific course.

That being said, this is an introduction to c++ and as such; arrays, strings, and anything else outside of int, floats, doubles, and char is basically not allowed. In the code there needs to be the ability to type in various entries in any order (male entry followed by female and as well as the opposite.)

the issue i'm having is this, at the end of the program it is required to give an output of "General School Averages" which are sorted by female and male. I understand how to get the total in which to divide the problem, i just can't seem to get the sum. Anytime I try to get the sum, the value for the first course (first time through loop) is not kept so I can't figure out for the life of me how to do it. Any hints or assistance would be greatly appreciated. I know the code is long and kinda "brutish" so bear with me on that part. here's the code

//GPA calculator for Ghemri
//dealing with gpa range 0.0-4.0, set cap?
//try a do while loop

#include <iostream>



using namespace std;


int main(void)
{


 int size, counter;
 //int studentTotal= 0;
 char gender;
 double studentInfo,total,sum, avg;
 double minRange = 0.0, maxRange = 4.0;
 double maxGpa=0,gpaAvg,gpaSum;
 double femaleSum, femaleAvg, femaleTotal;
 double maleSum, maleAvg, maleTotal;
 int femaleNumber,maleNumber, gpaNumber;
 double sumFemaleAvg;// femaleGeneralAvg;//sumMaleAvg, maleGeneralAvg;


 cout << "\nPlease enter the number of courses you want considered: ";

 cin >> size;

 while(size <=0)
 {
  cout << "\nInvalid entry, number of course must be greater than zero\n";

  cin >> size;

 }

 //sumFemaleAvg+=femaleAvg;

 for(int course =1; course <= size; course++)
 {
  maleTotal = 0;
  femaleTotal=0;
  total = 0;
  femaleNumber = 0;
  maleNumber = 0;
  gpaNumber = 0;
  maxGpa= 0;
  gpaSum = 0;


  //double  doubleArray[course] = {femaleAvg};
   cout << "\nEnter student information(0.0 O to end):\t";

   cin >> studentInfo >> gender;

  while(studentInfo < minRange || studentInfo > maxRange)
  {
   cout << "\nInvalid entry, try again...\n";

   cout << "Enter student's information(0.0 O to end): \t";

   cin >> studentInfo >> gender;
  }
  if(studentInfo > maxGpa)
  {
   maxGpa=studentInfo;
  }
  if(studentInfo > 3.00)
  {
   gpaSum=studentInfo;
   gpaNumber=1;
  }

  if(gender == 'f' && studentInfo > minRange && studentInfo < maxRange)
  {
   femaleNumber=1;
   femaleSum = studentInfo;
   maleSum=0;

  }
  if(gender == 'm' && studentInfo > minRange && studentInfo < maxRange)
  {
   maleNumber=1;
   maleSum = studentInfo;
   femaleSum=0;
  }

  sum =studentInfo;
  counter = 0;
  counter++;


  while(studentInfo != 0.0 && gender != 'O')
  {

   cout << "Enter student information(0.0 O to end):\t";

   cin >> studentInfo >> gender;

   if(studentInfo > maxGpa)
   {
    maxGpa=studentInfo;
   }

   if(studentInfo < minRange || studentInfo > maxRange)
    {
     cout << "\nInvalid entry, try again...\n";

     cout << "Enter student's information(0.0 O to end): \t";

     cin >> studentInfo >> gender;
    }

   if(gender != 'm' && gender !='f'&& gender != 'O')
    {
     cout << "Invalid entry, enter m for male or f for female\n";

     cout << "Enter student's information(0.0 O to end): \t";

     cin >> studentInfo >> gender;
    }

    sum +=studentInfo;
    total+=counter;
    avg = sum/total;

    if(studentInfo > 3.00)
    {
     gpaSum+=studentInfo;
     gpaNumber++;
     gpaAvg= gpaSum/gpaNumber;
    }

    if(gender == 'f' || gender =='F')
    {
     femaleSum+=studentInfo;
     femaleNumber++;
     //femaleTotal+=femaleNumber;
     femaleAvg = femaleSum/femaleNumber;
     //sumFemaleAvg = femaleAvg;
    }

    if(gender == 'm' || gender == 'M')
    {
     maleSum+=studentInfo;
     maleNumber++;
     //maleTotal+=maleNumber;
     maleAvg = maleSum/maleNumber;
    }

   if(studentInfo == 0 && gender == 'O')
   {
    cout << "\nResults for course "<< course<<":\n";
    cout << "Female Student Average\t Male Student Average\n";
    cout << "\t";
    if(femaleNumber==0)
    {
     cout<< "N/A" << "\t\t\t";
    }
    else
    {
     cout<< femaleAvg <<"\t\t\t";//femaleAvg

    }
    if(maleNumber==0)
    {
     cout << "N/A\n";
    }
    else
    {
     cout<<maleAvg << endl;
     //sumMaleAvg = maleAvg;
    }

    cout << "\nHighest GPA: " << maxGpa<<endl;
    cout <<  "Highest average GPA for course "<< course << ": "<< gpaAvg<< endl;
   }
  }
  sumFemaleAvg = femaleAvg;

 }

 /*double genAvg[]={femaleAvg};

 result+=genAvg[course];*/

 sumFemaleAvg+=femaleAvg;

 cout<< "this is a test for the value sum " << sumFemaleAvg<<endl;
 //cout<< "this is another test " << result <<endl;

    //maleGeneralAvg = sumMaleAvg/course;
  /*cout << "the sum is " << sumFemaleAvg<<endl;
  cout << "the other sum is "<< sumFemaleAvg2<<endl;
  cout << "the other other sum is " << femaleAvg;*/

 return 0;
}
+2  A: 

Try to avoid extreme repetition and factor common operations into functions. I'll "bear with you" for now, but really there's no reason I should. This is the first thing you need to learn as a programmer.

It looks like the variable sumFemaleAvg is supposed to be summed over loop iterations. However the line sumFemaleAvg = femaleAvg; overwrites the variable every time. Do

  sumFemaleAvg += femaleAvg;

and likewise for other variables you wish to add up over multiple iterations.

Potatoswatter