Comp Sci Store values in an arbitrarily sized matrix C++

  • Thread starter Thread starter B3NR4Y
  • Start date Start date
  • Tags Tags
    C++ Matrix
AI Thread Summary
The discussion focuses on calculating statistical values such as max, min, count, average, and standard deviation from user-inputted numbers in C++. The original poster is unsure how to store an arbitrary number of inputs, considering using an array but is advised that it's unnecessary for this task. Instead, it's suggested to use accumulator variables to keep track of the sum and count, which simplifies the calculations without needing to store all values. The conversation also touches on the benefits of dynamic arrays for cases where data needs to be saved for further processing. Ultimately, the poster learns valuable lessons about handling user input and data storage in C++.
B3NR4Y
Gold Member
Messages
170
Reaction score
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]
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:
Physics news on Phys.org
You can create a function to increase the array size by one and keep track of the size. You can use pointers.
 
B3NR4Y said:

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


Mod note: Added code tags to the code below.
My code so far is
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 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.
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):
C:
#include <cmath>
#include <iomanip>
<rest of 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:
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.
 
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.
 
B3NR4Y said:
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?
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.
 
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.
 

Similar threads

Replies
3
Views
2K
Replies
3
Views
2K
Replies
6
Views
3K
Replies
7
Views
2K
Replies
14
Views
4K
Replies
2
Views
2K
Back
Top