# So many if statements >:(

This is a discussion on So many if statements >:( within the C Programming forums, part of the General Programming Boards category; Okay, I'm a little annoyed by the amount of if statements I've had to use here. We have to figure ...

1. ## So many if statements >:(

Okay, I'm a little annoyed by the amount of if statements I've had to use here. We have to figure out the distances of the centroid of a triangle to the 3 points of the triangles and list them in increasing order. Here's my code in that calculation so far:

Code:
``` if(distance_1 > distance_2 && distance_3 < distance_2)
printf("The corresponding travel distances are: %.2lf, %.2lf and %.2lf.\n", distance_3, distance_2, distance_1);

if(distance_1 < distance_2 && distance_3 > distance_2)
printf("The corresponding travel distances are: %.2lf, %.2lf and %.2lf.\n", distance_1, distance_2, distance_3);

if(distance_2 < distance_1 && distance_3 > distance_1)
printf("The corresponding travel distances are: %.2lf, %.2lf and %.2lf.\n", distance_2, distance_1, distance_3);

if(distance_1 < distance_3 && distance_2 > distance_3)
printf("The corresponding travel distances are: %.2lf, %.2lf and %.2lf.\n", distance_1, distance_3, distance_2);

if(distance_2 < distance_3 && distance_1 > distance_3)
printf("The corresponding travel distances are: %.2lf, %.2lf and %.2lf.\n", distance_2, distance_3, distance_1);

if(distance_3 < distance_1 && distance_2 > distance_1)
printf("The corresponding travel distances are: %.2lf, %.2lf and %.2lf.\n", distance_3, distance_1, distance_2);```
This satisfies some of the instances. BUT, if two or more of the x or y coordinates of the triangle points are equal to each other, it obviously won't output the travel distances. I want to know if I have to keep writing if statements to satisfy the rest of the instances, or if there is an easier way to represent the instances without writing an if statement for each... :P (I HOPE THIS MAKES SENSE)

2. Assuming you haven't learned about arrays and sorting yet, you can maybe clean it up a bit by nesting your if-statements and using some else and else-if clauses. Here's some sample pseudo code:
Code:
```if d1 < d2 and d1 < d3 // d1 is the smallest
print d1
if d2 < d3  // d2 is the middle, d3 the largest
print d2
print d3
else  // d3 must be the middle, d2 the largest
print d3
print d2
else if // d2 is the smallest
else // d3 must be the smallest```
EDIT: Fixed typos in the pseudo code!

3. Are you just trying to sort them into ascending order? If so, put them in an array and sort them then print the array in order. Or just change the appropriate less than/ greater than operators into <= / >=. If two values are the same, it doesn't matter what order you print them in.

And be sure to change each if after the first to "else if", assuming you only want to print one of the options.

You could also use min() and max() macros, I think.

The smallest value would be min(min(d1,d2),d3). The largest one would be max(max(d1,d2),d3). The middle one would be something like min(max(d1,d2),max(d2,d3),max(d1,d3)) = min(min(max(d1,d2),max(d2,d3)),max(d1,d3)) if you're limited to using a minimum function with only two arguments.

That last one is strange, but think about it like this. One of the max function is guaranteed to compare the two smallest items, and max of that is the middle one. The other max values will have the largest value. So the min of the largest value (returned twice) and 2nd largest is 2nd largest, or the middle element. Don't try to expand this to 4 items unless you really enjoy pain.

I take no responsibility when you get in trouble for using code like that in a homework assignment if you haven't gone over macros yet.

If you're looking to do something other than print them out in order, give more detail.

4. Made it a little shorter... HOWEVER. Now when entering these test values:
(x1,y1) = 2.0 5.6
(x2,y2) = 3.1 -4.7
(x3,y3) = -2.2 3.5

it's giving me the answers 3.76, 3.76, and 6.53. It's supposed to be 3.76, 4.26, and 6.53 instead. I've tried messing around with brackets and other parts of the code... but I can't seem the catch the error.. Any suggestions?

Code:
```#include <stdio.h>
#include <math.h>
int main()

{
double x1, y1, x2, y2, x3, y3;

printf("What are the x-y coordinates of the first location?\n");
scanf("%lf%lf", &x1, &y1);

printf("What are the x-y coordinates of the second location?\n");
scanf("%lf%lf", &x2, &y2);

printf("What are the x-y coordinates of the third location?\n");
scanf("%lf%lf", &x3, &y3);

double centroid_x = (x1+x2+x3)/3; //Calculates the x-coordinate of the centroid of the triangle.
double centroid_y = (y1+y2+y3)/3; //Calculates the y-coordinate of the centroid of the triangle.

double distance_1 = sqrt(pow(x1-centroid_x,2)+pow(y1-centroid_y,2)); //Calculates the distance from the centroid to point (x1,y1).
double distance_2 = sqrt(pow(x2-centroid_x,2)+pow(y2-centroid_y,2)); //Calculates the distance from the centroid to point (x2,y2).
double distance_3 = sqrt(pow(x3-centroid_x,2)+pow(y3-centroid_y,2)); //Calculates the distance from the centroid to point (x3,y3).

printf("You should live at location (%.2lf, %.2lf).\n", centroid_x, centroid_y);

double smallest, middle, largest;

if(distance_1 < distance_2 && distance_1 < distance_3)
{    smallest = distance_1; }

if(distance_2 < distance_3)
{    middle = distance_2;
largest = distance_3; }

else
{    middle = distance_3;
largest = distance_2; }

{ if(distance_2 < distance_1 && distance_2 < distance_3)
smallest = distance_2;

else
smallest = distance_3; }

printf("The corresponding travel distances are: %.2lf, %.2lf, and %.2lf.\n", smallest, middle, largest);

return 0;
}```

5. Pick a better indentation and bracketing style:
SourceForge.net: Indentation - cpwiki
Indent style - Wikipedia, the free encyclopedia

I recommend one of the top 3 styles mentioned on the Wikipedia page.

Doing that shows some errors:
Code:
```    if(distance_1 < distance_2 && distance_1 < distance_3)
{
smallest = distance_1;
}

if(distance_2 < distance_3)  // this if and it's else should be inside the above if, after smallest = distance_1
{
middle = distance_2;
largest = distance_3;
}
else
{

middle = distance_3;
largest = distance_2;
}
{  // where is the else if?  these brackets do nothing other than create a new scope
if(distance_2 < distance_1 && distance_2 < distance_3)
smallest = distance_2;
else
smallest = distance_3;
}
// You'll need a final else here for when distance_3 is the smallest```
Reread my pseudo code carefully, paying extra attention to indent levels and how that corresponds to nesting your if statements.

EDIT: Take a look at our tutorial here: http://www.cprogramming.com/tutorial/c/lesson2.html for some examples of bracketing.

6. ## Looks like I was beat to the punch...

Your brackets aren't lined up correctly...look this part over:
Code:
`if(distance_1 < distance_2 && distance_1 < distance_3)     {    smallest = distance_1; }          if(distance_2 < distance_3)         {    middle = distance_2;              largest = distance_3; }          else         {    middle = distance_3;              largest = distance_2; }      { if(distance_2 < distance_1 && distance_2 < distance_3)         smallest = distance_2;      else         smallest = distance_3; }`

7. THANK YOU. I guess I just wasn't thinking over all the inequalities and situations I needed to take into account for each set. Also thanks for the links about the organization, our teacher never really went over this. He basically just told us to use spaces and keep it grouped. Looks much cleaner now.

8. You're welcome. Glad you got it working.

9. Didn't feel like creating a new thread because I have a simple question. I'm working on another problem and I have everything great so far except for one thing (I THINK). Here is my code:
Code:
```#include <stdio.h>
#include <math.h>
int main()

{
const double PI = 3.141592654;

int num_layers, num_guests, guests_fed;

printf("How many layers will your cake have?\n");
scanf("%d", &num_layers);

printf("What is the radius of the top layer?\n");

printf("What is the radius of the bottom layer?\n");

printf("How much cake in cross-sectional area does each guest need?\n");
scanf("%lf", &cake_needed);

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 people = 0;
int layers_count = 1;

for (layers_count = 1; layers_count <= num_layers; layers_count++)
{
people += (PI * pow(radius, 2)) / cake_needed;
}

printf("Your cake will feed %d guests.\n", people);
}

return 0;
}```
I don't really understand how to make a statement that allows the radius to dynamically change. As the code is now, it calculates each of the layers of the cake as serving the same amount of people (no matter what layer it's on), and multiplies it by the number of layers. Can anyone help me out here?

