So for college we got a programming project where we have to find the values of a vector and part of it is finding the min and max value
so i created a little start out program and it was going well until i got an error when i would try to run it telling me the vector subscript is out of range
what am i doing wrong?
here's my code :
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector <double> v;
double x = 0;
while (x != 0)
{
cin >> x;
v.push_back(x);
}
double min = 0;
double max = 1000000000000000;
double median = 2;
double average = 0;
for (int i = 0; i < sizeof(v); ++i)
{
if(v[i] >= max)
max = v[i];
}
cout << "The max value is: " << max << endl;
for (int i = 0; i < sizeof(v); ++i)
{
if(v[i] <= min)
min = v[i];
}
sizeof(v) is NOT the size of the vector. It is the size of the class that holds the vector, not the number of elements in the vector. You want to use v.size()