1

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;
}
Ben Heuser
  • 11
  • 1
  • You would either have to allocate memory for the array in the function, or pass it a pre-allocated array to populate. For example `void addierer(int m, int n, int d[m][n])`. Or `void addierer(int d[][3])` since you have effectively hard coded the size. – Weather Vane Oct 17 '21 at 15:36

1 Answers1

0

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

notan
  • 359
  • 1
  • 10