# Homework Help: Store values in an arbitrarily sized matrix C++

1. Oct 9, 2015

### B3NR4Y

1. The problem statement, all variables and given/known data
"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.

2. Relevant equations
while loops are definitely useful.

3. The attempt at a solution
My code so far is
Code (C):
#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]
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: Oct 9, 2015
2. Oct 9, 2015

### axmls

You can create a function to increase the array size by one and keep track of the size. You can use pointers.

3. Oct 9, 2015

### Staff: Mentor

In the future, please use code tags around your code. I have done this in what I copied here. Here's how you do it (using the optional attribute for C or C++ code):
[code=c]
#include <cmath>
#include <iomanip>
<rest of code>
[/code]

There is no need whatsoever for storing the numbers in an array. To calculate the mean, all you need is the sum of numbers entered so far, and the count of them. To calculate the standard deviation, you need to sum of the squares of the numbers and the square of the sum of the numbers. To calculate each of the sums, use an accumulator variable (it must be initially set to 0) inside a loop. In each loop iteration do this:
Code (C):
sum_x = sum_x + x;
count++;
Use something similar to get the sum of the squares of the input values.

For the calculation of the min and max values, the first value you read will automatically be both the min and max value. After each new value is read in, program logic should reset the min variable if the new value is smaller than the previous min value. Use similar logic for determining the max value.

4. Oct 9, 2015

### B3NR4Y

Thank you both! You helped me a lot. I ended up using arrays, and learned a lot about them then I found out the assignment doesn't require arrays like Mark said, but at the end of the day I learned something and that's all that matters, right?

Thanks a lot, again.

5. Oct 9, 2015

### Staff: Mentor

Well, maybe. The way I recommended is better if you don't know in advance how many numbers you need to work with. If you use arrays, you need to specify at compile time how big the array needs to be.

6. Oct 11, 2015

### DuncanM

Since your problem, as-is, does not require you to save the numbers, I suppose your solution works okay. Glad to know you got it working.

However, in most cases, the data is saved for additional work. In this case, a dynamic array would be used.
One way of approaching this problem would be to keep prompting the user for input until a pre-determined character is input and then you know no more data will be input. Each time through the loop, the dynamic array is incremented by one unit and, to be thorough, you should check to see if the memory was successfully allocated (an exception thrown.) Once all the data is input, you can do what you want with it. And because the data is saved in an array, it is there for additional work, if needed.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted