简体   繁体   English

如何在C中为矩阵使用相同的用户定义函数?

[英]How can I use the same user defined function for matrix in C?

I need to use the same user defined function in C, for adding and subtracting matrices in C. 我需要在C中使用相同的用户定义函数,以便在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 UPDATE

dasblinkenlight Here's what I end up doing with your suggestions: dasblinkenlight以下是我对您的建议的最终看法:

Declared in main function int add =1; 在主函数中声明int add = 1; int sub = -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");
    }
}

Consider: 考虑:

  • Subtraction is an addition of an additive inverse 减法是加法逆的加法
  • The additive inverse of a number is the number multiplied by -1 . 数字的加法逆是数字乘以-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 : 现在,您的两个函数都可以重写为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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个 function 中使用 main 中定义的变量? C - How can I use a variable defined in main in another function? C 用户定义的矩阵 - User defined matrix in c 如何正确定义和使用返回类型为用户定义结构的函数? (在C中) - How do I properly define and use a function whose return type is a user defined structure? (In C) 如何让用户输入用 c 填充矩阵? - How can i get user input to fill a matrix with c? 如何在 function 中初始化可变长度矩阵,然后在不使用 malloc 或 C 中的 calloc 的情况下返回相同的矩阵 - How to initialize a variable length matrix inside a function and then return the same matrix without the use of malloc or calloc in C 如何从用户那里读取数据并在我将在 BST 中插入节点的同一 function 中使用它们 - How can I read the data from the user and use them in the same function that I'll insert nodes in the BST 无法在C中打印用户定义的函数 - Can't Print User Defined Function in C 如何使用malloc在C中创建矩阵并避免内存问题? 我如何使用C99语法将矩阵传递给函数? - How may I create a matrix in C using malloc and avoiding memory problems? How I can use C99 syntax to pass the matrix to a function? 如何在 void function 中创建矩阵并作为参数 C 返回 - How can I create a matrix in a void function and return as parameter C 如何在 C 中为外部定义的函数创建别名? - How I can make alias on external defined function in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM