Can someone please take a look at my code and help me figure out what I'm doing wrong? I'm supposed to use arrays in a program that gets 10 grades from the user, averages them, and displays the result. If -1 is entered for a grade, it's not supposed to be counted as it's considered an assignment that hasn't been completed yet. It shouldn't factor into the average at all. If the user enters -1 10 times, the program displays --- instead of the average. For some reason, the averages I'm getting are off by 1. I've tried using floats instead of ints and adding cout.setf(ios::fixed) and cout.precision(0), but that doesn't fix it.
/***********************************************************************
* Program:
* Assignment 31, Array Design
* Sister Unsicker, CS124
* Author:
* Lanie Molinar
* Summary:
* This program gets 10 grades from the user, averages them, and displays the
* result.
*
* Estimated: 2.0 hrs
* Actual: 1.5 hrs
* I had some difficulty getting the style checker to not complain about
* my NUMGRADES constant. First, it didn't like the _ when I tried to
* name it NUM_GRADES, and when I tried using NUM-GRADES, it complained
* about there not being white space between operators. It finally stopped
* complaining when I used NUMGRADES.
************************************************************************/
#include "stdafx.h"
#include <iostream>
usingnamespace std;
#define NUMGRADES 10
/***********************************************************************
* The function getGrades gets 10 grades from the user, passing them to main().
***********************************************************************/
void getGrades(float grades[], int num)
{
for (int i = 0; i < num; i++)
{
cout << "Grade " << i + 1 << ": ";
cin >> grades[i];
}
return;
}
/***********************************************************************
* The function averageGradesGrades averages the grades inputted by the user
* and returns that value to main().
***********************************************************************/
void averageGrades(float grades[], int num)
{
float sum = 0;
int notCompleted = 0;
int i = 0;
cout.setf(ios::fixed);
cout.precision(0);
for (i = 0; i < num; i++)
{
if (grades[i] != -1)
sum += grades[i];
else
notCompleted++;
}
int average = sum / (num - notCompleted);
if (notCompleted != num)
cout << average;
else
cout << "---";
return;
}
/**********************************************************************
* The main function calls getGrades and averageGrades and displays the
* result returned by averageGrades.
***********************************************************************/
int main()
{
float grades[NUMGRADES];
getGrades(grades, NUMGRADES);
cout << "Average Grade: ";
averageGrades(grades, NUMGRADES);
cout << "%\n";
return 0;
}
If the user enters -1 10 times, the program displays --- instead of the average
1 2 3 4 5 6
NUMGRADES 10
if (notCompleted != num) ///in here is your bug
cout << average;
else
cout << "---";
the averages I'm getting are off by 1int average = sum / (num - notCompleted); ///when you convert a floating point number to an integer you'll lose some precision and it's not a good idea.
I finally figured it out. I didn't need to use floats after all because ints worked well. Also, I was missing an if statement, so my program kept trying to divide 0 by 0 in some cases, which made it crash.