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];

}

cout << "The min value is " << min << endl;

}

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];

}

cout << "The min value is " << min << endl;

}

Last edited on

I think double is limited to (15 digits). I think your using 16.

http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx

Last edited on

`sizeof`(v)

is `v.size()`

Topic archived. No new replies allowed.