Thread: Iterate variable (newbie)

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Post Iterate variable (newbie)

    Just starting C, so I have some problems with the basics!

    I have several inputs and need to identify the largest inputted number.
    I am told by my teacher to use a loop.

    so far I have this
    Code:
                    float n1, n2;
    
    	while (n1 >= 0)
    	{
    		printf("Enter any number: \n");
    		scanf("%f", &n1);
    		printf("Enter any number: \n");
    		scanf("%f", &n2);
    		/* Reads two numbers */
    		if (n1 > n2)
    			max = n1;
    		else
    			max = n2;
    		/* Compare the two numbers, keep the higher number. */
    	}
    	printf("Largest number is: %f", max);
    I want the program to stop running and outputting the highest number if 0 or a negative number is inputted.
    I see the potential bug with n1 >= 0 with that n2 can be typed in as a break parameter.

    Should I possibly pull the n1, n2 outside the while loop and then keep the 'max' inside the loop ?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you want to end the loop if n2 is also >= 0 you will do:
    Code:
    while (n1>=0 && n2>=0) {
      ...
    }
    And you HAVE to initialize n1 (and n2 if you use the above code). If you don't it will automatically initialized, so it might be <0 in the begining. You should do:
    Code:
    float n1=1, n2;

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Thanks C_ntua

    I continued work, and I think I have simplified it a bit:

    Code:
    int main(void) {
    	float n1, max=0;
    
    	while (n1 > 0)
    	{
    		printf("Enter any number: ");
    		fflush(stdout); /* Compiler won't stop to print line */
    		scanf("%f", &n1);
    		
    		if (n1 > max)
    			max = n1;
    	}		
    
    	printf("Largest number is: %f", max);
    
    	return EXIT_SUCCESS;
    But now it won't stop t ask me for a input?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    I don't want to initialize n1 since it can be any positive number and I risk initializing it higher than the inputted number.

    Code:
    int main(void) {
    	float n1, max=0;
    	
                    printf("Enter any number: ");
    	fflush(stdout);
    	scanf("&#37;f", &n1);
    
    	/* Putting the input outside the loop, I make sure n1 is never 
    	 * a random number and will always continue to the while loop*/
    	
    	while (n1 > 0)
    	{
    		printf("Enter any number: ");
    		fflush(stdout); /* Compiler won't stop to print line */
    		scanf("%f", &n1);
    		
    		if (n1 > max)
    			max = n1;
    	}		
    
    	printf("Largest number is: %f", max);
    
    	return EXIT_SUCCESS;
    I put another line outside the loop, but I think it looks a bit untidy... is there any way of simplifying this, so I don't have repeating-lines?

    edit: Oddly, it reads the decimal numbers as the highest numbers, if I type number like 25 and 0.25 it will read out the 0.25 as the higher number. If I only use integers, it works as intended?
    Last edited by Roffemuffe; 10-15-2008 at 07:17 AM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    You could initialise max to the same as the first number entered, and then at the end test to see if max is <= 0. If so, output a message saying that no numbers were input instead of the highest number.

    A variable must always have a known value before you test it .... attempting to test an undefined variable is asking for undefined behaviour

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    The part outside the while loop isn't used. As far as I can see, it reads a number, then goes to while-loop, reads a new number and works on that number, but forgets to work on the first number I typed in.

    I added a line to max = n1 and now is working as intended, thanks Driver!

    Code:
    int main(void)
    {
          int n1, max=0;
          printf("Enter any number (0 to end): ");
          fflush(stdout);
          scanf("%f", &n1);
    ->    max = n1;
    
    while ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM

Tags for this Thread