#include <iostream>
using namespace std;
int main()
{
double mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8, mark9, mark10, average;
cout << "Input mark for learner 1";
cin >> mark1;
cout << "Input mark for learner 2";
cin >> mark2;
cout << "Input mark for learner 3";
cin >> mark3;
cout << "Input mark for learner 4";
cin >> mark4;
cout << "Input mark for learner 5";
cin >> mark5;
cout << "Input mark for learner 6";
cin >> mark6;
cout << "Input mark for learner 7";
cin >> mark7;
cout << "Input mark for learner 8";
cin >> mark8;
cout << "Input mark for learner 9";
cin >> mark9;
cout << "Input mark for learner 10";
cin >> mark10;
average = (mark1 + mark2 + mark3 + mark4 + mark5 + mark6 + mark7 + mark8 + mark9 + mark10) / 10;
cout << "The class average mark is:" << average << endl;
return 0;
}

- 24,506
- 3
- 32
- 49

- 69
- 3
-
8Consider using a `vector` or an `array`. – kotatsuyaki Jun 23 '22 at 09:17
-
2You can compute the average summation using only two variables (1 for input 1 for accumulation) in a loop then divide it by 10. – huseyin tugrul buyukisik Jun 23 '22 at 09:25
-
1Use `std::vector`, then maybe `std::accumulate`. Don't use explicit `std::cin`, instead create a function like `void getInput(std::istream& istr, std::vector[double] arr)`. – pptaszni Jun 23 '22 at 09:31
-
1The code does not check to see if the input fails. If the input is trustworthy, that's okay. But if the input is not guaranteed to be "clean", then the code will stumble on all input past that point. – Eljay Jun 23 '22 at 11:14
-
1rewrite the program so it can handle N marks, N can be 1, 2, 3, 50, ...., this smells like a loop and something dynamic list – rioV8 Jun 23 '22 at 11:46
5 Answers
Using an array and looping through its elements, you can use:
const int count = 10;
double mark[count];
double sum = 0;
for (int i = 0; i < count; i++)
{
cout << "Input mark for learner " << (i+1) << ": ";
cin >> mark[i];
sum += mark[i];
}
double average = sum / count;
In case you don't need the separate mark
's later anywhere, you could just use a local mark
inside the loop
const int count = 10;
double sum = 0;
for (int i = 0; i < count; i++)
{
double mark;
cout << "Input mark for learner " << (i+1) << ": ";
cin >> mark;
sum += mark;
}
double average = sum / count;

- 1,066
- 1
- 5
- 22
-
2Other improvement: `cout << "Input mar for learner " << (i+1) << ": "`. Notes: `cout` was missing. You don't need `to_string` if you use streaming already. Added some whitespace at the end of the printing for better visuals. – Notinlist Jun 23 '22 at 09:33
-
1
-
-
1Nitpick: I would name `COUNT` in lowercase, and reserve all-caps for macros. – HolyBlackCat Jun 28 '22 at 11:43
-
You can use a number of standard library functions, giving you an opportunity to learn them at the same time.
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
int main() {
std::array<double,10> mark; // use std::vector if the number of entries is variable
std::for_each(begin(mark), end(mark), [i = 0](auto& m) mutable {
std::cout << "Input mark for learner " << ++i << "\n";
std::cin >> m;
});
std::cout << std::reduce(begin(mark), end(mark)) << "\n";
}
Here is documentation for you to read:
Also avoid using namespace std;
.

- 32,434
- 14
- 99
- 159
Welcome to the community! First of all, it would be a good idea to use indentation for your code, so it is readable to others as well. If you just want to calculate the average, then you can do it with the code below. I tried to keep it as simple as possible for you.
Edit: Added new functionality to get the maximum entered value.
#include <iostream>
using namespace std;
int main()
{
int numberOfMarks = 10;
double temp, maxValue = -1, average = 0;
for (int i = 0; i < numberOfMarks; ++i)
{
cout << "Input mark for learner " << i + 1 << ": ";
cin >> temp;
average = average + temp; // Same as average += temp
// Store the maximum value
if (maxValue < temp)
{
maxValue = temp;
}
}
average = average / numberOfMarks;
cout << endl << "The class average mark is: " << average << endl;
cout << "The maximum value is: " << maxValue << endl;
return 0;
}

