Reputation: 25
I have a program that takes in a pyramid of people with weight values and is supposed to use a recursive function to add the weight of the two people that the person is supporting (if the person is on an edge of a pyramid they only are supporting one persons weight I included a picture encase I didn't explain this well enough) and add that to its own weight value. The problem I'm currently having is simply that the function itself is only taking in the first value of the 2D array instead of all the values?
Code:
#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;
}
Upvotes: 2
Views: 75
Reputation: 31409
There's one major mistake in your code. You cannot have a variable size on float pyramid[bottom][bottom]
and then send it to a function accepting a float [100][100]
. A quick fix for this is to just declare float pyramid[100][100]
No matter the size of bottom
.
As for the rest, I made three solutions. One solution fixes the pyramid without printouts, and you can print it afterwards. The reason is that it is much simpler. You do not have to worry about the recursive calls being in a specific order. After that there's one solution that calculates the weight of a given person. The last solution handles printouts inside the recursive function.
Code with relevant changes:
#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");
}
}
I also took the liberty of dividing the weight by 2, for obvious reasons. I also changed the name of the variables to something a bit less confusing for me.
input.txt :
5 3 4 5 7 5 3 4 5 3 6 7 8 4 7 5
Output:
$ ./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
An alternative recursive approach is to write a function that returns the total weight for a particular node. Looks like this:
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;
}
And then you can print the result with a similar loop as above, but change the print statement to: printf("%.2f ",gweight(pyramid, i, j))
The drawback of this method is that you will calculate the same value many more times. For huge pyramids it could greatly impact performance.
And finally, a version that actually does exactly what you want.
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);
}
Upvotes: 3