I eventually figured this one out. Here’s the code I came up with:
/***********************************************************************
* 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. It doesn't count -1 as a completed assignment, so it won't
* count it when it computes the average.
*
* Estimated: 2.0 hrs
* Actual: 4.0 hrs
* I had a lot of trouble making my code pass all 3 tests in testbed. I
* kept failing at least one every time. After working on it for a while,
* though, I finally got it working.
************************************************************************/
#include <iostream>
using namespace std;
#define NUMGRADES 10
/***********************************************************************
* The function getGrades gets 10 grades from the user, passing them to main().
***********************************************************************/
void getGrades(int 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(). It doesn't count -1's as completed
* assignments, and if the user enters -1 for all grades, it outputs dashes
* instead of the average.
***********************************************************************/
void averageGrades(int grades[], int num)
{
int sum = 0;
int notCompleted = 0;
int average;
for (int i = 0; i < num; i++)
{
if (grades[i] != -1)
sum += grades[i];
else
notCompleted++;
}
if (sum != 0)
average = sum / (num - notCompleted);
if (notCompleted != num)
cout << average;
else
cout << "---";
}
/**********************************************************************
* The main function calls getGrades and averageGrades and displays the
* result returned by averageGrades.
***********************************************************************/
int main()
{
int grades[NUMGRADES];
getGrades(grades, NUMGRADES);
cout << "Average Grade: ";
averageGrades(grades, NUMGRADES);
cout << "%\n";
return 0;
}
solved How can I fix this error in my code for an assignment?