0

I'm supposed to multiply a certain line (I specify what line exactly in the 4th argument of the function) of a given matrix by a number.

The main function:

int main_tp05(int argc, const char *argv[]){
    int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}};
    multiply_matrixNx100_line_by_scalar(mNx100,3,3,1,2);
    return  0;
}

I've tried to solve it like so:

void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns, int line, int scalar){
    for (int i = 0; i < lines; i++) {
        for (int j = 0; j < columns; j++) {
            if(i == line){
                printf("%d\n", mNx100[i*scalar][j] );
            }
        }
        printf("\n");
    }
}

To note that:

1- I can´t change the parameters.
2- MAXCOLS100 is a macro on the .h file. I put it with the value of 3.
3- The scalar is the number I want to multiply the line by. 
6
  • What have you tried and what isnt working ? Commented Oct 22, 2019 at 22:59
  • I've tried the method that is in this question, with some minor tweeks but nothing seems to work to notice some worthy progress.
    – Diogo Cruz
    Commented Oct 22, 2019 at 23:01
  • 1
    Try mNx100[i*scalar][j] --> mNx100[scalar][j]*i
    – chux
    Commented Oct 22, 2019 at 23:36
  • @chux it doesn't work
    – Diogo Cruz
    Commented Oct 23, 2019 at 11:04
  • 1
    mNx100[i*scalar][j] --> mNx100[i][j]*scalar;
    – thomachan
    Commented Oct 24, 2019 at 8:46

2 Answers 2

0

I WROTE THIS CODING AND VERIFIED BY MYSELF IT WORKS SUCCESSFULLY.

    #include <stdio.h>
    #include <stdlib.h>
    int main() 
    {
        int i,j,a[5][5],b,c,d,m,n,r;
        printf("Enter the number of rows and columns\n");
        scanf("%d %d",&m,&n);
        printf("Enter the matrix\n");
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        printf("Enter the number to multiply the matrix\n");
        scanf("%d",&b);
        printf("Enter 0 to multiply row or 1 to multiply column\n");
        scanf("%d",&d);
        if(d==0)
        {
            printf("Enter the row number to be multiplied\n");
            scanf("%d",&r);
            for(i=r,j=1;j<=n;j++)
            {
                a[i][j]*=b;
            }
        }
        else if(d==1)
        {
            printf("Enter the row number to be multiplied\n");
            scanf("%d",&c);
            for(j=c,i=1;i<=m;i++)
            {
                a[i][j]*=b;
            }
        }
        printf("matrix after multiplied\n");
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                printf("%d  ",a[i][j]);
            }
            printf("\n");
        }
        return 0;
    }

OUTPUT OF THIS CODING

0

I'm supposed to multiply a certain line (I specify what line exactly in the 4th argument of the function) of a given matrix by a number

Usually, "multiply a row of a matrix by a scalar" means to multiply every element of the row by a certain scalar value. That's not what the posted function does, it multiplies the index of the row by the passed argument:

void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
                                         int line, int scalar) {
    for (int i = 0; i < lines; i++) {        // <-- Useless, the row is known
        for (int j = 0; j < columns; j++) {
            if(i == line){
                printf("%d\n", mNx100[i*scalar][j] );
                //                    ^^^^^^^^
            }
        }
        printf("\n");
    }
}

If the intent is to only print the modified row, the previous function could be rewritten as

void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
                                         int line, int scalar) {
    if (line < 0  ||  line >= lines)
        return;
    for (int j = 0; j < columns; j++) {
        printf("%d\n", mNx100[line][j] * scalar);
    }
    printf("\n");
}

If instead the function is supposed to only modify the matrix, without printing it, we could use

mNx100[line][j] *= scalar;

Inside the loop, in place of the call to printf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.