Hi all,
im am writing a program to take in and store a student's grade as an int.
Can anyone tell me please how do i get the average score of the grade?

This is what i have so far...

Regards.

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	const int NUM_STUDENTS=5;
	const int NUM_SCORES=6;
	
	int i;
	struct Student
	{
		int number;
		float scores[NUM_SCORES];
	};

	Student studentA, studentB ={333333, {45, 53, 57, 46, 51, 55} },
		    studentC ={333333, {45, 53, 57, 46, 51, 55} };

	
	
		
	cout << "Enter the STUDENTS ID NUMBER: ";
	cin >> studentA.number;
	
	cout << "Enter " << NUM_SCORES << " test scores seperated by a space: ";

	for (i=0; i <NUM_SCORES; i++)
	{
		cin >> studentA.scores[i];
	}



	studentB.number = studentA.number+1;


	for (i=0; i<NUM_SCORES; i++)
	{
		studentB.scores[i]=65;
	}

	cout << "\n StudentA: number " <<  studentA.number << " scores: ";
	cout << fixed << setprecision(1);


	for (i=0; i<NUM_SCORES; i++)
	{
		cout << setw(5) << studentA.scores[i];
	}

	cout << "\n StudentB : number " << studentB.number << " scores ";
	for (i=0; i<NUM_SCORES; i++)
	{
		cout << setw(5) << studentB.scores[i];
	}

	cout << "\n StudentC: number " << studentC.number << " scores ";

	
	for (i=0; i<NUM_SCORES; i++)
	{
		cout << setw(5) << studentC.scores[i];
	}

	cout << endl << endl;
		
	
	

	return 0;
}

In your loops where you are inputting the values, keep a running sum of the scores. Then outside of the loop divide by NUM_SCORES.

In your loops where you are inputting the values, keep a running sum of the scores. Then outside of the loop divide by NUM_SCORES.

Thanks for your reply,

but would you be able to elaborate on that please?

make a variable called total, initialize it to zero
for loop (i: from 0 to NUM_SCORES)
   total+=score[i]

Then divide total by NUM_SCORES
You can create a total variable for each of A, B, and C, or you can recycle it by setting it to zero in between loops.

First, do what jonsca suggested by creating a new variable, adding to the variable of the current grade to get a running total of all grades added together, then divide by NUM_SCORES (sum)/NUM_SCORES will give you your average.

To extend on that, you will need to add an additional for-loop to get the average from the students that you did not initially input from the user. That for loop will sum all the grades together as I explained earlier and then again, divide by NUM_SCORES. You can make a local variable to save the averages for each student, or add a member function to the original structure, Student.


You may also make a function to pass as an argument the student object so that you don't have to rewrite the function three times, or as many student as there are. You just have to create the function once, then call the function as many times as you want to, no matter how many students.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.