Full score counter in for loop

Hi guys! For one of my assignments I have to write a code that collects lab scores from user input and do some calculations with them. I got through most of it but am stuck at the final part.

QUESTION: Calculate how many full scores (i.e. 10) you received in your labs. If you get more than 5 full scores write "GOOD WORK" otherwise write "NEEDED TO WORK HARDER". (Hint: write another for loop to check if it is a full score and keep count of the full scores. Outside the loop write the message depending on the number of scores.

What would I use as the parameters for the for loop? What would go inside of it? Do I need to declare another integer for this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int lab_score[12]; //declare variable array for 12 lab scores.
    int i; //declare i for loop.
    int lab_sum=0, lab_avg; //declare variables for the sum of all of the lab scores and for the average of the scores.

    for (i=1; i<=12; i++) //a for loop that makes the following prompt be done 12 times, going from 1-12.
    {
        cout << "Please enter the score you got on lab #" << i << ":\t"; //ask the user to enter lab scores.
        cin >> lab_score[i]; //allow user to enter lab scores.
        lab_sum=lab_sum+lab_score[i];
    }
        lab_avg=lab_sum/12;

        cout << endl;
        cout << "Lab score total: " << lab_sum << endl;
        cout << "Lab score average: " << lab_avg << endl;
Hello newprogrammergirl,

I add the extra lines just to give you an idea. I believe this would work better for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//#includes??

constexpr std::size_t MAXSIZE{ 12 };

int main()
{
	//.
	//.
	//.

	int lab_score[MAXSIZE]; //declare variable array for 12 lab scores.
	//int i; //declare i for loop. Not necessary to define it here.
	int lab_sum = 0, lab_avg{}; //declare variables for the sum of all of the lab scores and for the average of the scores.

	for (int i = 0; i < MAXSIZE; i++) //a for loop that makes the following prompt be done 12 times, going from 1-12.
	{
		cout << "Please enter the score you got on lab #" << i + 1 << ":\t"; //ask the user to enter lab scores.
		cin >> lab_score[i]; //allow user to enter lab scores.
		lab_sum = lab_sum + lab_score[i];
	}

	lab_avg = lab_sum / MAXSIZE;  // <--- Calculation could also be put in the "cout" statement.

	cout << endl;
	cout << "Lab score total: " << lab_sum << endl;
	cout << "Lab score average: " << lab_avg << endl;  // <--- Calculation could be put here.

	//.
	//.
	//.

	return 0;
}

I put a comment on line 12 because you can and should define "i" in the for loop. The only time to define "i" outside the for loop is when you need the value of "i" later.

In the for loop this is where you should define "i" also you need to start at zero not one. C++ along with other languages are zero based which means that array and anything like an array; vector, list, map and others the first element is zero. Starting at one like you do means you skip the first element of the array. In the middle yor use of i <= 12 will put outside the boundary of the array because the elements of the array go from 0 to 11 not 12.

Inside the for loop is OK except you will need to change "i" in the first "cout" statement to "i + 1", so it looks right.

After the for loop the code will work, but notice the comments I made in the code.

It is more acceptable to the variable "MAXSIZE" as I have an stay away from the magic numbers. This way if you want to change the size of the array you only have to go to one place do do this and everywhere "MAXSIZE" is used will be changed so to speak. The use of capital letters lets you know that the variable is defined as a constant.

Hope that helps,

Andy

Edit:
Last edited on
QUESTION: Calculate how many full scores (i.e. 10) you received in your labs. If you get more than 5 full scores write "GOOD WORK" otherwise write "NEEDED TO WORK HARDER". (Hint: write another for loop to check if it is a full score and keep count of the full scores. Outside the loop write the message depending on the number of scores.

What would I use as the parameters for the for loop? What would go inside of it? Do I need to declare another integer for this?
One comment on Andy's revision: Since the average might not be an integer, you should declare lab_avg as a double. But even that won't work right, because lab_sum/MAXSIZE will be computed using integer arithmetic. The result is truncated toward zero to keep it as an integer. To fix this, you just have to cast one of the numbers as a double. I'd do MAXSIZE since it can be done at compile time: lab_sum / (double)MAXSIZE. Or for the purists: lab_sum / static_cast<double>(MAXSIZE)

Now for your question. You'll need to
1 count up the number of full_scores the user received.
2 Use an if statement to see if they got more than 5 full scores, and
3 print "GOOD WORK" or "NEEDED TO WORK HARDER" depending on the result of the if.

See how I broke the problem down into smaller steps? That's the essence of programming.

To count up the full scores, you'll need
- a new counter. Let's call it full_scores. Define it and initialize to zero.
- a for loop. This can be exactly like the first one: for (int i=0; i<MAXSIZE; i++)
- Inside the for loop, you want to increment full_scores, but only if lab_score[i] is a full score.

See if you can translate all that to code.
A couple of other things to think about:
- Could you do all of this inside one loop rather than requiring two?
- Once you have it inside one loop, do you have to store the individual scores inside the array at all? When a loop iteration is done, do you ever access the individual score again?
@newprogrammergirl - Often people make a separation between the obtaining of input and the analysis, so this is why your teacher suggested to make a second loop to iterate over your storage structure.

Since the program is simple enough, you could also do the analysis in real-time. You're already summing things up at the moment of input; in this same area you could also check for "full scores", incrementing a counter or so.
Topic archived. No new replies allowed.