PLEASE HELP!!!!i need to write a program to record, average, and display grade for a maximim class size of 15 students. display a menu of options to the user, such as: input grades, display grades, and display class average.

Have you attempted anything on your own?

If not, I suggest first mapping out (conceptually, or on paper) exactly what you have to do. Create a flowchart for program flow if you have to...

For example, a simple diagram of flow could be:

get input from user --> save input in proper format (variable) --> compute averages --> display computations

Next, try to translate your ideas into code or "pseudocode" to start. If you do this, you'll find your problem is actually not very difficult at all.

Finally, once you have a working shell for your program, you can consider adding more complex features. For example, you may want to include some error checking in your code, and make your code fool-proof (so that it wont break in certain conditions). If your really up to it, you may also want to create a GUI-complete "mark program".

In any case, start with the flowcharts and pseudocode (I know this might seem silly for this problem, but its a good habit to get into), and do some problem solving to figure out what it is you have to do. Then try to translate it into some code. Things to think about: how are you going to get user input, how will you store it, how do you calculate averages...just break the problem down, get one part working at a time, and you'll find it a much less daunting task.

(I have to go for lunch but I'll check on ya when I get back). Good luck.

>>PLEASE HELP!!!!

Post code using code tags, include a description of the problem you're having, etc.

thats what i have so far(continues doing)... my problem is that. i duno how to inset each code to the menu..

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


int main()
{
const int SIZE = 15;
int choice, answer;
double grade ;
double total = 0;
double average = 0;


cout << "This program would ask display the grade for a maximim class size of 15 students.";



cout << "Please select.";
<< "1. Enter grade.";
<< "2. Display grade.";
<< "3. Exit the program. ";
<< "choice: ";
cin  >> choice;
}



for (int i = 0; i < SIZE; i++)
{
cout << "Enter grade " << i + 1 << ": ";
cin >> grade;


}


cout << "\nYou entered:\n\n";
for (int i = 0; i < SIZE; i++)
{
cout << "double grade[" << i <<"]: " << grade << endl;
total += grade;
}
average = total / SIZE;


cout << "\nThe total is: " << total << endl;
cout << "\nThe average is: " << average << endl;



return (0);
}

Edited 3 Years Ago by happygeek: fixed formatting

Well...I'm not totally sure what it is your asking, but I guess I could lead you in a direction that I think is what you are looking for...

Looks to me like you need to create some helper functions (or at least thats what I would do). So for example (I have provided the prototypes for some functions that might be useful):

#include <iostream>
#define SIZE 15
using namespace std;

void enter_grades(double *);
void display_grades(double *);

int main()
{
     int choice, answer;
     double grades[SIZE], total = 0, average = 0;

     cout << "Please select...\n(1) Enter Grades\n(2) " <<
             "Display Grades\n(3) Exit\nChoice: ";
     cin >> choice;

     switch (choice)
     {
          case 1:
          {
               enter_grades(grades);
          } break;
          case 2:
          {
               display_grades(grades); 
          } break;
          case 3:
          {
                return 0;
          } break;
          default:
          {
               cout << "\n\nYou have entered an invalid choice.  " <<
                      "Program terminated prematurely.";
               return 1;
          }
     }

     // compute total and average here, or create a helper function to do it
   
     cout << "\n\nThe total is: " << total << endl;
     cout << "The average is: " << average << endl;

     system("pause");
     return 0;
}

void enter_grades(double *grades)
{
     //code here to get the grades
}

void display_grades(double *grades)
{
     //code here to display the grades
}

Basically then, based on what the user inputs, you call a corresponding function to accomplish whatever task. This is one way of doing things. Alternatively, instead of using a switch statement, you could just use multiple if statements, but this is a classic case of switch because later you may choose to add more menu options...Also, you could change the functions to take arrays as parameters if you are not comfortable using pointers (but as you may have already learned, or will learn eventually, they are pretty much the same thing...)

Also, after you write the code for the helper functions, you may want to allow the user to loop back to the menu...Just something to think about.

Thank you .. but i dont know why i stil got some error messages..and the total and average didnt come out right..

#include <iostream>
#include <iomanip>
using namespace std;


void enter_grades(double *);
void display_grades(double *);
const int SIZE = 25;


int main()
{


int choice;
double grades, total = 0, average = 0;


cout << "Please select...\n(1) Enter Grades\n(2) " <<
"Display Grades\n(3) Exit\nChoice: ";
cin >> choice;


switch (choice)
{
case 1:
{
enter_grades(grades);
} break;
case 2:
{
display_grades(grades);
} break;
case 3:
{
return 0;
} break;
default:
{
cout << "\n\nYou have entered an invalid choice.  " <<
"Program terminated prematurely.";


}


return 1;
}


total = total + grades;
average = total / SIZE;



cout << "\n\nThe total is: " << total << endl;
cout << "The average is: " << average << endl;


system("pause");
return 0;
}


