Thread: storing numbers

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    Question storing numbers

    Code:
    #include <stdio.h>
    
    int main
    {
    	int number, largest;
    	int counter = 0;
    
    	while ( counter <= 10 ) {
    		printf( "Enter Number: " );
    		scanf( "%d", &number );
    
    		number = largest;
    		if ( largest > number )	
    			largest = number;
    
    		counter++;
    	}
    
    	printf( "Largest Number was: %d", number );
    	return 0;
    }
    Hi, im not sure how to store numbers precisely, all i wanted to do was compare numbers on input and store/output the largest number that was inputted. It just outputs a bunch of numbers.

    Thanks
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You may be storing the largest, but at the end, you're just displaying number and not largest.

    Furthermore, you just want to do:
    Code:
    scan value into 'number'
    if( number > largest )
        largest = number
    Your problem is that you're overwriting the 'largest' value every time in your loop without caring what's in it. See:
    Code:
    number = largest;
    if ( largest > number )
    The if check will never be true, because you've just set largest and number to hold the same value.

    Quzah.
    Last edited by quzah; 01-14-2003 at 09:43 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    You need to shuffle things around a little, read the first number outside of the loop and assign it to largest, then go into the loop and do your tests. It's also better to use a for loop when you know exactly how many times you're going to repeat. You also want to print largest at the end instead of number.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int number, largest;
      int counter = 0;
    
      printf( "Enter Number: " );
      fflush(stdout);
      scanf( "%d", &number );
    
      largest = number;
      
      for (counter = 0; counter < 10; counter++)
      {
        printf( "Enter Number: " );
        fflush(stdout);
        scanf( "%d", &number );
    
        if ( number > largest )
          largest = number;
      }
      
      printf( "Largest Number was: %d\n", largest );
    
      return 0;
    }
    *Cela*

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    hi Cela, i didnt understand the (stdout) part, what does that function actually do? Well ive solved my problem by just initializing the first number on input as the largest number. And then using this as the comparison number on the next input in the loop. This was a better method i think.

    Thanks.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int number, largest;
      int counter = 0;
    
      printf( "Enter Number: " );
      scanf( "%d", &largest );
    
      while (counter <= 9 )
      {
        printf( "Enter Number: " );
        scanf( "%d", &number );
    
        if ( number > largest )
          largest = number;
       counter++;
      }
      
      printf( "Largest Number was: %d\n", largest );
    
      return 0;
    }
    there are only 10 people in the world, those who know binary and those who dont

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    A little bit about fflush()

    The fflush() function causes any buffered output data for stream to be written to file. If any data is written, the file's associated st_mtime and st_ctime values are marked for update. The stream remains open.

    If stream is a null pointer when calling fflush(), all streams opened for writing or update are flushed.

    To more info, please visit this site

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >i didnt understand the (stdout) part, what does that function actually do?
    When you call printf, the '\n' character causes the output to be flushed to its final destination, but if you don't end you call to printf with a newline the output may not be flushed until it's convenient for the program. Sometimes this messes up the order of input and output, usually confusing the user a lot. So I've gotten into the habit of explicitly flushing the output when I want it out. So if my printf call doesn't use a newline, I call fflush() after it so that everything works out everywhere.
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 11:53 AM
  2. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM