for loop issue

#include <iostream>
#include <cmath>
using namespace std;

int main() {

int row;
int column;
double avrg;
double sig;

double matrix[3][3] = {
5,1,3,
8,6,3,
10,15,3};

for(row = 0; row <= 2; row++) {
for (column = 0; column <= 2; column++) {
avrg += matrix[row][column]/3;
/* this is supposed to calculate the average of each row of the array.*/
}
for(row = 0; row <= 2; row++) {
for (column = 0; column <= 2; column++) {
sig += sqrt(pow(matrix[row][column] - avrg,2)/3);
/* for each average obtained, these two loops are supposed to calculate the (first number in the first row - the average of the first row)^2 +
(second number in the first row - the average of the first row)^2 +
(third number in the first row - the average of the first row)^2

and then do this same procedure for the nest two rows /*
}
cout << sig << '\n';
sig = 0;
}
avrg = 0;
}
return 0;
}


the final output should be
1.6329
2.0548
4.9216

but instead, I am getting
2.3094
4.6188
10.9697

If anyone can tell my why this isn't working the way I want it to I would really appreciate it.
closed account (4Gb4jE8b)
repost with code blocks please
This should look cleaner.

#include <iostream>
#include <cmath>
using namespace std;

int main() {

int row;
int column;
double avrg;
double sig;

double matrix[3][3] = {
5,1,3,
8,6,3,
10,15,3};

for(row = 0; row <= 2; row++) {
for (column = 0; column <= 2; column++) {
avrg += matrix[row][column]/3;
}
for(row = 0; row <= 2; row++) {
for (column = 0; column <= 2; column++) {
sig += sqrt(pow(matrix[row][column] - avrg,2)/3);
}
cout << sig << '\n';
sig = 0;
}
avrg = 0;
}
return 0;
}
siegel14159265

Try to place your code between code tags [ code ]code here[ /code ] (spaces have to be removed), it makes code to be highlighted and preserves code format (spaces, tabs, ...)


Your code is too confusing :-(
For example, you are using the variable row as a main function variable in two different for loops. Thus, when you've broken out from the second loop the row is equal to 3 and the fist loop is not going to continue execution (cause` the clause is "row <= 2" and 3 is greater than 2).

I recommend you to make use of internal loop variables, to declare variables in preexecution statement of a for loop.
1
2
3
for(int row =0; row <=2; ++row) {
//code goes here
}

Such a code helps you to avoid confusing loops as you've written.

You, also, using an uninitialized variables avrg and sig. In some situations it could lead to unpredictable things. I believe, using an uninitialized variables is a bad practice. However, it is just my humble opinion.

About your calculation mistakes.

i) As I can see from your 'right output' you are to calculate standard deviation, so you has to calculate average squares then to take a root, while you are calculating something else, the sqrt() call is misplaced.

ii) Moreover, as a result of your confusing loops you are calculating the standard deviation of the second and the third columns using the average of the fist one.

I apologise for any grammar mistakes you may find in my posts. Still, I suppose they are understandable.
Last edited on
Repost of properly indented original code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   int row;
   int column;
   double avrg;
   double sig;

   double matrix[3][3] = {
   {5,1,3},
   {8,6,3},
   {10,15,3}};

   for(row = 0; row <= 2; row++)
   {
      for (column = 0; column <= 2; column++)
      {
         avrg += matrix[row][column]/3;
         /* this is supposed to calculate the average of each row of the array.*/
      }
      for(row = 0; row <= 2; row++)
      {
         for (column = 0; column <= 2; column++)
         {
            sig += sqrt(pow(matrix[row][column] - avrg,2)/3);
            /* for each average obtained, these two loops are supposed to calculate the (first number in the first row - the average of the first row)^2 +
            (second number in the first row - the average of the first row)^2 +
            (third number in the first row - the average of the first row)^2
            and then do this same procedure for the nest two rows */ // You had /* here, instead of */
         }
         cout << sig << '\n';
         sig = 0;
      }
      avrg = 0;
   }
   return 0;
}
Last edited on
Topic archived. No new replies allowed.