- #1
B3NR4Y
Gold Member
- 170
- 8
Homework Statement
"Calculate the max, min, count, average, and standard deviation (std dev) of a set of numbers.
The formula for average is:
average is sum divided by count
The formula for standard deviation is
stddev is the square root of the variance
The formula for variance is
variance is (the sum of the squares divided by the count) minus the average squared.
By the sum of the squares, I mean for a list like: 2 3 4, the squares are 4, 9, 16, and therefore the sum of the squares would be 29.
When prompted "Enter Another? " Type n to leave the prompt and calculate the output values. Type y, or any other single character to continue entering values."
I know how to do this in principle, but I'm having trouble storing an arbitrary amount of numbers. I think it should be an array, but I am not quite sure.
Homework Equations
while loops are definitely useful.
The Attempt at a Solution
My code so far is
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
string answer;
double firstNumber;
cout << "Enter a number: ";
cin >> firstNumber;
cout << "Enter another? ";
cin >> answer;
while (answer == "y"){
cout << "Enter another? ";
cin >> answer;
}
}
[I]
I reason the program should initially ask for a number, store it, then ask if you want another and store that in answer. Then use a while loop to continuously create variables each time the user inputs yes and then cease when the user enters no. But I don't know how to store a potentially infinite amount of variables, obviously I can't create millions of new variables, so it should be an array and I just call upon the members of the array when I calculate the standard deviation and such, but I don't know how to do that. Especially since the array is of arbitrary size.
Last edited: