Function to multiply each row of a matrix by the value of its corresponding secondary diagonal

Asked

Viewed 489 times

0

I’m not getting the values of the secondary diagonal of a matrix by a function.

/*
19) Elabore uma sub-rotina que receba como parâmetro uma matriz A(6,6) e multiplique cada linha pelo
elemento da diagonal principal da linha. A sub-rotina deverá retornar a matriz alterada para ser mos-
trada no programa principal.
 */

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

#define DIM 6

//Funções
void Prencher_Matriz(int mat[][DIM]);
void Imprimir_Matriz(int mat[][DIM]);

void Armazenar_Diagonal_Secundaria(int mat[][DIM]);
void MultiplicacaoPelaDiagPrincipal(int mat[][DIM]);

int main(int argc, char** argv) {
    int i, j;
    int matriz[DIM][DIM];

    Prencher_Matriz(&matriz);
    Imprimir_Matriz(&matriz);
    Armazenar_Diagonal_Secundaria(&matriz);

    return (EXIT_SUCCESS);
}

void Prencher_Matriz(int mat[][DIM]) {
    int i, j;
    srand(time(NULL));

    for (i = 0; i < DIM; i++) {
        for (j = 0; j < DIM; j++) {
            mat[i][j] = (rand() % 10) + 1;
        }
    }
}

void Imprimir_Matriz(int mat[][DIM]) {
    int i, j;

    printf("MATRIZ GERADA \n");
    for (i = 0; i < DIM; i++) {
        for (j = 0; j < DIM; j++) {
            printf("%4d", mat[i][j]);
        }
        printf("\n");
    }
}

void Armazenar_Diagonal_Secundaria(int mat[][DIM]) {
    int i, j, p = 0;
    int ElementosDS[DIM];

    for (i = 0; i < DIM; i++) {
        for (j = 0; j < DIM; j++) {
            if (i + j == DIM - 1) {
                ElementosDS[p] = mat[DIM - i];
                p++;
            }
        }
    }
}

Well, what I figured out is what to do is to store the values of the main diagonal elements in a vector and multiply them all from your line by it. Follow an example:

| 1 | 2 | 3 |

| 4 | 5 | 6 |

| 7 | 8 | 9 |

The main diagonal elements of the matrix above are matrix 1, 5 and 9. According to what was requested the generated matrix would be:

| 1 | 2 | 3 |

| 20 | 25 | 30 |

| 63 | 72 | 81 |

I’m not getting the matrix into function. I want to do a function to store the elements of digonal in a vector and then take this same vector generated in that function and play it in another function to do the calculations on top of the original matrix.

  • 1

    You in the title say it’s to multiply by the element of the secondary diagonal, but in the code comment it says it’s by the main diagonal. Which diagonal is to multiply after all ?

  • Beyond what @Isac said, what do you mean by "multiply each line"? Do you want to multiply the sum of the elements of each line by the diagonal? This will not return an array(6,6). Or else you want to print each element of a line diagonally?

  • I ended up getting confused, is the main diagonal same.

  • From what I understand it is to take the element of that diagonal and multiply by all of that line. Ex:

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.