Thread: Simple loop problem I cant seem to solve

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    98

    Simple loop problem I cant seem to solve

    Hey all I'm new here and I really appreciate this resource and all your help.
    So here goes,...


    I am in a beginners c programming class and I have been presented with the following problem:
    "Write a program that finds the largest in a series of numbers enterd by the user. The program must prompt the user to enter numbers one by one. When user enters 0 or a negative number, the program terminates and must display the largest nonnegative number entered."


    Okay so here's my code so far:

    insert
    Code:
     #include <stdio.h>
    
    int main(void)
    
    {
     float number, highestnumber;
    
    
     printf("Enter a series of numbers and I will tell you which is the largest.\nEn
    ter a 0 or negative number when you wish to terminate the program.\n");
    
        scanf("%f", &number);
    
        while  (number != 0 && number > 0)  {
         printf("Enter your next number: ");
         scanf("%f", &number);
    
         if (number >= highestnumber) {
         number =  highestnumber; }
    
    
    
    
       else if (number < highestnumber) {
        highestnumber = highestnumber; }
                        }      // end of while loop
       printf("The highest number is: %2f\n", highestnumber);
    
    
    
    
     return 0;
    
    }





    Anyway, the program works as far as it allows you to enter as many numbers as you want, until you hit 0 or a negative, then it terminates.

    The problem is that no matter what the highest number was, the program always says that the highest number is 0.

    There must be some relation between the highestnumber and number that I am missing, but does anyone have any ideas? This is due tomorrow and the chapter has no analogous problems that will help me.


    Thanks a bunch

    -P

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Without initializing highestnumber, it'll contain garbage so tests like if (number >= highestnumber) will be uncertain.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You got the logic right but you are making two mistakes:

    1) highestnumber is not initialized to a value. Initialize it to 0.0 at the beginning of the program.

    2) when you input a number larger than highestnumber assign highestnumber the value of the number read instead of the other way around.

    3) There is no need to have an else. If the number you read is smaller than your current highest don't do anything. You just don't care about it.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Thank you so much let me go implement that.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Okay works better now except that I can't seem to get it to include the number from the first call of scanf in the set of possible highest numbers. It seems to include only those within the loop. Perhaps the loop should end before the final if (statement)?

    insert
    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
     float number, highestnumber;
    
     highestnumber = 0.0;
    
     printf("Enter a series of numbers and I will tell you which is the largest.\nEn
    ter a 0 or negative number when you wish to terminate the program.\n");
    
     scanf("%f", &number);
    
        while  (number != 0 && number > 0)  {
         printf("Enter your next number: ");
         scanf("%f", &number);
    
         if (number >= highestnumber) {
         highestnumber = number; }
    
    
                        }      // end of while loop
       printf("The highest number is: %2f\n", highestnumber);
    
    
    
    
    
    
    
    
    
    
     return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    If I remove the first call of scanf it goes back to giving me 0 as the answer.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you are going to read an extra number outside the loop, just set highestnumber to that read-in number; and then do the loop normally.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    So I could prompt the user to enter a number n, and initialize highestnumber as highestnumber=n
    ?

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Remove the first printf and scanf outside the loop and change the loop from a while() to a do while since you need to priming.


    Code:
    do{
       printf
       scanf
    }while(number > 0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  3. whats the problem....
    By vapanchamukhi in forum C Programming
    Replies: 3
    Last Post: 09-05-2008, 12:19 PM
  4. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  5. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM