二维数组 - 函数

时间:2014-12-09 13:45:32

标签: c++ c arrays visual-studio-2012

我必须解决两个2D数组的问题,并计算诸如赔率数,偶数之和和两个数组的加法之类的事情。我收到多个错误。有人能帮我吗? 这是我定义数组的方式,它还说display_odd不是有效的void函数。为什么呢?

#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 个答案:

答案 0 :(得分: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]);  
}

答案 1 :(得分:-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;