【问题标题】:2D array function only passing in 1 Value二维数组函数只传入 1 个值
【发布时间】:2018-04-20 07:41:30
【问题描述】:

我有一个程序可以接收具有权重值的人的金字塔,并且应该使用递归函数来添加该人所支持的两个人的权重(如果该人位于金字塔的边缘,他们仅支持一个人的重量我包括一个图片包我没有很好地解释这一点)并将其添加到它自己的重量值中。我目前遇到的问题只是函数本身只接受二维数组的第一个值而不是所有值?

代码:

#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;
}

【问题讨论】:

  • 你能解释一下“函数本身只接收二维数组的第一个值而不是所有值”是什么意思吗?
  • 我认为您不应该更改二维数组中的值。相反,从weight() 函数返回一个值。
  • float pyramid[bottom][bottom]float x[100][100] 不匹配(如果 bottom != 100)。
  • “不匹配”是什么意思?能举个例子吗?
  • 您将第一个数组元素的索引(y 和 z)传递给函数 weight,但 weight 不会修改这些值来索引数组中的其他元素。

标签: c arrays function multidimensional-array


【解决方案1】:

您的代码中有一个重大错误。您不能在 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。我还将变量的名称更改为对我来说不太容易混淆的名称。

输入.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;
}

然后您可以使用与上述类似的循环打印结果,但将打印语句更改为: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);
}

【讨论】:

  • 虽然不是问题所要求的,但非递归解决方案避免了每次递归调用产生的函数调用开销。虽然对于小型金字塔,这并不重要,但对于大型计算,它会加起来。您还可以根据需要将二维数组替换为指向类型和realloc 的简单指针。很好的答案。
  • @DavidC.Rankin 是的,我也想过这个问题,但我也认为这离问题有点远。也在考虑动态分配和指针,但我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 2020-11-01
相关资源
最近更新 更多