Like Tree7Likes

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. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    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)
    Last edited by shukiren; 09-22-2011 at 11:16 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    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!
    Last edited by anduril462; 09-22-2011 at 11:27 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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.
    Last edited by KCfromNC; 09-22-2011 at 11:34 AM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    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.
    Last edited by anduril462; 09-22-2011 at 02:46 PM.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102

    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; }
    Last edited by rmatze; 09-22-2011 at 02:47 PM. Reason: Looks like anduril462 beat me to it...

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    You're welcome. Glad you got it working.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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;
        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 people = 0;
            int layers_count = 1;
    
            for (layers_count = 1; layers_count <= num_layers; layers_count++)
            {
                double radius = radius_bot - radius_diff;
                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. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    bump?

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    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
    for (radius = radius_top; radius <= radius_bot; radius += radius_diff)
        current area = PI * radius * radius
        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. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by shukiren View Post
    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. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if / else statements
    By garrettchap1 in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2011, 08:51 PM
  2. help with if and else statements
    By kburyanek in forum C Programming
    Replies: 7
    Last Post: 09-25-2009, 01:28 AM
  3. What are these statements for?
    By pheres in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 08:09 AM
  4. IF statements
    By dragon2309 in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2005, 12:59 AM
  5. for statements
    By Lyanette in forum C++ Programming
    Replies: 9
    Last Post: 03-29-2003, 06:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21