c++ - Having trouble formatting code to solve for Standard Deviation of a 2D array -
most of code program completed. need standard deviation. when run program, compiles , gives me values, think std value not correct. don't think using arr[rows][cols]
best way go it, used anyway show of values in array. think giving me incorrect output. how fix it? use different way show each value, how? loop should run 24 times. each time use number[0]
- average , keep , add number[1]
- average way till hits max amount of numbers in array.
here formula standard deviation.
- s standard deviation.
- m mean or average. -----> have double average m
n number of values in array. -------> have max_values equal n.
* x each value in array.
89 93 23 89 78 99 95 21 87 92 90 89 94 88 65 44 89 91 77 92 97 68 74 82
//to calculate standard deviation of 2-d array double getstandarddeviation(int arr[rows][cols], double &std, double average, int num, const int max_values) { double devsum = 0; //sum of every value in array minus average squared (int x = 0; x < max_values; x++) { devsum += pow((arr[rows][cols] - average), 2); } std = sqrt(devsum / (max_values - 1)); return std; } //end of getstandarddeviation method
the error in getstandarddeviation
using wrong indices, row
, col
, every value of x
.
instead of
for (int x = 0; x < max_values; x++) { devsum += pow((arr[rows][cols] - average), 2); // ^^^^^^^^^^^ wrong indices }
use
double square(double x) { return x*x; } ( int r = 0; r < rows; ++r ) { ( int c = 0; c < cols; ++c ) { // use of pow(..., 2) not necessary. // devsum += pow((arr[r][c] - average), 2); // use more efficient method. devsum += square(arr[r][c] - average); } }
Comments
Post a Comment