void enter_grades(double *grades)
{
for (int i = 0; i < SIZE; i++)
{


cout << "Enter grade " << i + 1 << ": ";
cin >> grades;
}



}


void display_grades(double *grades)
{


for (int i = 0; i < SIZE; i++)
{
cout << setw(8) << grades;
if ( (i + 1) % 5 == 0)
cout << endl;


}
}

Edited 3 Years Ago by happygeek: fixed formatting

post first couple error messages and identify the lines they refer to.

You need to determine the total somehow. One way is to loop through the array containing the grades after all data entry has been completed, adding each grade to a running total before dividing by the actual number of grades in the array. That will require you to pass an int by reference to enterGrades() to keep track of the number of grades actually entered, since it may be less than SIZE given this statement for post #1: "display grade for a maximim class size of 15 students. " An alternative is to pass average to enterGrades by reference, calculate the average in enterGrades by calculating a running total and keeping track of the actual number of grades entered and then calculate average before exiting enterGrades. But since you're going to want to know the actual number of grades enetered when it comes time to display them anyway, I'd do it the first way.

First of all please keep in mind to post your code using the code tags. You have not done so even after Lerner told you.

total = total + grades[SIZE];

what you are doing here is adding 0 and value of grade[25] whereas the array size is only 24 (0 to SIZE-1), hence you get a garbage value

Even if you change the code to total = total + grades[SIZE-1]; , the result will still be the last value of the array.

You need to loop from the start and go till SIZE-1 and keep adding each element to total to get the correct sum

for(int i=0;i<SIZE;i++)
{
total=total+grades[i];
}

i just found out that .. i need to write my code in this way.. but i got error message..

#include <iostream>
using namespace std;


const int MAXSIZE = 25;


// Add function prototypes here
void readValues(int [], int&);
void displayValues(int[], int);
int findTotal(int[], int);
int findAverage(int[], int);


int main()
{
int values[MAXSIZE];
int numValues;  // Counts the number of values entered
int total, average;


// Use reference parameters here for numValues
readValues(values, numValues);
displayValues(values, numValues);
total = findTotal(values, numValues);
average = findAverage(values, numValues);



// Add code to display the total and average values.
cout << "\nThe total entered was: " << total << endl;
cout << "\nThe average entered was: " << average << endl;


return (0);
}


// Define functions here
void readValues(int vals[], int& num_els)
{
int choice;
num_els = 0;
cout << "Enter a maximum of " << MAXSIZE << " values or 99999 to terminate\n";
cout << "Please select...\n";
cout <<"(1) Enter Grades\n";
cout <<"(2) Display Grades\n";
cout <<"(3) Exit\n";
cout <<"Choice: ";
cin >> choice;


if (choice ==1)
for (int i = 0; i < MAXSIZE; i++, num_els++)
{
cout << "Enter value " << i + 1 << ": ";
cin >> vals;
if (vals == 99999)
break;
}


return;
}


void displayValues(int vals[], int num_els)
{
double choice;
if (choice ==2)
cout << "You entered " << num_els << " values: \n";
for (int i = 0; i < num_els; i++)
{
cout << "Value " << i + 1 << ": " << vals << endl;
}


return;
}


int findTotal(int vals[], int num_els)
{
int i, tot = vals[0];


for (i = 1; i < num_els; i++)
if (tot < vals)
tot = vals;


return tot;
}


int findAverage(int vals[], int num_els)
{
int i, ave = vals[0];


for (i = 1; i < num_els; i++)
if (ave > vals)
ave = vals;


return ave;
}

Edited 3 Years Ago by happygeek: fixed formatting

1) Put this:

int choice;
bool more = true;

while(more)
{
  cout << "Enter a maximum of " <<  MAXSIZE << " values or 99999 to terminate\n";
  cout << "Please select...\n";
  cout <<"(1) Enter Grades\n";
  cout <<"(2) Display Grades\n";
  cout << "(4) Display Class Average\n";
  cout <<"(3) Exit\n";
  cout <<"Choice: ";
  cin >> choice;

  /*switch statement here to call the functions as described by n1337 in post #5.  case 3 should change the value of more to stop the loop.  */
}

in main() instead of readValues.

2) Change this:

cin >> vals[i];
if (vals[i] == 99999)
break;

to this:

int temp;
cin >> temp;
if(temp == 99999)
{
   vals[i] = temp;
   break;
}

since you don't want to include the value of 99999 with the scores.

3) Drop the first line below:

if (tot < vals)
tot = vals;

and change the second line to this:

tot += vals;

4) To find the average you need the total and the number of grades entered, so pass those values to findAverage, calculate the average and return it. BTW, the way you have average declared it will truncate any decimal points and just display the integer value of the decimal calculated by dividing total by number of grades entered. If that's what you want, fine. If not, declare average to be a double and make relevant changes as needed to accomodate that change.

This article has been dead for over six months. Start a new discussion instead.