0

So while I was checking the values within my arrays, I noticed that my mat2 array is printing right the 1st time, but not for the 2nd time, 3rd time, etc... Also the 2nd, 3rd, etc.. time it prints, they're all the same.

Here's the code to better illustrate:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
    int rows;
    int cols = 2;
    int i, j, k;
    double value;

    printf("Input number of data points: ");
    scanf("%d", &rows);
    printf("\n\n");

    double matA[rows][cols];
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("Input element at [%d][%d]: ", i, j);
            scanf("%lf", &matA[i][j]);
        }
    }

    double input;
    printf("\nInput the x-value you are interpolating: ");
    scanf("%lf", &input);

    double mat1[rows - 1];
    for(i = 0; i < rows; i++) {
        j = 0;
        mat1[i] = (matA[i + 1][j + 1] - matA[i][j + 1]) / (matA[i + 1][j] - matA[i][j]);
        j++;
    }

    printf("\n");

    for(i = 0; i < rows - 1; i++) {
        printf("%.9lf\n", mat1[i]);
    }

    double mat2[rows - 2];
    for(i = 0; i < rows; i++) {
        j = 0;
        mat2[i] = (mat1[i + 1] - mat1[i]) / (matA[i + 2][j] - matA[i][j]);
        j++;
    }

    printf("\n");

        // printing mat2 array for the first time
    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }

    double mat3[rows - 3];
    for(i = 0; i < rows; i++) {
        j = 0;
        mat3[i] = (mat2[i + 1] - mat2[i]) / (matA[i + 3][j] - matA[i][j]);
        j++;
    }

    printf("\n");

    for(i = 0; i < rows - 3; i++) {
        printf("%.9lf\n", mat3[i]);
    }

        // printing mat2 array multiple times after printing it the first time
    printf("\n");

    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }

    printf("\n");

    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }

    printf("\n");

    for(i = 0; i < rows - 2; i++) {
        printf("%.9lf\n", mat2[i]);
    }
        // end of printing mat2 array multiple times
    return 0;
}

So for example, I input the following:

4
8
1
9
4
10
5
11
10
9.2

This is the output that I get (how the code runs):

Input number of data points: 9.2


Input element at [0][0]: 8
Input element at [0][1]: 1
Input element at [1][0]: 9
Input element at [1][1]: 4
Input element at [2][0]: 10
Input element at [2][1]: 5
Input element at [3][0]: 11
Input element at [3][1]: 10

Input the x-value you are interpolating: 9.2

3.000000000
1.000000000
5.000000000

-1.000000000     // this is mat2 printing the first time, which is correct
2.000000000

1.000000000

0.105371901      // this is mat2 printing multiple times after printing it the first time
-0.520661157

0.105371901
-0.520661157

0.105371901
-0.520661157     // end of printing mat2 multiple times after printing it the first time

I wonder what's wrong with the code.

  • 6
    You have undefined behaviour. `double mat2[rows - 2]; for(i = 0; i < rows; i++) {` Your loop goes beyond the bounds of the array. Perhaps a typo since the other loops stop at `rows-2`. – kaylum Mar 13 '20 at 11:04
  • 1
    You define `double mat2[rows - 2]`, then assign `mat2[i]` for every `(i = 0; i < rows; i++)`. That means that you write to two non-existent array entries. – M Oehm Mar 13 '20 at 11:04
  • Well, that was easy. @kaylum @M Oehm thanks – flamethrower10 Mar 13 '20 at 11:12

1 Answers1

1

With the definition of mat2:

double mat2[rows - 2];

and the assignment inside the loop:

for(i = 0; i < rows; i++)    // Note `i < rows` but not `i < (rows - 1)`.
{
...
mat2[i] = (mat1[i + 1] - mat1[i]) / (matA[i + 2][j] - matA[i][j]);
...
}

you attempt to assign a value to an not existent element/beyond the array, because mat2 has rows - 2 elements, but not rows - 1 elements but the loop got walk through for rows times.

Also note that you trying to read values beyond the borders of mat1 inside the assignment expression at the last iteration, since mat1 has only rows - 1 elements, but not rows elements:

mat1[i + 1]   // mat1 has only `rows - 1` elements. 

It is the same for the two-dimensional array matA, but here is the issue with a dimension instead of an element:

matA[i + 2][j]  // matA has only `rows - 1` dimensions, not `rows + 1`.

Writing or reading beyond the bounds of an array means the behavior is undefined.

The same goes for the other mat arrays and assignments in its respective loops with increasingly more "out of the bounds" violations, because the loop condition of i < rows inside the loop for assigning values to the elements of the array keeps constant, although the array sizes changing decreasingly.

  • thank you, that clarifies it. :) Follow up question: my code above doesn't create a number of array depending on user input on variable `rows`. Is it possible to achieve this using a for loop? What I'm trying to achieve is a more efficient way of assigning new arrays every time the loop iterates until the number of `rows` inputted by the user. – flamethrower10 Mar 13 '20 at 12:56
  • @PauloVictorio The problem is: I don´t really understand the algorithm nor the original task behind, so that I can help you out efficiently. In this question you asked for the reason why the code doesn´t work as expected. I gave you the reason, since I do not need to understand the specific concept of a program to find issues in it. My advice is: Ask another question (do **not** edit this one) which especially focuses on to finding a solution for the specific problem and by this show the exact task order (what is the thing you want to accomplish) and what you have tried so far to achieve it. – RobertS supports Monica Cellio Mar 13 '20 at 13:22
  • I just posted another question regarding that with my code in another thread: https://codereview.stackexchange.com/questions/238827/how-can-i-make-my-code-more-reusable-for-different-data-set – flamethrower10 Mar 13 '20 at 13:24
  • @PauloVictorio The more focus the better. You also can split parts of the algorithm and ask separate questions for it, which might bring you better to your target. Stack Overflow questions which are clearly focused are preferred by all users of the community. – RobertS supports Monica Cellio Mar 13 '20 at 13:24