2D数组函数仅传递1个值

时间:2017-11-07 19:16:19

标签: c arrays function multidimensional-array

我有一个程序,它接收具有权重值的人的金字塔,并且应该使用递归函数来增加该人支持的两个人的权重(如果该人在金字塔的边缘,他们只支持一个人的重量我包括一个图片包装我没有解释得这么好)并将其添加到自己的重量值。我目前遇到的问题只是函数本身只接受2D数组的第一个值而不是所有的值? Pyramid Example

代码:

#include <stdio.h>

void weight(float x[100][100],int y, int z,int b)
{
    if(z==0&&y==0)
    {
        printf("%.2f\n",x[y][z]);
        weight(x,y+1,z,b);
        return;
    }
    if(z==0&&y==b)
    {
        printf("%.2f",x[y][z]);
        x[y][z]+=x[y-1][z];
        printf("%.2f",x[y][z]);

    }
    if(z==0&&y!=b)
    {
        x[y][z]+=x[y-1][z];
        printf("%.2f",x[y][z]);
        weight(x,y+1,z,b);
    }
    if(y==z&&y==b)
    {
        printf("%.2f",x[y][z]);
        x[y][z]+=x[y-1][z-1];
        return;
    }
    if(y==z&&y!=b)
    {

        x[y][z]+=x[y-1][z-1];
        printf("%.2f\n",x[y][z]);
        weight(x,y+1,0,b);
    }
    if(y!=z)
    {
        printf("%.2f",x[y][z]);
        x[y][z]+=x[y-1][z]+x[y-1][z-1];
    }

}


int main()
{

    //Initialize Variables for use within the program
    int bottom;
    int input;
    int counter=0;

    printf("How many people are in the bottom row of your pyramid: ");
    scanf("%d",&bottom);

    //Initializes pyramid array at correct length
    float pyramid[bottom][bottom];

    //Takes in all user given input values for pyramid weights
    for(int i=0;i<bottom;i++)
    {
        for(int j=0;j<=i;j++)
        {
            printf("Please input the weight of person #%d: ",++counter);
            scanf("%f",&pyramid[i][j]);
        }
    }

    //Prints out all weight values based on user given input
    printf("Pyramid before weight distribution\n");
    for(int i=0; i<bottom;i++)
    {
        for(int j=0;j<=i;j++)
        {
            printf("%.2f ",pyramid[i][j]);
        }
        printf("\n");
    }

    //Prints out all weight values after supporting weight values have been added thru recursive function
    printf("Pyramid after weight distribution\n");
    weight(pyramid,0,0,bottom-1);

    return 0;
}

1 个答案:

答案 0 :(得分:3)

您的代码中存在一个重大错误。您不能在float pyramid[bottom][bottom]上拥有可变大小,然后将其发送到接受float [100][100]的函数。快速解决此问题的方法就是声明float pyramid[100][100]无论bottom的大小。

至于其余部分,我提出了三个解决方案。一种解决方案可以在没有打印输出的情况下修复金字塔,然后您可以打印它。原因是它更简单。您不必担心递归调用是按特定顺序进行的。之后有一个计算给定人体重的解决方案。最后一个解决方案处理递归函数内的打印输出。

相关更改的代码:

#include <stdio.h>

void weight(float w[100][100],int y, int x,int b)
{
    if(y<b) {
        float current = w[y][x];
        w[y+1][x] += current / 2;
        w[y+1][x+1] += current /2;
        weight(w, y+1, x, b);
        if(x==y)
            weight(w, y+1, x+1, b);
    }
}

int main()
{
    ...

    //Initializes pyramid array at correct length                                                                  
    float pyramid[100][100];

    ...

    // Transform pyramid into weighted pyramid
    weight(pyramid,0,0,bottom);

    //Prints out all weight values after supporting weight values have been added thru recursive function          
    printf("Pyramid after weight distribution\n");
    for(int i=0; i<bottom;i++)
    {
        for(int j=0;j<=i;j++)
        {
            printf("%.2f ",pyramid[i][j]);
        }
        printf("\n");
    }
}

由于显而易见的原因,我也冒昧地将体重除以2。我还将变量的名称改为对我来说不那么混乱的东西。

input.txt:

5 3 4 5 7 5 3 4 5 3 6 7 8 4 7 5

输出:

$ ./a.out < input.txt
Pyramid before weight distribution
3.00 
4.00 5.00 
7.00 5.00 3.00 
4.00 5.00 3.00 6.00 
7.00 8.00 4.00 7.00 5.00 
Pyramid after weight distribution
3.00 
5.50 6.50 
9.75 11.00 6.25 
8.88 15.38 11.62 9.12 
11.44 20.12 17.50 17.38 9.56 

另一种递归方法是编写一个返回特定节点总权重的函数。看起来像这样:

float gweight(float w[100][100], int y, int x)
{
    float ret = w[y][x];

    if(y==0)
        return ret;
    if(x<y)
        ret +=  gweight(w, y-1, x) / 2;
    if(x>0)
        ret += gweight(w, y-1, x-1) / 2;
    return ret;
}

然后您可以使用与上面类似的循环打印结果,但将print语句更改为:printf("%.2f ",gweight(pyramid, i, j))

此方法的缺点是您将多次计算相同的值。对于巨大的金字塔,它可能会极大地影响性能。

最后,这个版本实际上完全符合您的要求。

void weight(float w[100][100],int y, int x,int b)
{
    if(y==b)
        return;

    float current = w[y][x];

    printf("%.2f ", current);

    if(y<b) {
        w[y+1][x] += current / 2;
        w[y+1][x+1] += current /2;
    }

    if(y==x) {
        printf("\n");
        weight(w, y+1, 0, b);
    } else
        weight(w, y, x+1, b);
}