2014-12-09 4 views
0

두 개의 2D 배열로 문제를 해결해야하고 확률 수, 짝수의 합 및 두 배열의 합계와 같은 계산 된 값이 필요합니다. 여러 오류가 발생합니다. 누군가 나를 도울 수 있습니까? 배열을 정의하는 방법이며 함수는 display_odd이 아닙니다. 왜?2 차원 배열 - 함수

#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]); 
} 
+2

해당 C++ 태그에 대해 확실합니까? 나에게 C처럼 보입니다. 이 줄의 – Borgleader

+0

: arr = display_matrix_sum (arr1 [DIM] [DIM], arr2 [DIM] [DIM]); 당신은 int 값을 (display_matrix_sum에서 반환 된) int [] [] 변수에 삽입하려고했습니다. arr이 2 차원 배열로 선언 된 것을 보았습니다. – Ariela

+0

'arr1 [DIM] [DIM]'은'int' (또는 그 인덱스가 유효한 경우)입니다. 배열은 "arr1"이라 불리우므로 함수에 전달해야합니다. – molbdnilo

답변

-1

당신이 배열을 선언하거나 배열의 특정 멤버를 참조 할 때, 당신은 대괄호를 사용하는 경우 : 당신이 인수로 배열을 통과 할 때

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

, 당신은 단순히를 사용 배열의 이름.

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

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

그것은 또한 당신이 정의하는 글로벌 배열보다 당신의 기능에 다른 이름을 매개 변수와 지역 변수를 제공하는 데 도움이 될 수 있습니다, 그리고 주요 기능에 지역의 배열로 그 세계 배열을 정의 할 수 있습니다.

가장 큰 문제점은 배열이 포인터 일 뿐이라는 것을 이해하지 못하는 것입니다. 할당 된 힙이 아니면 않는 한 그냥 전달할 수 없습니다. 이를 나타 내기 위해 각 함수를 다시 작성해야합니다. 예를 들어 display_matrix_sum의 서명

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

같아야 의해 호출되어야한다

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; 
+0

배열은 포인터가 아니며 예제는 다차원 배열을 할당하지 않지만 룩업 테이블은 나쁜 습관. [다차원 배열의 올바른 할당] (http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimension-arrays)을 참조하십시오. – Lundin

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

C 함수 배열을 반환 할 수있다. 추가 인수로 대상 배열을 전달할 수 있습니다.

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]); 
}