Returning Two Dimensional Array from a Function in C

Viewed 58

I dont really know how to properly pass the twodimensional Array to my Main Function from my declared function. The programm needs to calculate the sum of two 3x3 Matrizes, but the Input needs to be made via a function. Can somebody help me? I am relatively new to coding and this program is written in c.

#include <stdio.h>
#include <stdlib.h>
int addierer ()
{ 
int i=3,j=3;
int d[i][j];


printf("\n What are the numbers of the matrix? \n");            // Abfrage der Zahlen der  Matrix
for (i = 0; i < 3; ++i){
for (j = 0; j < 3; ++j) {
    printf("Enter number a%d%d : ", i + 1 ,j + 1);
    scanf("%d", &d[i][j]);
}}
    return (d[i][j]);
}
int main() {
   int i=3,j=3;
   int a[i][j], b[i][j], sum[i][j];
    
   a[i][j]  = addierer();
   b[i][j]  = addierer();


for (i = 0; i < 3; ++i)
for (j = 0; j < 3; ++j) {
    sum[i][j] = a[i][j] + b[i][j];
}     
printf("Sum:  \n");                     //  Summe der beiden Matrizen
for (i = 0; i < 3; ++i){
for (j = 0; j < 3; ++j){
    printf("%d  ", sum[i][j]);
    if (j == 3 - 1) {
    printf("\n\n");
    }}}
    
 return 0;
}
1 Answers

If itis always a 3x3 matrix. You could pack it into a structure and return the struct.

Related