如何将2d整数数组传递给C中的函数?

时间:2017-06-21 11:32:45

标签: c arrays multidimensional-array

我正在尝试创建一个程序,该程序将根据用户设置的边界创建随机数组 我的初始代码是有效的,因为一切都在主要,但我不知道如何使用函数。任何帮助非常感谢,也在这里首先发布,所以抱歉可怜的问题礼仪。

初始代码:

#include <stdio.h>
#include <stdlib.h>
/* function to print the array */
void fnDisplayArray(int arr[][6])
{
    printf("The content of the array: \n");
    int nRows , nCollumns;
    for(nRows = 0; nRows<5; nRows++)
    {
        for(nCollumns = 0; nCollumns<6; nCollumns++)
        {
            printf("%4d  ",arr[nRows][nCollumns]);
        }
        printf("\n");
    }
}
/* function to populate the array */
void fnPopArray(int arr[][6], int low, int high)
{

    int nRows , nCollumns;

    for(nRows = 0; nRows<5; nRows++)
    {
        for(nCollumns = 0; nCollumns<6; nCollumns++)
        {

            arr[nRows][nCollumns] = low+ (rand()%(high-low));
        }
    }

}

/* function to find minimum value in array */
int fnMinimum(int arr[][6])
{
    int min = 10000;
    int nRows = 0, nCollumns = 0;

    for(nRows = 0; nRows<5; nRows++)
    {
        for(nCollumns = 0; nCollumns<6; nCollumns++)
        {
            if (arr[nRows][nCollumns]<min)
            min = arr[nRows][nCollumns];
        }
    }
    return min;
}
/* function to find maximum value in array */
int fnMaximum(int arr[][6])
{
    int max = -1;
    int nRows = 0, nCollumns = 0;

    for(nRows = 0; nRows<5; nRows++)
    {
        for(nCollumns = 0; nCollumns<6; nCollumns++)
        {
            if(arr[nRows][nCollumns]>max)
            max = arr[nRows][nCollumns];
        }
    }
    return max;
}

然而,当我尝试用函数执行此操作时,只有他们输入的第一个被传递/使用

功能尝试:

#include <stdio.h>
#include <stdlib.h>

// Other Inclusions
//      e.g.    #include "myheader.h"

//  Function Prototypes (if not included within a header file)
int * fnPopArray(int [7][9], int *, int *);
void fnSwap(int *, int *);

//  Main
int main(void)
{
    // Variable Declarations
    int low = 0;
    int high = 0;
    int arn2Darr [7][9];
    int nRows = 0, nCollumns = 0;

    // Code starts here ......

    printf("enter 2 numbers from which the array will be generated \n");
    scanf("%d %d", &low, &high);

    printf("selected numbers are %d and %d\n", low, high);

    if (low > high)
    {
        fnSwap(&low, &high);
        printf("new are %d, %d respectively.", low, high);
    }

    fnPopArray(arn2Darr, &low, &high);


}

// functions //

int * fnPopArray(arn2Darr[7][9], *low, *high)
{

    int nRows , nCollumns;

    for(nRows = 0; nRows<7; nRows++)
    {
        for(nCollumns = 0; nCollumns<9; nCollumns++)
        {

            arn2Darr[nRows][nCollumns] = *low+ (rand()%(*high-*low));
        }
    }

}

void fnSwap(int *x, int *y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;

1 个答案:

答案 0 :(得分:0)

目前尚不清楚它是编译问题还是逻辑问题。请具体说明这个问题。如果我理解你的问题,你就有编译问题。在您的新修订代码函数定义不正确,我已更正您的代码。请检查以下代码,如果您希望得到不同的答案,请告诉我

// functions //

int * fnPopArray(int arn2Darr[m][n],int *low, int *high)
{

    int nRows , nCollumns;

    for(nRows = 0; nRows<m; nRows++)
    {
        for(nCollumns = 0; nCollumns<n; nCollumns++)
        {

            arn2Darr[nRows][nCollumns] = *low+ (rand()%(*high-*low));
        printf("%d ", arn2Darr[nRows][nCollumns]);

        }
    }   
}