Store values in an arbitrarily sized matrix C++

  • Context: Comp Sci 
  • Thread starter Thread starter B3NR4Y
  • Start date Start date
  • Tags Tags
    C++ Matrix
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a C++ program to calculate statistical values (max, min, count, average, and standard deviation) from a set of user-input numbers. Participants explore methods for storing an arbitrary number of inputs, debating the use of arrays versus alternative approaches.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses uncertainty about how to store an arbitrary number of inputs, suggesting that an array might be appropriate.
  • Another participant proposes creating a function to dynamically increase the size of an array using pointers.
  • A different participant argues that storing numbers in an array is unnecessary, suggesting that maintaining a sum and count would suffice for calculating the average and standard deviation.
  • Participants discuss the logic for calculating min and max values based on user input, emphasizing the need to reset these values as new numbers are entered.
  • One participant shares their experience of using arrays and acknowledges that the assignment did not require them, reflecting on the learning process.
  • Another participant reiterates that using arrays requires knowing the size at compile time, suggesting that dynamic arrays are more suitable for unknown input sizes.
  • A later reply emphasizes the importance of saving data for potential additional work, advocating for the use of dynamic arrays and proper memory management.

Areas of Agreement / Disagreement

Participants exhibit a mix of agreement and disagreement regarding the necessity of arrays for the task. While some advocate for their use, others argue that simpler methods can achieve the same results without them. The discussion remains unresolved on the best approach to take.

Contextual Notes

Participants highlight limitations in their approaches, such as the need for dynamic memory allocation and the implications of using static arrays. There is also mention of the importance of checking for successful memory allocation when using dynamic arrays.

B3NR4Y
Gold Member
Messages
170
Reaction score
1

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;
    count << "Enter a number: ";
    cin >> firstNumber;
    count << "Enter another? ";
    cin >> answer;
    while (answer == "y"){
        count << "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;
    count << "Enter a number: ";
    cin >> firstNumber;
    count << "Enter another? ";
    cin >> answer;
    while (answer == "y"){
        count << "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 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K