Hello all. First time working with vectors in C++, and I'm having a bit of an issue. My goal is to have a vector of 70 random integers ranging from 30 to 50, print out each integer, and find the average value of all 50 numbers. This is what I have thus far.
#include <iostream> #include <vector> #include <ctime> #include <cstdlib> using namespace std; int main() { vector <int> temp; int randNum; srand(time(NULL)); int sum; for(int i = 0; i < 70; i++) { randNum = rand() % 50 + 1; temp.push_back(randNum); } for (int i = 0; i <= 70; i++) { sum += temp[i]; } float average = sum / 70; cout << "The average is " << average; return 0; }
So far, so good. You may want to add and endl to your cout.
Join our real-time social learning platform and learn together with your friends!