Thread: Find the Largest and Smallest Number

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    I tried this but it did not work.

    if( x > y) {
    if(x > z) {
    printf("Largest: %d\n", x);
    }
    }




    I did get the && to work if I were to use that though.


    if ( x>y && x>z ){
    printf( "Largest: %d\n", x);
    }

    if ( y>x && y>z ){
    printf( "Largest: %d\n", y);
    }

    if ( z>y && z>x ){
    printf( "Largest: %d\n", z);
    }

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    Okay, I've got it done. Thanks for your help guys! I really appreciate it!

  3. #18
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    In your test, the first line checks if x is > y - if it is, it moves to the second line, and checks if x is greater than z - if it is, then it prints out that x is the largest, as obiviously, if there are three integers, and it is larger than the other two, than it is is largest. Now lets say x > y, so it checks if x > z, but finds that x is actually < z - the printf statement will not get executed, and obviously x is not the largest. Similarly, if you check if x > y, where x is not > y, then the nested if (x > z) does not even get checked. In this case also then, we see that x is not the largest. So then we need to write some more lines. We need to see if y > x and if so, then we need to see if y > z - if it is, great, print out a message telling the world that y is largest. Otherwise, if either two of those tests fail, then you know that y is not the largest. By this time, you know that by default z must be largest, but since you are not using an else statement, you will want to run the tests on z.

    edit::: blech... all this typing and all too late.
    Last edited by kermit; 09-03-2006 at 06:39 PM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    I did the nesting and i got the program running like I wanted to. I really appreciate your help A LOT! I got two more programs to go and I will be done. Below is what I did, so hopefully it might help someone in the same boat as me in the future.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int x, y, z; 
    	int sum;
    	int average;
    	int product;
    
    	
    	
    	printf( "Input 3 different integers: ");
    	scanf( "%d%d%d", &x, &y, &z );
    	
    
    	sum = x + y + z;
    	average = (x + y + z)/3;
    	product = x * y * z;
    
        
      	printf( "Sum is %d\n", sum );
    	printf( "Average is %d\n", average);
    	printf( "Product is %d\n", product );
    
       if( x>y) {
          if(x>z) {
        printf("Largest: %d\n", x);
        }
      }   
      
      if( y>x) {
          if(y>z) {
        printf("Largest: %d\n", y);
        }
      }   
      
      if( z>y) {
          if(z>x) {
        printf("Largest: %d\n", z);
        }
      }   
      
      
         if( x<y) {
          if(x<z) {
        printf("Smallest: %d\n", x);
        }
      }   
      
      if( y<x) {
          if(y<z) {
        printf("Smallest: %d\n", y);
        }
      }   
      
      if( z<y) {
          if(z<x) {
        printf("Smallest: %d\n", z);
        }
      }   
      
      
      
    
    system ("pause");
    	return 0;
    
    }
    Last edited by Dave_Sinkula; 09-03-2006 at 07:14 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is the average supposed to be an int?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Yeah. Make it a float or a double or something. If it's not one of those, and if the average is 3.141, the average variable will only be 3.

    >> system ("pause");
    Read the FAQ about using cin.get() and cin.ignore() to avoid using system("pause") commands, unless you have to. The reason why you should try to avoid using system commands is because, apparently, some people like to hack into computers, and make batch files, called pause or cls or whatever, so when you system ("pause"), you inadvertently execute the bat file, and the most popular conclusion is that your entire hard drive is deleted. Not sure how likely that is to really happen, but it's something to keep in mind.

    Also, learn to use code tags, as Dave_Sinkula suggested. And if you want your post to "help someone in the same boat as me in the future", use if else statments here on the boards, even if you aren't allowed to use them in your submission, or don't know how.
    Last edited by twomers; 09-04-2006 at 02:33 PM.

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah. Make it a float or a double or something. If it's not one of those, and if the average is 3.141, the average variable will only be 3.
    That was my thought exactly, but perhaps they haven't learnt about floating point numbers yet.

    so when you system ("pause"), you inadvertently execute the bat file
    You could put the whole path in, which wouldn't be infalliable but would be slightly better (and less portable, but then it's unportable to begin with). The best idea would be to read the FAQ as suggested. [edit] Here it is: http://faq.cprogramming.com/cgi-bin/...&id=1043284385 [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> so when you system ("pause"), you inadvertently execute the bat file
    We all make these mistakes though ... right dwks ... haha, I'd forgotten about that ... hope you like the new change ... however, not all of us consider the consequences :/


    >> but perhaps they haven't learnt about floating point numbers yet.
    floats are number which can have fractions, ie 3.14. You use the word float much like an int -

    Code:
    float pi = 3.141;
    printf ("PI: %f", pi);
    However, floating point arithmitic isn't ... perfect. 'There's something in the FAQ or the Tutorials about it if you're interested. I'd generally use doubles or something.
    Last edited by twomers; 09-04-2006 at 10:17 AM. Reason: Nostalgia

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    >> but perhaps they haven't learnt about floating point numbers yet.
    floats are number which can have fractions, ie 3.14. You use the word float much like an int -
    Evidently it doesn't matter how easy it is.
    We cannot use what we have not learned in class.
    If it hasn't been taught, they can't use it.

    I'd generally use doubles or something.
    Doubles are (generally) more accurate than floats. They used to be slower to use than floats (that's why floats exist) but it doesn't matter now.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #25
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> >> We cannot use what we have not learned in class.
    >> If it hasn't been taught, they can't use it.

    I know. But because they haven't learnt it in class doesn't mean that they can't know it. That's why I put it down there. Just as a pointer to what you could/should do.

  11. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I guess you didn't see this?
    we have not gone over && so we are not allowed to use them in our program.
    Are you just assuming that?
    no, i am not assuming. We cannot use what we have not learned in class. I think its a bunch of bs. My teacher all he does is talk about going to hooters and 69 numbers.

    Evidently you can't just "know" something in that class.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #27
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I KNOW, and I did see it. I wasn't saying that the OP should use what I said for his class. I was saying that it's how it could/should be done. I understand the limitations of the teacher, but I don't see what the OP shouldn't know how it could/should be done, just because his teacher hasn't covered it yet!

    Why not 'just "know"' it for himself, but not use it in class? I acknowledge I was unclear, so I clarified in my previous post.

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, I see. Sorry.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM
  5. Replies: 2
    Last Post: 09-05-2004, 07:13 AM