【问题标题】:Two dimensional array - functions二维数组 - 函数
【发布时间】:2014-12-09 13:45:32
【问题描述】:

我必须解决两个二维数组的问题,并计算奇数、偶数之和以及两个数组的加法。我收到多个错误。有人能帮我吗? 这是我定义数组的方式,它还说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]);  
}

【问题讨论】:

  • 你确定那个 C++ 标签吗?在我看来像 C。
  • 在这一行:arr = display_matrix_sum(arr1[DIM][DIM] ,arr2[DIM][DIM]);您尝试将 int 值(从 display_matrix_sum 返回)插入到 int[][] 变量中 - 正如我所看到的 arr 被声明为二维数组。
  • arr1[DIM][DIM] 是一个 int (或者如果这些索引是有效的)。该数组称为“arr1”,因此您应该将其传递给函数。
  • @Borgleader 有一些变量没有在函数的开头声明,所以我认为这不可能是 C

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


【解决方案1】:
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]);  
}

【讨论】:

    【解决方案2】:

    当你声明一个数组,或者当你引用一个数组中的特定成员时,你使用方括号:

    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;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2012-01-26
    • 1970-01-01
    相关资源
    最近更新 更多