I don't know how to calculate average

I am doing a homework about calculating average. It is about calculating average of some intergers. I do not know the number of intergers entered before a user use the programme. Then, the user will enter the intergers. This homework requires me to enter the intergers with a space separated (for example:3 4 5). Then, I don't know how to add up the integers and find the average.

Could anyone help me?
Can you show your efforts in doing this? ( ie: some of your code )
#include<iostream>
using namespace std;

int main(){

double assignmentNum,examNum,scoreA,scoreE;
int counterA,counterB;
cout<<"Give the number of assignments: "<<endl;
cin>>assignmentNum;


for(counterA=1;counterA<=assignmentNum;counterA++){
cout<<"Give the score of assignment"<<endl;
cin>>scoreA;}



double assignmentAvg=scoreA/assignmentNum;


cout<<assignmentAvg<<endl;



return 0;

}
Last edited on
Notice that scoreA/assignmentNum will divide the last input by the number of values.
You need another variable to hold the sum of the various values given by the user and then divide that one

And please use [code][/code] tags when posting code
I don't know how to hold the sum as the number of integers entred is not known until I enter the number of integers.
Initialize that variable with 0 and add the value of the new input each time you get one
But the integers are entered in this way:3 4 5. It isn't like this: Enter a integer: 3 Enter a integer: 4 Enter an integer:5 . So, I can't use this fomula: (a+b+c)/n
Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	int number1;
	int number2;
	int number3;
	int average; 
	
	std::cout << "Enter First Integer: "; 
	std::cin >> number1;
	
	std::cout << "Enter Second Integer: "; 
	std::cin >> number2;
	
	std::cout << "Enter Third Integer: "; 
	std::cin >> number3;
	
	average = (number1 + number2 + number3) / 3;

	std::cout << "Average is " << average << std::endl;

	return 0;
}
You can maintain an ongoing average with just the average value and total number of datapoints.

Since average = sum / number of data points, we can rework that formula to get sum = average * number of data points. You only need to keep track of the average and total number of datapoints to be able to recalculate the average with each newly added datapoint.

So in a loop, you can prompt the user to input a value, recalculate the average, and then repeat until the user is done inputting values.

The following formula can be used to recalculate the average:
average = (average*numDataPoints + newVal)/(1 + numDataPoints++);

Alternatively, if you want to keep track of the sum as well (it's only one additional variable), you could use this slightly less complex formula:
average = (sum += newVal)/++numDataPoints;
Above only (barely) works if average is floating point, otherwise error is introduced due to integer division
(dropping of remainder). It is best simply to keep the sum and number of data points.
But the question doesn't allow me to use this statement more than once: Enter an integer.

It only allows: Enter the integers: a b c ...


This is the question:

"When calculating the grade of a course, we usually need to take into account the scores achieved by the student in both 1) the written assignments and, 2) the various exams.

In this lab you are asked to implement a Course Grade Calculator program that takes as input the scores of a student in both the assignments and the exams and outputs the grade achieved by the student in the course.

To achieve this purpose, you need to know how it is possible to calculate the grade in a course given the different scores. To facilitate the development of the program, we can make the following assumptions:

Both types of scores can range from 0 (lowest) to 100 (highest).
The student has handed assignmentNumber assignments and taken examNumber exams. For assignments, we can compute the corresponding average value by adding all the assignment scores and then dividing them by the number of assignments. Similarly for exams. Let us call the two average values assignmentAvg and examAvg respectively.
The relative importance of each score is determined by the weight of the score. In our scenario, all assignment scores have the same weight, called assignmentWeight, and all exams have also the same equal weight, called examWeight. The ratio between the two weights is an indicator of how important assignments are compared to exams. For instance, if the assignments have weight 1 and exams have weight 2, then that suggests that exam scores are twice as important as assignment scores, when calculating the grade.
The total score, denoted as totalScore, can be computed by the weighted average of the assignment and the exam scores. Concretely, if assignmentAvg and examAvg are the average of the assignment and exam scores respectively, then totalScore can be computed using the formula:
totalScore =
(assignmentWeight*assignmentAvg + examWeight*examAvg)/
(assignmentWeight + examWeight)
After the calculation of the total score, we distinguish between the following cases:
If totalScore is between 90 and 100 (90 inclusive), then the grade is A.
If totalScore is between 80 and 90 (80 inclusive), then the grade is B.
If totalScore is between 70 and 80 (70 inclusive), then the grade is C.
If totalScore is between 60 and 70 (60 inclusive), then the grade is D.
If totalScore is below 60, then the grade is F.
Input and Output Specifications
Input
Give the number of assignments: assignmentNumber
Give the number of exams: examNumber
Give the weight of assignments: assignmentWeight
Give the weight of exams: examWeight
Give the score of the assignmentNumber assignments: score_1 ... score_n (separated by spaces)
Give the score of the examNumber exams: score_1 ... score_k (separated by spaces)

Output
The grade of this course is: grade
Do you want to calculate the grade of another course? (y/n) option

Valid range for all scores is 0 to 100, while the weights have to be positive numbers. Also, you do NOT have to check whether the input data are valid or not. We assume that all inputted data are valid.

A skeleton file and a sample program are provided for your reference. "


The sample is:

http://course.cse.ust.hk/comp102/Info/Ron/fall09/lab4/img/Execute2.jpg


Last edited on
There are several ways of receiving input that way, some more robust than others, but since we're assuming all input data are valid, here's probably the easiest (but least robust) method.

1
2
3
4
5
6
7

  //assuming numAssignments was already specified:
  for(int x = 0; x < numAssignments; x++){
    cin >> scoreA;
    sum += scoreA;
  }


With that code, if numAssignments = 3, you can enter 3 numbers with spaces in between, then hit enter, and it'll input and sum those three numbers.
Topic archived. No new replies allowed.