answersLogoWhite

0


Best Answer

We'll assume the matrix elements are doubles, but we can easily adapt the code to cater for any numeric data type.

First we need a (primitive) function that emulates the += operator for two arrays of doubles:

double* add_assign_array (double* a, double* b, size_t sz) {

for (size_t i=0; i<sz; ++i) a[i] += b[i];

return a;

}

Note that we are wholly reliant upon the caller to ensure all arguments are valid. We could test for null pointer arguments, however there's no advantage in doing so when we cannot even guarantee that a and b actually refer to at least sz elements. For efficiency it's better if the caller handles any and all necessary runtime tests and thus keep those tests to a minimum.

With this function in place we can now add two matrices, row by row:

double* add_assign_matrix (double* a, double* b, size_t rows, size_t cols) {

size_t i;

for (size_t row=0; row<rows; ++row) {

i = row * cols;

add_assign_array (a[i], b[i], cols);

}

return a;

}

Example usage:

// Utility functions: void print_array (double*, size_t);

void print_matrix (double*, size_t, size_t);

int main (void) {

const size_t rows = 3;

const size_t cols = 4;

double a[rows][cols] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

double b[rows][cols] = {{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}};

printf ("Matrix a:\n");

print_matrix (a, rows, cols);

printf ("Matrix b:\n");

print_matrix (b, rows, cols);

printf ("Matrix a+=b:\n");

add_assign_matrix (a, b, rows, cols);

print_matrix (a, rows, cols);

return 0;

}

void print_array (double* a, size_t sz) {

for (size_t i=0; i<sz; ++i) printf ("%f\t") a[i];

printf ("\n");

}

void print_matrix (double* a, size_t rows, size_t cols) {

for (size_t row=0; row<rows; ++row) print_array (a[row * cols], cols);

}

Note that the add_assign function emulates a += b rather than c = a + b. However, we can easily emulate this by copying one of the matrices and then calling add_assign upon the copy:

// e.g., c = a + b;

double c[rows][cols]; // uninitialised matrix

memcpy (c, a, rows * cols * sizeof (double)); // c is a copy of a

add_assign_matrix (c, b, rows, cols); // c += b

It's far from intuitive but arrays and matrices are anything but intuitive in C Programming.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C program to add 2 matrices using functions?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program in C using the fprint and fscan functions?

By using those two functions in your code.


How do you develop a JAVA program that computes matrices?

Matrices can't be "computed" as such; only operations like multiplication, transpose, addition, subtraction, etc., can be done. What can be computed are determinants. If you want to write a program that does operations such as these on matrices, I suggest using a two-dimensional array to store the values in the matrices, and use for-loops to iterate through the values.


Write a C program to find sum of 3 matrices?

matrix


How do you write c program to perform sum of elements of matrix using pointers with functions?

i cant write


Write C program using functions to simulate a Calculator with the basic functions - square root and reciprocal?

trytytytytytytytytrf 6 bcvvtgujhy6


Why we write java program using classes?

Classes are well organised functions in java which help discriminate between two different functions.


What is the importance of functions in a c plus plus program?

Functions are very important in C++, as you can't write the simplest program to print hello without using a function. Overall you can say that function are building blocks of a C++ program. Functions can also be defined by the programmer to reduce program size.


Write a program to illustrate the usage of pointers with arrarys and functions?

* * * * * * * * * * write the c++ program and show me brifily?


How do you add two matrices using Linux shell script?

write ashell script to add awo matrix using array.


Write a c program using dynamic memory allocation function calloc to find sum of two matrices and also print the resultant matrix?

printf("%s",per&gt;50?:"pass",per&lt;50?:"fail");


Write an algorithm for multiplication of two sparse matrices?

how to multiply two sparse matrices


Write a sample program using ASPNET explaining all the syntax and semantics of the program?

write a sample program using asp.net explaining all the syntax and semantics of the program