I have this program where it asks a user how many values the user will enter in, asks for the values and puts them into a vector. Then I need to calculate the average and standard deviation. I have the average part but it looks like something is wrong and I get the wrong average. And im not too sure how to get started on the standard deviation part.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
// Create vector
vector<double> values;
// Declare variables
double count;
double value;
double average;
double stdDev;
// Prompt user and get the number of values
cout << "How many values will you enter?\n";
cin >> count;
// Get the values from user and calculate average
for( double i = 1; i<= count; i++)
{
cin >> value;
values.push_back(value);
average+=values[i];
}
cout << average/count << '\n';
//Calculate standard deviation
return 0;
}
any help is appreciated.
double average;is uninitialized. It has indeterminate value. - LogicStuffdouble average; => double average = 0;. local standard datatypes are not 0 initialized. - NathanOliverpush_back,values[0]is defined butvalues[1]is not. And so on. Your counting should start at0. - Mark Ransom