10. bump?

11. Read the forum guidelines, especially #3 where it says don't bump.

You need to address objective #3, repetition (loops). You get the radius_diff correctly, so you need to loop from top radius to bottom radius in steps of radius_diff (a for loop should suffice).
Code:
```total area = 0
total area += current area

num guests = total area / area per guest.```
Note, working with floats/doubles is inherently inaccurate. Exact equality doesn't always work, especially when radius_diff is not a whole number. You might the loop condition (middle of the for-loop parts) to be something like
Code:
`radius - radius_bot < 0.01  // loop must stop when radius is no more than 0.01 units greater than radius_bot`

12. Okay thanks. I got it working for multiple situations. I changed the double to int so it would round instead, because it was always off by like 1 or 2 due to decimals.

13. Originally Posted by shukiren
Okay thanks. I got it working for multiple situations. I changed the double to int so it would round instead, because it was always off by like 1 or 2 due to decimals.
Part of that inaccuracy may have to do with you calculating how many people each layer feeds then totaling the people. The truncation of going from float/double to int is your biggest loss of precision. That truncates the calculations for each layer (big imprecision), and adds the truncated values, instead of adding all the values together for total cross-sectional area (which has a fairly small imprecision), then truncating once.

14. Why not just calculate the volume of the overall cake then the volume of 1 piece of cake, and divide?

OR

If they are cutting from edge to center on each layer, calculate the width of 1 piece and the circumfrence of the layer and divide...

15. Well, if each layer of the cake can only feed a certain amount of people, and that number people is a whole number. Wouldn't truncating each layer, adding them together, and then dividing by the cross sectional area needed by each person solve it correctly? Because if I add the areas of each layer and each layer isn't truncated, it ends up feeding more people than it should.

Page 1 of 2 12 Last