Averaging arrays question!

I have written a code but can not seem to crack how to average the array with only the slots entered in the running program. as it is right know it is dividing by 10 because that is the max on the array. Here is my code and thanks for the input!

#include <iostream>
using namespace std;

int main()
{
const int MAXLABS = 10; // max size of the lab array
const int MAXTESTS = 3; // max size of the test array
float labs[MAXLABS]; // float array of labs
float tests[MAXTESTS]; // float array of tests
int lgrade = 0; // # of labs entered
int lx; // loop control variable (LCV)
int tgrade = 0; // # of tests entered
int tx; // loop control variable (LCV)
float avg1, avg2; // avg1 = LAb average, avg2 = Test average
float OLGRADE; // this is the overall score for both the labs and tests


do {
cout << "Enter a Lab Grade ";
cin >> labs[lgrade];
lgrade++;
cout << endl;
} while ( ( lgrade < MAXLABS) && ( labs[lgrade-1] != -1) );

if ( labs[lgrade-1] == 0)
{
lgrade--;
}

for (lx= 0; lx < lgrade; lx++)
{
cout << "labs[" << lx << "] = " << labs[lx] << endl;
}
// compute the average for labs

avg1 = 0;
for(lx= 0; lx<10; lx++)
avg1+= labs[lx];
avg1 /= 10;
cout << "Average is " << avg1 << '\n';

do {
cout << "Enter a Test Grade ";
cin >> tests[tgrade];
tgrade++;
cout << endl;
} while ( ( tgrade < MAXTESTS) && ( tests[tgrade-1] != -1) );

if ( tests[tgrade-1] == 0)
{
tgrade--;
}

for (tx= 0; tx < tgrade; tx++)
{
cout << "tests[" << tx << "] = " << tests[tx] << endl;
}
// compute the average for tests

avg2 = 0;
for(tx=0; tx<3; tx++)
avg2+= tests[tx];
avg2 /= 3;
cout << "Average is " << avg2 << '\n';

// overall grade
OLGRADE = (avg1 * 0.6) + (avg2 * 0.4);
cout << "Overall Grade is " << OLGRADE << '\n';


return 0;

}
Two things before I give you a hint: first, you wrote a "program", to use the term loosely, not a code; second, please use [co de][/co de] tags.

Now for the hint, you can always have another variable that acts as a counter.
Last edited on
Well, it seems to me that you have two options: Use lgrade, or loop looking for the value of -1. I prefer the former.

According to your input loop, you'll stop at lgrade == 10 or input value == -1. If you input -1, you know that lgrade is one too many inputs. Same thing if you typed -1. -1 should not be considered for the average.

The following I don't understand:
1
2
3
4
if ( labs[lgrade-1] == 0)
{
lgrade--;
}


I interpret this as follows: "If the last grade entered is zero, then remove one from the lgrade counter (which before the reduction equals the total number of grades entered)". Are you saying that a grade of zero can be ignored from the average? Some kind of curve for the students? If it is, then note that for it to work you will have to make sure that you enter the zero grade for the student as the last entry or the student will not get the adjusted average. Or maybe the adjusted curve rule is "if the student got a zero in the last test, then eliminate it". If this is the rule, then I guess it is OK.

If the above code has a different purpose, then remove it because it is not acheiving such purpose.

Now the average loop:
1
2
3
4
5
6
avg1 = 0;
for(lx = 0; lx < lgrade; lx++)
{
    avg1 += labs[lx];
}
avg1 /= lgrade;


At this point, avg1 should contain the correct average. Note however, that you must not touch the value of lgrade between the end of the input loop and the end of the average calculation, as lgrade represents the total number of elements in the labs array.

The same technique can be applied to the other average you calculate.

This sounds like homework to me, and I might have revealed a little too much, but it seems to me that you needed the extra push. I sure hope that this helps you truly learn this powerful programming language.
Topic archived. No new replies allowed.