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