-2

i've been working on an assignment to create a certain stochastic test, everything was going great until I had to make a loop tp represent a double summation

int A[][6] = { {4529,9045,13568,18091,22615,2789},
                {9045,18097,27139,36187,45234,55789},
                {13568,27139,40721,54281,67825,83658},
                {18091,36187,54281,72414,90470,111580},
                {22615,45234,67852,90470,113262,139476},
                {27892,55789,83658,111580,139476,172860} };
    float B[] = { 1 / 6,5 / 24, 11/120, 19/720, 29/5040, 1/840 };
   
    float R = 0, temp1 = 0, temp2, temp3;
    for (int i = 0; i < 6; i++) {
        for (int j = 0; i < 6; j++) {
            temp2 = r[i] - (n * B[i]);
            temp3 = r[j] - (n * B[j]);
            temp1 += (A[i][j]*temp2*temp3);
            
        }
        
    }
R = temp1 / n;
    cout << "R= " << R;

r is an array of length 6 I defined previously in the program

the last 2 lines produce nothing even though there aren't any error, I tried to replace them with merely couting anything still nothing

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • What do you mean by "producing nothing"? – Vlad from Moscow Apr 05 '23 at 21:42
  • 3
    `float B[] = { 1 / 6,5 / 24, 11/120, 19/720, 29/5040, 1/840 };` Pretty sure you just made an array of all zeroes. – ChrisMM Apr 05 '23 at 21:44
  • 3
    What is the value of `n`? Where is `r` defined? – Nathan Pierson Apr 05 '23 at 21:46
  • 2
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel Apr 05 '23 at 21:51
  • 4
    `for (int j = 0; i < 6; j++) {` when does this loop stop? – 463035818_is_not_an_ai Apr 05 '23 at 21:53
  • 3
    Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Apr 05 '23 at 21:53
  • Consider what @463035818_is_not_a_number posted, your loop is hung. Set a break point at your last line, do you ever get there? – lakeweb Apr 05 '23 at 21:56
  • Suggestion: `temp2` and `temp3` should be locally scoped to the inner loop. – Chris Apr 05 '23 at 22:11

3 Answers3

3

The array B

float B[] = { 1 / 6,5 / 24, 11/120, 19/720, 29/5040, 1/840 };

is initialized by zeroes because in initialing expressions there are used the integer arithmetic.

You should write for example

float B[] = { 1.0f / 6,5.0f / 24, 11.0f/120, 19.0f/720, 29.0f/5040, 1.0f/840 };

Also as @Oded Ben Dov pointed out there is a typo

for (int j = 0; i < 6; j++) {
                ^^^^^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1
// Define your floats as this way
float B[] = { 1.f/6, 5.f/24, 11.f/120, 19.f/720, 29.f/5040, 1.f/840};

...

for (int i = 0; i < 6; i++) 
{
    //use j instead of i to prevent infinite loop
    for (int j = 0; j < 6; j++) {
        temp2 = r[i] - (n * B[i]);
        temp3 = r[j] - (n * B[j]);
        temp1 += (A[i][j]*temp2*temp3);     
    }     
}
Shift Delete
  • 1,015
  • 6
  • 13
1

You're stuck in an infinite loop in the 2nd for loop because of a typo

for (int j = 0; i < 6; j++) {...}

should be

for (int j = 0; j < 6; j++) {

So the code never reaches those last two lines

Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53