- 468
- 2
- 6
- 15
-
This does make a lot of sense .If i tried to also find the largest value ,would i need to use another "for statement " ? – Zaheer Letang Jun 23 '22 at 09:54
-
@ZaheerLetang Not really, you can do it inside the same loop. Just create a new double variable and compare it to the new input value each time using an if statement. I can modify my code if you don't understand what I mean. – Adrian Jun 23 '22 at 10:39
-
If you wouldnt mind.I tried searching for the solution but im not sure how to use it in this code – Zaheer Letang Jun 23 '22 at 11:31
-
@ZaheerLetang Check out the edited code, it didn't become much more complicated. Of course, it would be a better solution to copy the first entered value to maxValue instead, but that would require another if statement. So for the sake of simplicity, I initialized maxValue to -1 instead. – Adrian Jun 23 '22 at 12:59
-
1@ZaheerLetang: Initiallizing `maxValue` with `-1` will only work if the actual maximum value is greater or equal than `-1` (wich might be the case in this szenario but not in others). Initializing with `-INFINITY` works in any case. (also, see [this question](https://stackoverflow.com/questions/9874802/how-can-i-get-the-maximum-or-minimum-value-in-a-vector) for more optionns on how to find max values.) – Felix Jun 24 '22 at 06:56
Welcome to the world of C++, some of the things were mentioned already in other commends but i will try to summarize and explain. well lets start that in C++ it is recommend from avoid using C Style array as C++ offer much better solution which is std::vector which is part of STL which is very central and important part of C++.
Also i would like to advice that in general when you write software it is important to try to avoid hard-coded limitation, for example you declare for every student an variable but is more generic of you can declare in run-time how many student will be. also what if you want to extend you program? suppose you also want to print all the marks that was entered? then it is very easy to traverse data in the std::vector! (which is preferable then store the sum in a variable when loop on the fly).
Another thing is try to make your program stable , for example what if user accidentally wrote 12w instead of 127, you can use exception and check you input for such cases. (used std::stod to check the input correctness)
also it is good to know that there is no need to re-invent the wheel as C++ have very large verity of algorithms that suitable for many tasks. for example std::accumulate can add all the values in the vector from begin() to end(). (which are iterators).
Now some of the things in the code most probably is unfamiliar for you but don't be "scared" as you can cover them one by one, comment\comment parts you want to enable\disable and understand how they work, as well you can try to improve the example that i gave..
and last is pay to what compiler you have, because for example std::reduce is available from C++ 17 and would not compile in older C++ version.
Below is rewrite of your program in a more stable and generic
#include <iostream>
#include <vector>
#include <numeric>
int main()
{
size_t numberOfStudents;
std::cout << "How many learners there is in Class : ";
std::cin >> numberOfStudents;
std::vector<double> grades;
grades.reserve(numberOfStudents); // read about reserve when you learn how vector work.
for(size_t studentIndex = 0; studentIndex < numberOfStudents; ++studentIndex)
{
double mark;
std::string input;
std::cout << "Input mark for learner [" << studentIndex << "] : ";
std::cin >> input;
try
{
mark = std::stod(input); // std::stod will throw exception if the input is wrong.
}
catch(const std::invalid_argument& ex)
{
std::cout << "Error: input is Incorrect=[" << input << "]." << std::endl;
return -1;
}
grades.push_back(mark);
}
// when possible is it good practice to use const
const double totalSum = std::accumulate(grades.begin(), grades.end(),0.0);
std::cout << "Average: " << totalSum / numberOfStudents << std::endl;
return 0;
}
So in summary.
- Avoid using std::namespace and use std:: as practice.
- Prefer using std::vector<> instead of C style array.
- Use exception and\or check you input for incorrect values, array boundary, divide by zero etc.
- Use STL and learn how to use STL algorithms when you feel need to write loop.( in this case instead of writing loop that will summarize doubles in a std::vector) .
- Think about write generic, easy to read and extensible software.
Hope this will help you! and Good Luck!

- 109
- 6
-
To 2.: Not necessarily. If the size of the collection is known at compile time, using `std::array` or a raw array can have tangible performance advantages. So, it depends on the situation. For a person learning, it's probably a good thing to become familiar with `std::vector` at any rate, though. To 3: Yes, proper input checking is important. Whether `std::stod`'s job is sufficient, depends on how robust you want the program to be. Finally, the standard library is not the same as the STL and most importantly geeksforgeeks is a terrible source for C++. It gives bad and often wrong advice. – bitmask Jul 11 '22 at 12:06
Same implementation but with dynamic arrays <:)
int main(){
//this is our 10 mark variables
int* arr = new int[10]
int avr = 0;
for (int i = 0; i < 10; ++i)
{
cout << "Input mark for learner " << i + 1 << ": ";
cin >> arr[i];
avr += arr[i];
}
cout << "The class average mark is:" << avr/10<< endl;
delete[] arr;
}

- 37
- 7
-
4This is a bad advice for somebody who admits to learn C++. 1.) There is no need for dynamic allocation as 10 is a constant literal - known at compile time. 2.) `10` is a magic number. Magic numbers should be named constants. 3.) Using `new[]` literally in application code is bad style. As if `std::vector` (or in this case `std::array`) were not yet invented... 4.) (Nitpicking) A `new[]` should be accompanied by the corresponding `delete[]` (something which is auto-achieved with `std::vector`) even although the OS will care about the leaked dynamic array in this specific case... – Scheff's Cat Jun 23 '22 at 11:03
-
1