Thread: Print the 2 greater numbers of 10 numbers.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Print the 2 greater numbers of 10 numbers.

    Code:
    /*C Program must enter 10 numbers and print the 2 greatest ones using only while and if.
    i.e.
     
    1 2 3 4 5 6 7 8 9 10
    
    greatest numbers are: 10 and 9.
    
    This is what I've done so far:
    
    */
    
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
          int num, greater, greater2, counter;
          counter = 0;
          
          printf("\Enter number and I will tell you which\n"); 
          printf("ones are the two largest numbers of ten: ");
          scanf("%d", &num);
          
          greater = num;
    
          counter ++;
          
          while (counter < 10){
          printf("\nEnter number and I will tell you which\n"); 
          printf("ones are the two largest numbers of ten: ");
          scanf("%d", &num);
                
                if (num > greater) {
                   greater2 = num;
                   greater = greater;
                   }
                else{
                   greater2 = greater;
                   }
                   {
                   counter ++;
                   }
                   }      
    printf("\n\nThe two largest numbers are: %d and %d\n",  greater2, greater);
                      getch();
                      return 0;
    }
    
    /* Any help????? */
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I think in your if statement you want greater2( second largest?) to be assigned greater's value and greater is then assigned num.

    Your unconditional else statement is going to mess you up. If greater is 25 and greater2 is 15 and I enter 20, what is your program going to do?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Quote Originally Posted by joelro79 View Post
    Code:
    /*C Program must enter 10 numbers and print the 2 greatest ones using only while and if.
    i.e.
     
    1 2 3 4 5 6 7 8 9 10
    
    greatest numbers are: 10 and 9.
    
    This is what I've done so far:
    
    */
    
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
          int num, greater, greater2, counter;
          counter = 0;
          
          printf("\Enter number and I will tell you which\n"); 
          printf("ones are the two largest numbers of ten: ");
          scanf("%d", &num);
          
          greater = num;
    
          counter ++;
          
          while (counter < 10){
          printf("\nEnter number and I will tell you which\n"); 
          printf("ones are the two largest numbers of ten: ");
          scanf("%d", &num);
                
                if (num > greater) {
                   greater2 = num;
                   greater = greater;
                   }
                else{
                   greater2 = greater;
                   }
                   {
                   counter ++;
                   }
                   }      
    printf("\n\nThe two largest numbers are: %d and %d\n",  greater2, greater);
                      getch();
                      return 0;
    }
    
    /* Any help????? */
    Your immediate problem seems to be the lines marked in red.

    Also consider how you will account for negative numbers (eg maybe set greater/greater2 to num on the first time round the while loop) You may not need the else statement either.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Assume greater is the largest number and greater 2 is the second largest. Each new number will fit into 1 of 3 categories :

    1) larger than both greater and greater2
    2) in between greater and greater 2
    3) smaller than both greater and greater2

    Figure out how to update greater and greater2 in all 3 cases. I'll help with case 3 - you want to do nothing. The other two are up to you.

  5. #5
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    Quote Originally Posted by KCfromNC View Post
    Assume greater is the largest number and greater 2 is the second largest. Each new number will fit into 1 of 3 categories :

    1) larger than both greater and greater2
    2) in between greater and greater 2
    3) smaller than both greater and greater2

    Figure out how to update greater and greater2 in all 3 cases. I'll help with case 3 - you want to do nothing. The other two are up to you.
    I'm wondering if professors are teaching their students to write out psudocode like this anymore? It seems to me that psudocode and flow charts are the greatest tools in helping someone work out problems like this. Do the paperwork first, and then the code is so much easier. Figure out how the computer is going to think through your instructions, and then type them out correctly, and you only have to write your code once. It saves a lot of headache and time in the long run.

    Here is an example of some psudocode that is related to, but does not solve your problem. Say you are comparing three numbers to find the greatest:

    Code:
    get num1
    get num2
    get num3
    
    if num1 is greater than num2 and num3, 
        num1 is greatest.
    else if num2 is greater than num1 and num3
        num2 is greatest.
    else 
        num3 is greatest
    Apply similar logic and thinking to the situations KCfromNC proposed BEFORE you build your code and you should solve the problem in no time flat.
    Last edited by VASHtheCHIBI; 01-19-2012 at 12:03 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The logic for doing 3 numbers specifically doesn't necessarily work for 10 numbers.
    Code:
    for each number
        if new number bigger than highest number
            second highest = highest
            highest = new
        elseif new number bigger than second highest
            second highest = new
    But that will work for any number of numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    Quote Originally Posted by quzah View Post
    The logic for doing 3 numbers specifically doesn't necessarily work for 10 numbers.
    Quzah.
    I understand this. I said it was similar and related, not that it would solve his problem. I pruposly did not want to hand him the answer to his homework. I thought it would be better for him to think it through for himself. My example was just an example of psudocode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to print only N numbers of string
    By umen242 in forum C Programming
    Replies: 1
    Last Post: 06-26-2008, 12:55 AM
  2. Need help to print even numbers
    By ortegac in forum C Programming
    Replies: 3
    Last Post: 05-21-2006, 12:01 AM
  3. Print Out Factors Of Numbers
    By tom24 in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 11:57 AM
  4. How to print numbers?????
    By AssistMe in forum C Programming
    Replies: 2
    Last Post: 03-04-2005, 08:22 AM
  5. Print numbers by inorder..
    By NightWalker in forum C Programming
    Replies: 5
    Last Post: 09-15-2003, 10:24 AM

Tags for this Thread