simple if statment need help

This is a discussion on simple if statment need help within the C Programming forums, part of the General Programming Boards category; i cant figure out this if statment It is driving me nuts all im trying to do is take in ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    simple if statment need help

    i cant figure out this if statment

    It is driving me nuts

    all im trying to do is take in for floats

    evaluate them and print which one is bigger

    im very new to C

    for some reason the first half works but the second half doesnt

    For some reason the bottom part that says print integer is passing its criteria and keeps printing integer 2 out when integer 2 clearly is not less the integer 1

    im just giving it the input "1 2 3 4 "

    can someone please tell me why when given "1 2 3 4" it thinks my else if for integer 2 is true and is printing

    It is clearly false. I used && gates so all factors must be true for it to print. obviously it fails so why and how could it be printing

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
        float integer_1;//first number
        float integer_2;//second number
        float integer_3;//3rd number
        float integer_4;//4th number
    
    
    printf("this program displays the largest and smallest number entered by the user");
    
    printf("Please enter the 4 integers you would like to use with a space between them xx xx xx xx");
    
    scanf("%f  %f  %f  %f",&integer_1 ,&integer_2  ,&integer_3  ,&integer_4);//store the value of user input
                                                                            // into the correct variable location
    printf("%.1f %.1f %.1f %.1f\n",integer_1,integer_2,integer_3,integer_4);// print the variable on the screen to confirm they are taken in correctly. Just for testing
    
    
    
    
    if (integer_1>integer_2&&integer_1>integer_3&&integer_1>integer_4)//logical system to answer the question is integer 1 the biggest. If it does not pass to any of the criteria is should move on to next line
        printf("Largest%f\n",integer_1);//if it pass print largest value
    
        else
    
        if (integer_1<integer_2&&integer_1<integer_3&&integer_1<integer_4);//test to see if integer 1 is possibly the smallet value
        printf("smallest  %.1f\n",integer_1);//if it is print it on the screen
    
    
    
    printf("%.2f   %.2f   %.2f   %.2f\n\n\n",integer_1,integer_2,integer_3,integer_4);//just for testing
    
    if ((integer_2>integer_1)&&(integer_2>integer_3)&&(integer_2>integer_4))//logical system to find out if integer 2 is the greatest value
        printf("Largest%f\n",integer_2);//if it is print it
    
        else
    
        if (integer_2<integer_1&&integer_2<integer_3&&integer_2<integer_4);//logical system to see if integer 2 is smallest, if it is print it. If no criteria is met move on.
        {printf("smallest  %.1f\n",integer_2);}//this one is the problem i speak of! it keeps printing this off when its FALSE
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return(0);
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of this "logical scrum" you have going, do it the simple way.
    Code:
    int small, large;
    small = large = integer_1;
    
    if(small < other integers)
       small == that integer # value
    
    if(large > other integers)
       large == that integer # value
    
    printf(Smallest: %d        Largest: %d",small, large);
    It's such a simple word really -- row. :rofl:

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Your formatting/indentation needs work. Make sure you have an editor that has an auto-indent feature, it would help you catch your error here. On lines 30 and 42 you have an errant semicolon at the end of your if. That means the following statement is always executed.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by Adak View Post
    Instead of this "logical scrum" you have going, do it the simple way.
    Code:
    int small, large;
    small = large = integer_1;
    
    if(small < other integers)
       small == that integer # value
    
    if(large > other integers)
       large == that integer # value
    
    printf(Smallest: %d        Largest: %d",small, large);
    It's such a simple word really -- row. :rofl:

    i would love to do it an easier way
    if u have a second could you explain a bit more what you have going on

    why in the begining you have small-large=intege1?

    i dont understand that

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by anduril462 View Post
    Your formatting/indentation needs work. Make sure you have an editor that has an auto-indent feature, it would help you catch your error here. On lines 30 and 42 you have an errant semicolon at the end of your if. That means the following statement is always executed.

    right because it has been terminated

    im using codeblocks

    im very new to programming im a math major
    so im just learning what i can and cant do

    Thanks for the catch there. Do you think this will work if i continue on?

    Im sure you get the idea of what is 2 come next

    same sequnce but with integer 3 and then again integer 4

    Do you think that would work?
    im sure there are easier ways but i sure dont know them

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think of it as a point of reference. Say you only had 1 value, and your program had to find the smallest and the largest value. What would that be?

    Correct! The very first value, for both.

    Work it through with some coins on your desk, and see if it works.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Thanks for the help guys
    ill keep plugging away at it

    see if i can make any more progress

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    i think i got it!
    it was the semi colon

    the track i was on was working might not be simplest or prettiest but it does work i think atleast lol

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
        float integer_1;//first number
        float integer_2;//second number
        float integer_3;//3rd number
        float integer_4;//4th number
    
    
    printf("this program displays the largest and smallest number entered by the user");
    
    printf("Please enter the 4 integers you would like to use with a space between them xx xx xx xx");
    
    scanf("%f  %f  %f  %f",&integer_1 ,&integer_2  ,&integer_3  ,&integer_4);//store the value of user input
                                                                            // into the correct variable location
    printf("%.1f %.1f %.1f %.1f\n",integer_1,integer_2,integer_3,integer_4);// print the variable on the screen to confirm they are taken in correctly. Just for testing
    
    
    
    
    if (integer_1>integer_2&&integer_1>integer_3&&integer_1>integer_4)//logical system to answer the question is integer 1 the biggest. If it does not pass to any of the criteria is should move on to next line
        printf("Largest%f\n",integer_1);//if it pass print largest value
    
        else
    
        if (integer_1<integer_2&&integer_1<integer_3&&integer_1<integer_4)//test to see if integer 1 is possibly the smallet value
        printf("smallest  %.1f\n",integer_1);//if it is print it on the screen
    
    
    
    printf("%.2f   %.2f   %.2f   %.2f\n\n\n",integer_1,integer_2,integer_3,integer_4);//just for testing
    
    if ((integer_2>integer_1)&&(integer_2>integer_3)&&(integer_2>integer_4))//logical system to find out if integer 2 is the greatest value
        printf("Largest%f\n",integer_2);//if it is print it
    
        else
    
        if (integer_2<integer_1&&integer_2<integer_3&&integer_2<integer_4)//logical system to see if integer 2 is smallest, if it is print it. If no criteria is met move on.
        {printf("smallest  %.1f\n",integer_2);}//this one is the problem i speak of! it keeps printing this off when its FALSE
    
    
    
    
    if ((integer_3>integer_1)&&(integer_3>integer_2)&&(integer_3>integer_4))//logical system to find out if integer 2 is the greatest value
        printf("Largest%f\n",integer_3);//if it is print it
    
        else
    
        if (integer_3<integer_1&&integer_3<integer_2&&integer_3<integer_4)//logical system to see if integer 2 is smallest, if it is print it. If no criteria is met move on.
        {printf("smallest  %.1f\n",integer_3);}//this one is the problem i speak of! it keeps printing this off when its FALSE
    
    
    
    
    if ((integer_4>integer_1)&&(integer_4>integer_2)&&(integer_4>integer_3))//logical system to find out if integer 2 is the greatest value
        printf("Largest%f\n",integer_4);//if it is print it
    
        else
    
        if (integer_4<integer_1&&integer_4<integer_2&&integer_4<integer_3)//logical system to see if integer 2 is smallest, if it is print it. If no criteria is met move on.
        {printf("smallest  %.1f\n",integer_4);}//this one is the problem i speak of! it keeps printing this off when its FALSE
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return(0);
    
    }

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Yeah, your solution isn't the easiest/prettiest/most efficient, but it should work. Of course, you're going to test this thoroughly before submitting it to make sure, right?

    The reason your code is not the best is that it doesn't scale as well as Adak's (though no solution is really scalable without an array). Imagine how long your if conditions would be if you had 10 integers. Not only that, you compare each number against each other, twice (once for smallest, once for largest). For N integers, that's N*(N-1) for smallest and another N*(N-1) for largest. That's a lot of unnecessary comparisions. It never remembers anything from previous comparisons.


    Adak's solution looks at each number in turn, and remembers the smallest and largest number it's seen so far. It only uses N-1 comparisons for each of smallest and largest. That is the same concept you would use if you had to do this for an array of unknown size. Note that the conditions in Adak's example are backwards, it should be:
    Code:
    small = large = integer_1;  // look at integer_1, so far it's the smallest and largest number we've seen
    
    
    if (integer_2 < small)
        small = integer_2;  // integer_2 is smaller than anything so far, record it as the smallest
    
    
    // similar for largest and for integer_3 and integer_4

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by anduril462 View Post
    Yeah, your solution isn't the easiest/prettiest/most efficient, but it should work. Of course, you're going to test this thoroughly before submitting it to make sure, right?

    The reason your code is not the best is that it doesn't scale as well as Adak's (though no solution is really scalable without an array). Imagine how long your if conditions would be if you had 10 integers. Not only that, you compare each number against each other, twice (once for smallest, once for largest). For N integers, that's N*(N-1) for smallest and another N*(N-1) for largest. That's a lot of unnecessary comparisions. It never remembers anything from previous comparisons.


    Adak's solution looks at each number in turn, and remembers the smallest and largest number it's seen so far. It only uses N-1 comparisons for each of smallest and largest. That is the same concept you would use if you had to do this for an array of unknown size. Note that the conditions in Adak's example are backwards, it should be:
    Code:
    small = large = integer_1;  // look at integer_1, so far it's the smallest and largest number we've seen
    
    
    if (integer_2 < small)
        small = integer_2;  // integer_2 is smaller than anything so far, record it as the smallest
    
    
    // similar for largest and for integer_3 and integer_4

    Thanks yes it does work. I apprecaite the help and suggestions
    I would like to make it as nice as possible but bot truly unerstand what we are doing above so im gonna spend some time on.
    For now, my ugly way works for 4 digits. lol

    I apprecaite the help greatly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  2. If statment qustion
    By Yizi in forum C Programming
    Replies: 5
    Last Post: 11-21-2009, 03:53 AM
  3. C++ If statment question
    By TehClutchKiller in forum C++ Programming
    Replies: 17
    Last Post: 05-28-2008, 07:33 AM
  4. Weird Statment...
    By Devil Panther in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 02:55 PM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM

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