Store values in an arbitrarily sized matrix C++

  • Comp Sci
  • Thread starter B3NR4Y
  • Start date
  • Tags
    C++ Matrix
In summary: With the array method, you learned about arrays. So... please tell me that you also learned about the stats methods in the future!
  • #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]
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
  • #2
You can create a function to increase the array size by one and keep track of the size. You can use pointers.
 
  • #3
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):
[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:
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
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
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.
 
  • #6
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.
 

1. How do I declare an arbitrarily sized matrix in C++?

In C++, an arbitrarily sized matrix can be declared using a vector of vectors. For example, vector> matrix; This will create an empty matrix with no specified size.

2. How do I add values to an arbitrarily sized matrix in C++?

To add values to an arbitrarily sized matrix, you can use the push_back() function. For example, matrix.push_back({1, 2, 3}); will add a row with values 1, 2, and 3 to the end of the matrix.

3. How do I access specific elements in an arbitrarily sized matrix in C++?

To access a specific element in an arbitrarily sized matrix, you can use the at() function. For example, int value = matrix.at(0).at(2); will retrieve the value at the first row and third column of the matrix.

4. Can I resize an arbitrarily sized matrix in C++?

Yes, you can resize an arbitrarily sized matrix using the resize() function. For example, matrix.resize(5); will resize the matrix to have 5 rows. You can also specify the number of columns as a second parameter, matrix.resize(5, 3); will resize the matrix to have 5 rows and 3 columns.

5. How do I delete an arbitrarily sized matrix in C++?

To delete an arbitrarily sized matrix, you can use the clear() function. For example, matrix.clear(); will remove all elements from the matrix and make it empty.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top