0

I have to solve a problem with two 2D arrays and calculated things like odds number, sum of even number and addition of the two array. I get multiple errors. Can someone help me? It is how I define my arrays, and it also says display_odd is not a valid void function. Why?

#define DIM 50

#include <stdio.h>
#include <conio.h>

void display_odd(int[][DIM]);
int display_even_sum(int[][DIM], int[][DIM]);
int display_matrix_sum(int[DIM][DIM], int[DIM][DIM]);

void main(){
int x1, x2, y1, y2, x, y, arr1[DIM][DIM], arr2[DIM][DIM], arr[DIM][DIM];

printf("How large do you want the first matrix to be? ('x y') \n");
scanf("%d %d", &x1, &y1);
for (int i = 0; i < x1; i++){
    for (int j = 0; j < y1; j++){
        printf("A[%d][%d]= ", i + 1, j + 1);
        scanf("%d", &arr1[i][j]);
    }
}

printf("How large do you want the second matrix to be? ('x y') \n");
scanf("%d %d", &x2, &y2);
for (int i = 0; i < x2; i++){
    for (int j = 0; j < y2; j++){
        printf("B[%d][%d]= ", i + 1, j + 1);
        scanf("%d", &arr2[i][j]);
    }
}

if (x1 > x2)
    x = x1;
else
    x = x2;
if (y1 > y2)
    y = y1;
else
    y = y2;

//printf("\nThe odd numbers in matrix A are : \n");
//void display_odd(arr1[DIM][DIM]);
//printf("\nThe odd numbers in matrix B are : \n");
//void display_odd(arr2[DIM][DIM]);
printf("\nThe sum of all even elements is : ");
printf("\nThe sum of the initial matrixes is : \n");
arr = display_matrix_sum(arr1[DIM][DIM] ,arr2[DIM][DIM]);
for (int i = 0; i < DIM; i++){
    printf("\n");
    for (int j = 0; j < DIM; j++)
        printf(" %d", arr[i][j]);
}

_getch(); //Wait for it
}

void display_odd(int arr[][DIM]){
for (int i = 0; i < DIM; i++)
    for (int j = 0; j < DIM; j++)
        if (arr[i][j] % 2 == 1)
            printf("[%d][%d]", i, j);
}

int display_even_sum(int arr1[DIM][DIM],int arr2[DIM][DIM]){
int s = 0;
for (int i = 0; i < DIM; i++)
    for (int j = 0; j < DIM; j++)
        if (arr1[i][j] % 2 == 0)
            s += arr1[i][j];
for (int i = 0; i < DIM; i++)
    for (int j = 0; j < DIM; j++)
        if (arr2[i][j] % 2 == 0)
            s += arr2[i][j];
return(s);
}

int display_matrix_sum(int arr1[][DIM],int arr2[][DIM]){
int arr[DIM][DIM];
for (int i = 0; i < DIM; i++)
    for (int j = 0; j < DIM; j++)
        arr[i][j] = arr1[i][j] + arr2[i][j];
return(arr[DIM][DIM]);  
}
Freelancer
  • 836
  • 1
  • 14
  • 47
  • 2
    Are you sure about that C++ tag? Looks like C to me. – Borgleader Dec 09 '14 at 13:46
  • in this line: arr = display_matrix_sum(arr1[DIM][DIM] ,arr2[DIM][DIM]); you try to insert int value (returned from display_matrix_sum) to int[][] variable-as I saw arr is declared as Two dimensional array. – Rachel Fishbein Dec 09 '14 at 13:57
  • `arr1[DIM][DIM]` is an `int` (or would be if those indices were valid). The array is called "arr1", so that's what you should pass to the functions. – molbdnilo Dec 09 '14 at 13:57
  • @Borgleader there are variables that aren't declared at the beginning of the functions so I think that can't be C – pascx64 Dec 09 '14 at 14:24

2 Answers2

0
arr = display_matrix_sum(arr1[DIM][DIM] ,arr2[DIM][DIM]);

A C function cannot return an array. You can pass the destination array as an additional argument.

void display_matrix_sum();
…
display_matrix_sum(arr1, arr2, arr);
…
void display_matrix_sum(int arr1[][DIM], int arr2[][DIM], int arr[][DIM])
{
…
// remove this: return(arr[DIM][DIM]);  
}
Armali
  • 18,255
  • 14
  • 57
  • 171
-1

When you declare an array, or when you reference a specific member in an array, you use the square brackets:

int array[2][3];
array[1][0] = 6;
int x = array[1][0]; // x is now 6

when you pass an array as an argument, you simply use the name of the array;

someFunction(array); // passes the array
anotherFunction(array[1][0]); // passes 6;

return array; // returns the array;
return array[1][0]; // returns 6;

It may also help to give the parameters and local variables in your functions different names than the global arrays you define, and to define those global arrays as local arrays in your main function.

The biggest problem is that you don't seem to understand that arrays are just pointers. you can't just pass them around unless they are heap allocated. Each function should be rewritten to show this. For example display_matrix_sum's signature should look like

int** display_matrix_sum(int** A1, int** A2, int rows, int cols);

and should be called by:

int cols = 4;  // or whatever
int rows = 4;  // or whatever
int** arr1 = new int*[rows]; // arr1 is now any an array of int arrays
int** arr2 = new int*[rows]; // arrays work by pointing to the first element
for(int i = 0; i < rows; i++)
{
    arr1[i] = new int[cols]; // each element in arr1 becomes an int array
    arr2[i] = new int[cols];
}

/* fill in the values of the arrays */

int** out = display_matrix_sum( arr1, arr2, rows, cols, out);
// output now contains the result of the function

/* when you are done with arr1 and arr2 and out*/

for(int i = 0; i < rows; i++)
{
    delete[] arr1[i];
    delete[] arr2[i];
    delete[] out[i];
}
delete[] arr1;
delete[] arr2;
delete[] out;
dconman
  • 90
  • 3
  • Arrays are _not_ pointers and your example does not allocate multi-dimensional arrays, but look-up tables, which is bad practice. See [Correctly allocating multi-dimensional arrays](http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Mar 28 '17 at 10:48