-2

The code is supposed to take inputs to form a 3x3 Matrix and then multiply each term by the diagonal element of that line, but, for some reason that i don't know, it multiplies two times by the diagonal when the column index is bigger than the row index.

#include <stdio.h>

#define R 3

int a[R][R], i, j;

int main(void) {
    for (i = 0; i < R; i++) {
        for (j = 0; j < R; j++) {
            printf("\nInsira o n%i%i ", i, j);
            scanf("%i", &a[i][j]);
        }
    }

    for (i = 0; i < R; i++) {
        for (j = 0; j < R; j++) {
            a[i][j] = a[i][j] * a[i][i];
        }
    }

    for (i = 0; i < R; i++) {
        printf("\n");
        for (j = 0; j < R; j++) {
            printf("%i   ", a[i][j]);
        }
    }
}

input:

9 8 7
6 5 4
3 2 1

output:

81 648 567
30 25 100
3 2 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Saulo
  • 9
  • 1
  • 1
    Have you tried running your code line-by-line in a debugger while monitoring 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 Nov 15 '22 at 22:21
  • 1
    Please note that [it is generally expected that you make a debugging attempt yourself before asking for help on Stack Overflow](https://idownvotedbecau.se/nodebugging/). Questions which do not demonstrate any debugging attempt are usually not well received. – Andreas Wenzel Nov 15 '22 at 22:24
  • 1
    Concentrate on the first line, then ask yourself this: What value is in `a[i][i]` for *all* remaining iterations of the `j` loop after `a[i][j] = a[i][j] * a[i][i];` executes when `i` and `j` are both zero (0) ? And, as far as the post is concerned, it should include your *expected* output in addition to the actual output. – WhozCraig Nov 15 '22 at 22:24
  • 1
    Using `i` and `j` as global variables is appalling — don't do that. They should be local variables defined in `main()`. Since you aren't calling any functions that access `a` by name, you should make the array local to the main program too. You should be checking the return value from `scanf()` specifically, and input functions generally, to ensure you got the data you expect. It's often helpful to print out the matrix (or other input data) that you've just read. If it doesn't contain what you expect, then the calculations won't work very well. – Jonathan Leffler Nov 15 '22 at 22:55
  • Your code is doing what you told it to do and producing the correct answers given what you told it to do. So you need to step back and decide what you were trying to do and fix the algorithm so it _does_ do what you want it to do. It might be that you need to save the diagonal value before you run the inner loop, and multiply by the saved value instead of the current contents of the diagonal. – Jonathan Leffler Nov 15 '22 at 22:59
  • 1
    Saulo, `a[i][j] = a[i][j] * a[i][i];` changes the diagonal multiplier. Yet `a[i][i];` is used as a multiplier in later scaling iterations. When `i==j`, do `a[i][j] = a[i][j] * a[i][i];` in a later loop. – chux - Reinstate Monica Nov 15 '22 at 23:32

1 Answers1

0

The diagonal value for a given row is being changed before that row has been fully multiplied, so once the column goes past the diagonal, the multiplies are using the new value of that diagonal rather than the old value.

You can fix it (and improve the speed) as follows:

for (i = 0; i < R; i++) {
    int tmp = a[i][i];
    for (j = 0; j < R; j++) {
        a[i][j] *= tmp;
    }
}

Also, as mentioned, both i and j should be local variables.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41