Alright here's the problem:
Code:
Problem C: Infinite Cake (cake.c)
Wedding Cakes come in many designs. One of the designs is a set of cylindrical layers.
Each layer, from top to bottom has a bigger radius than the one above it. The number of
people each layer feeds is based up on the area of the full top surface of the cake. In order
for the cakes to look good, the sequence of radii of the different layers must follow an
arithmetic sequence with a positive difference. (If you forgot what an arithmetic sequence
is, please go online and look this up.)
Your problem will be to figure out how many people a particular cake design can feed,
based on the number of layers the cake has, the radius of the top layer cake, the radius of
the bottom layer cake, and the amount of cake (described by the area of the top of the
piece) that one person eats.
For example, if a layer of cake has cross-sectional area 30.7 units2 and each person eats
5.3 units2 worth of cake, then 30.7/5.3 = 5.79 people can be fed. BUT, people aren’t
fractional and we can’t give someone parts of cake from two different layers. Thus, in
this case, we’ll say that the cake feeds 5 people and each of those five pieces is a bit
bigger than necessary.
To correctly solve the problem, make this calculation separately for each layer. For
example, if one layer feeds 5.79 people and another layer feeds 8.66 people, together they
would only feed 5 + 8 = 13 people, instead of 5.79 + 8.66 = 14.45 or 14 people.
Input Specification
1. The number of layers will be a positive integer less than 100.
2. The radius of the top layer will be a positive real number less than 1000.
3. The bottom layer radius will be a positive real number at least as big as the top layer
and less than 1000. (In a one layer cake, this number will be the same as the top layer
radius.)
4. The amount of cake necessary for each individual will be a positive real number
representing cross-sectional area less than the cross-sectional area of the top layer.
Output Specification
Output the total number of guests that can be fed with the given cake design:
Your cake will feed X guests.
where X is the number of guests in question.
Output Samples
A sample output of running the program is included below. Note that this sample is
NOT a comprehensive test. You should test your program with different data than is
shown here based on the specifications given above.
Sample Run #1
How many layers will your cake have?
5
What is the radius of the top layer?
6
What is the radius of the bottom layer?
14
How much cake in cross-sectional area does each guest need?
4.5
Your cake will feed 374 guests.
Here's what I have so far. **ignore some of the bad formatting, I copied it in weird**
Code:
#include <stdio.h>
#include <math.h>
int main()
{
const double PI = 3.141592654;
int num_layers, num_guests, guests_fed;
double radius_top, radius_bot, cake_needed, radius_diff;
printf("How many layers will your cake have?\n");
scanf("%d", &num_layers);
printf("What is the radius of the top layer?\n");
scanf("%lf", &radius_top);
printf("What is the radius of the bottom layer?\n");
scanf("%lf", &radius_bot);
printf("How much cake in cross-sectional area does each guest need?\n");
scanf("%lf", &cake_needed);
radius_diff = (radius_bot - radius_top) / (num_layers - 1);
int layer_1 = (PI * pow(radius_top, 2)) / cake_needed;
if(num_layers == 1)
{
printf("Your cake will feed %d guests.\n", layer_1);
}
else
{
int layers_count = 1;
double area_layers;
while(layers_count < num_layers)
{
layers_count = layers_count++;
}
}
return 0;
}
My next step would be to make some sort of statement that calculates the areas of each section of the cake and divides them each by the cross sectional area of caked needed for each person. When all of these are added up, along with layer 1, it gives the total amount of people the cake will feed. I get the logic behind it, but I don't know how to make a statement that performs this. Can anyone help me out here?