How do I create a function for adding two matrices? What will it return?

Val

So for my homework I have to create a function for adding two matrices, the numbers are given by the user, and I think I have the function somewhat down, I just have no idea what to return because I know you can't return an array, so I tried making a variable to set it equal to but it's not working? Can someone help me out with this?

This is the part I'm stuck on:

int add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols)
{
    int matrix_total = c[0][0];

    for (int i = 0; i < Anum_rows; ++i) {
        for (int j = 0; j < Anum_cols; ++j) {
            c[i][j] = a[i][j] + b[i][j];
        }
        c[i][j] = matrix_total;
    }
    return matrix_total;
}

The last j in the c array is underlined with red also in visual studio.

Stephen Docy

Your code already does what it needs, it stores the resulting matrix in c, which, since arrays are passed as pointers, will still contain the updated values when add_matrix() returns. You just have extra code in the function, which should really not be needed to complete the matrix add operation:

void add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols) {
    for (int i = 0; i < Anum_rows; ++i) {
        for (int j = 0; j < Anum_cols; ++j) {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
}

Good point about Anum_cols. There is no need to pass in the number of columns when it is specified in the argument definition and it could lead to future errors.

#define NUM_COLS 10

void add_matrix(int a[][NUM_COLS], int b[][NUM_COLS], int c[][NUM_COLS], int Anum_rows)
{
    for (int i = 0; i < Anum_rows; ++i) {
        for (int j = 0; j < NUM_COLS; ++j) {
            c[i][j] = a[i][j] + b[i][j];
        }

    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I print return value of a function?

How do I get a function to return different values depending on what is passed through the function?

JavaScript: How do I return two values from a function and call those two variables in another function?

How do I return a &Path from a function?

How do i correctly return in a map function

How do I get the dot product of two matrices using numpy?

In Maxima, how do I get the elementwise (Hadamard) product of two matrices?

What do I return to the main function from my string function?

How do I create a function for two-dimensional array search?

How to create an error function comparing two matrices?

How do I return two functions in a function that renders as two components in react instead of the second function overwriting the first

How do I create a user function to return 'FALSE' if a column value string length is less than 5?

how do I collect the function return in load

How do I fix this problem with multiplying two matrices?

Scheme - How do I return a function?

What do I return for function field value?

How do I return value from this function

How do I return a WeightedChoice from a function?

How do I return a function pointer as const?

How do I a return of a function in percentage?

How do I Multiply two matrices in a TensorFlow custom op?

How do I create a function with a function in Python?

Python: How do I create a diagonal matrix with block matrices as diagonals

How do I create a function to compute and return the mean, maximum, and minimum?

How do I make a function in C for checking if two matrices are same dimensions

How do I create similarity matrices in r?

How do I diagonally divide two matrices

How do I program a function that takes two matrices A and B as input and outputs the product matrix A*B?

How do i return two alerts in JS?