I am a C++ beginner. Currently learning vectors. I am writing a program that calculates the average given the grades. The program I have written accepts only 5 grades. How can I modify the program so that the vector space is automatically allocated for any number of grades? That is, at the 'Enter grades:' prompt, I should be able to enter any number of grades and the average for those grades have to be calculated.
#include <iostream> #include <vector> using namespace std; int main() { vector<int> grades; int grade; cout << "Enter grades: "; for(int i = 1; i <= 5; ++i) { cin >> grade; grades.push_back(grade); } double total = 0; for(int i = 0; i < grades.size(); ++i) { total += grades[i]; } double avg; cout << "Average = " << total / grades.size() << endl; return 0; }
total.