0

I need to use the same user defined function in C, for adding and subtracting matrices in C.

This should be the code I need to declare

void matrixAddSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums);

right now I'm declaring two functions

void matrixAdd(int arrayone[10][10], int arraytwo[10][10], int rows, int colums);
void matrixSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums);

When I debug my code it does the work but I need to reduce the amount of code that I have type. Here's my code definition.

void matrixAdd(int arrayone[10][10], int arraytwo[10][10], int rows, int colums){
    int i, j;
    int sumM[10][10];

    for (i = 0; i < rows; i++){
        for (j = 0; j < colums; j++){
            sumM[i][j] = arrayone[i][j] + arraytwo[i][j];
            printf("\t%d", sumM[i][j]);
        }
        printf("\n");
    }
}

void matrixSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums){
    int i, j;
    int sumM[10][10];

    for (i = 0; i < rows; i++){
        for (j = 0; j < colums; j++){
            sumM[i][j] = arrayone[i][j] - arraytwo[i][j];
            printf("\t%d", sumM[i][j]);
        }
        printf("\n");
    }
}

UPDATE

dasblinkenlight Here's what I end up doing with your suggestions:

Declared in main function int add =1; int sub = -1;

//user defined functions declaration
void matrixAddSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums, int mul);
//user defined function definition
void matrixAddSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums, int mul){
    int i, j;
    int sumM[10][10];
    int scaM[10][10];
    for (i = 0; i < rows; i++){
        for (j = 0; j < colums; j++){
            scaM[i][j] = mul * arraytwo[i][j];
            }
        }


    for (i = 0; i < rows; i++){
        for (j = 0; j < colums; j++){
            sumM[i][j] = arrayone[i][j] + scaM[i][j];
            printf("\t%d", sumM[i][j]);
        }
        printf("\n");
    }
}
max
  • 1
  • 1
  • This is not a code review site. Try https://codereview.stackexchange.com – Mad Physicist Mar 24 '18 at 15:48
  • What is your actual concern anyway, I'm not sure I understand. – Mad Physicist Mar 24 '18 at 15:48
  • Your matrices should be some abstract data type like [here](https://stackoverflow.com/a/41410503/841108) – Basile Starynkevitch Mar 24 '18 at 15:53
  • Mad Physicist my concern is that I'm not supposed to have one UDF for adding and another one for subtraction and I wanted to know how to create a single one that will do both things. I'll check to https://codereview.stackexchange.com, thanks for the advise – max Mar 24 '18 at 17:29

1 Answers1

3

Consider:

  • Subtraction is an addition of an additive inverse
  • The additive inverse of a number is the number multiplied by -1.

Use these two facts to unify addition and subtraction. Write the following function:

static void matrixAddOrSubtract(
    int arrayone[10][10]
,   int arraytwo[10][10]
,   size_t rows
,   size_t columns
,   int mul); // Multiply arraytwo[i][j] by mul before adding to arrayone[i][j]

Now both of your functions could be re-written as single-line invocations of matrixAddOrSubtract:

void matrixAdd(int arrayone[10][10], int arraytwo[10][10], size_t rows, size_t columns) {
    matrixAddOrSubtract(arrayone, arraytwo, rows, columns, 1);
}
void matrixSub(int arrayone[10][10], int arraytwo[10][10], size_t rows, size_t columns) {
    matrixAddOrSubtract(arrayone, arraytwo, rows, columns, -1);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • dasblinkenlight I appreciate the solution, I still haven't learn how to use static function. I will have to do some research. – max Mar 24 '18 at 17:06
  • @max Static function is there to hide it from use in other translation units. Other than that, it could be non-static. – Sergey Kalinichenko Mar 24 '18 at 17:23