Reputation: 3385
I'm new to coding and am currently learning C at school. I had a question regarding how to add matrices in C using a function. I'm facing some difficulties and was hoping to get some words of advice here. The conditions that my instructor gave the class are 1) two 5 x 6 matrices with integer entries from 1 - 100, 2) define and use your own function. Here's the code I've written so far:
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COLUMN 6
size_t addMatrices(int a[][COLUMN], int b[][COLUMN]);
void printArray(int a[][COLUMN]);
int main(void) {
int row, column;
int matrix1[ROW][COLUMN] = { {0}, {0} };
int matrix2[ROW][COLUMN] = { {0}, {0} };
for (row = 0; row < ROW; row++) {
for (column = 0; column < COLUMN; column++) {
matrix1[row][column] = 1 + (rand() % 100);
matrix2[row][column] = 1 + (rand() % 100);
}
}
printf("matrix1:\n");
printArray(matrix1);
printf("\n\nmatrix2:\n");
printArray(matrix2);
printf("\n\nresult:\n");
addMatrices(matrix1, matrix2);
printfArray(result);
printf("\n");
return 0;
}
void printArray(int a[][COLUMN]) {
int row, column;
for (row = 0; row < ROW; row++) {
for (column = 0; column < COLUMN; column++) {
printf("%d ", a[row][column]);
}
printf("\n");
}
}
size_t addMatrices(int a[][COLUMN], int b[][COLUMN]) {
int result[ROW][COLUMN] = { {0}, {0} };
int row, column;
for (row = 0; row < ROW; row++) {
for (column = 0; column < COLUMN; column++) {
result[row][column] = a[row][column] + b[row][column];
}
}
return result;
}
If you look at the body of the main method, the compiler says that there's an error because the variable "result" isn't defined when being passed to the function printArray(). I understand the concept of why this error occurs (regarding local variables and passing parameters), but how can I solve this problem?
Aside from this, any other words of advice or suggestions are greatly appreciated.
Thank you!
Upvotes: 0
Views: 162
Reputation: 84521
Continuing from the comment, you cannot return a locally declared array from a function because the memory for the locally declared array is created on the function stack which is destroyed on function return. Either declare result
in main
and pass as a parameter to addMatrices
or dynamically allocate storage for result
in addMatrices
(so it will survive return) and return a pointer to the beginning of the block of memory and assign it to a pointer in main
. Declaring result
in main
and passing it as a parameter is the easiest thing to do.
Putting those pieces together, you could modify your code as follows:
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COLUMN 6
void addMatrices (int (*a)[COLUMN], int (*b)[COLUMN], int (*res)[COLUMN]);
void printArray (int (*a)[COLUMN]);
int main(void) {
int row, column;
int matrix1[ROW][COLUMN] = {{0}};
int matrix2[ROW][COLUMN] = {{0}};
int result[ROW][COLUMN] = {{0}};
for (row = 0; row < ROW; row++) {
for (column = 0; column < COLUMN; column++) {
matrix1[row][column] = 1 + (rand() % 100);
matrix2[row][column] = 1 + (rand() % 100);
}
}
printf ("matrix1:\n");
printArray (matrix1);
printf ("\n\nmatrix2:\n");
printArray (matrix2);
printf ("\n\nresult:\n");
addMatrices (matrix1, matrix2, result);
printArray (result);
putchar ('\n'); /* don't use printf to print a single character :) */
return 0;
}
void printArray(int a[][COLUMN]) {
int row, column;
for (row = 0; row < ROW; row++) {
for (column = 0; column < COLUMN; column++) {
printf(" %3d", a[row][column]);
}
putchar ('\n');
}
}
void addMatrices (int (*a)[COLUMN], int (*b)[COLUMN], int (*res)[COLUMN])
{
int row, column;
if (!a || !b || !res) {
fprintf (stderr, "error: invalid parameter.\n");
return;
}
for (row = 0; row < ROW; row++) {
for (column = 0; column < COLUMN; column++) {
res[row][column] = a[row][column] + b[row][column];
}
}
}
Example Use/Output
$ ./bin/addmtrx
matrix1:
84 78 94 87 50 63
91 64 41 73 12 68
83 63 68 30 23 70
94 12 30 22 85 99
16 14 92 57 63 97
matrix2:
87 16 36 93 22 28
60 27 27 37 69 30
31 24 36 3 59 68
57 43 74 20 38 25
71 27 81 74 71 82
result:
171 94 130 180 72 91
151 91 68 110 81 98
114 87 104 33 82 138
151 55 104 42 123 124
87 41 173 131 134 179
note: C coding style generally favors all lower-case variable and function names, reserving all upper-case for constants and macros. Leave the MixedCase and camelCase names for java and C++. (it is style, so it is completely up to you, but...)
Upvotes: 2