forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path52-average_grades_using.cpp
43 lines (37 loc) · 1.1 KB
/
52-average_grades_using.cpp
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
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <stdexcept>
using std::vector;
/* average_grades( grades )
Given a vector of grades (integers in the range 0 - 100),
compute and return the average of all elements.
Task: Improve this code to deal with cases where the
vector is empty or where grades in the vector are
outside the range 0 - 100.
*/
float average_grades(vector<int> const &grades)
{
if (grades.size() == 0)
{
throw std::invalid_argument{"Can't compute the average of zero things"};
}
float sum{0};
for (unsigned int i{0}; i < grades.size(); i++)
{
if (grades.at(i) < 0 || grades.at(i) > 100)
{
throw std::out_of_range{"Grade out of range"};
}
sum += grades.at(i);
}
return sum / grades.size();
}
int main()
{
vector<int> grades1{90, 70, 80};
vector<int> grades2{}; //Invalid (can't average zero things)
vector<int> grades3{90, 70, -80}; //Invalid (grades can't be negative)
auto av{average_grades(grades1)};
std::cout << "Average: " << av << std::endl;
return 0;
}