matrix multiplication with minimum maximum of each line

Asked

Viewed 1,184 times

1

hello I made a program in order to multiply each element of each row of an array by its highest value element, but my program multiplies each one by itself

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

//achar maior menor de cada linha da matriz e e multiplicar cada numero da linha por ele
int main(int argc, char *argv[]) 
{
    int mat[4][4];
    int i, j, aux;

    //le matriz
    for(i=0; i<=3; i++)
    {
        for(j=0; j<=3; j++)
        {
            setlocale(LC_ALL, "Portuguese");
            printf("digite um numero para a posição %d e coluna %d de mat:\n", i, j);
            scanf("%d", &mat[i][j]);
        }
    }

    //1ºfase de processamento
    for(i=0; i<=3; i++)
    {
        for(j=0; j<=3; j++)
        {
        if(mat[i][j]>mat[i][j] || mat[i][j]==mat[i][j] )//se o elemento na posição mat={i,j} for o maior numero da linha
            {
                aux=mat[i][j];//guardar em aux
                mat[i][j]=mat[i][j]*aux;
            }
        }
    }

    system("cls");
    fflush(stdin);

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


return 0;
}
  • 1

    Hello Leonardo, I read some of your previous questions and I had the impression that you are trying to solve a series of exercises with C in Windows environment (correct me if I’m wrong). I would just like to leave a comment: Have you figured out how to use the GDB? The Debugger can help you a lot. What’s more, most IDES (including Dev-C++ that college teachers like to recommend) support GDB.

  • yes windows use, thanks @Anthonyaccioly I to in 2 period of computer science I get the questions here why staff respond and faculty teachers will not look for GDB

1 answer

2


Create an auxiliary function to calculate the largest element of a vector:

int maxElement(int array[], int arraySize)
{
    int max = array[0];
    int i;
    for (i = 1; i < arraySize; i++)
    {
        if (array[i] > max)
            max = array[i];
    }

    return max;
}

Then adapt your logic to use this function:

//1º fase de processamento
for(i = 0; i < 4; i++)
{
    // Calcula o valor do maior elemento da linha
    int aux = maxElement(mat[i], 4);
    for(j = 0; j < 4; j++)
    {
        // Mesma coisa do que mat[i][j] = mat[i][j] * aux
        mat[i][j] *= aux;      
    }
}

Functional example in Ideone

  • still not intendi I would not like to use function even already knowing because the teacher will complain and ask me to explciar function and I am not sharp in this subject and I do not intend your algorithm mine is not so far away so it only multiplies or each number by itself or only the largest number of each line my last algorithm

  • //fase de processamento&#xA; for(i=0; i<=3; i++)&#xA; {&#xA; for(j=0; j<=3; j++)&#xA; {&#xA;&#xA; if(mat[i][j]>=mat[i][3])//se o elemento na posição mat={i,j} &#xA; { //for maior que o ultimo elemento da linha&#xA;&#xA; aux=mat[i][j];//guardar em aux&#xA; mat[i][j]=aux*mat[i][j];//multiplica aux por cada numero da linha&#xA; }&#xA; &#xA; }&#xA; }

  • 1

    Hello Leonardo. The algorithm definition is: For each line (external loop), find the largest element of the line (auxiliary function) and multiply it by each element of the line (internal loop). Your algorithm is skipping the step of calculating the largest element of the line and is making a if unnecessary. I recommend that you spend time debugging this solution in GDB if something is not clear. If you want to turn everything into a single function, transport the body of the function maxElement into the outer loop is trivial, but I wouldn’t do it (that way it gets much cleaner).

  • // Calculates the value of the largest element of the line int aux = maxElement(mat[i], 4); why the ,4

  • 1

    It is the size of the array. An array is an array of arrays. Its array is a 4-line array, where each row is a 4-position array (logo int [4][4]). The 4 is passed as argument (parameter int arraySize). In C it is common practice to pass the size of the vector along with it.

Browser other questions tagged

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