Thread: max number program not functioning properly

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    13

    max number program not functioning properly

    Code:
    #include <stdio.h>
    
    
    
    void main () {
    	int num[5];
    	int index;
    	
    	printf("\nEnter 5 numbers : \n");
    
    	for(index = 0; index < 5; index++ )
    	{
    		scanf("%d",&num[index]);
    	}
    	
    	int max = num[0];
    	
    	for (index = 0; index < 5; index++)
    	{
    		if(max < num[index]);
    		{
    			max = num[index];
    		}
    	}
    	
    	printf("\nThe largest number you entered was : %d\n",max);
    	
    }
    I made this and compiled it and it seemed to be working sometimes, like i would enter 5 random numbers and i would get the right answer but other times it will just give me the last number i entered. and now all it does is give me the last number i enter ( of the 5). any suggestions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see how that code would not always work. There is nothing wrong with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    :S , maybe i will try to compile it using something else, i'll let you know
    Last edited by MaaaTtY; 03-02-2009 at 10:55 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Look closer:

    Code:
    		if(max < num[index]);
    		{
    			max = num[index];
    		}
    Turning on compiler warnings may help...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    haha, woops =]

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Side note:

    Don't use void main. Use int main instead.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM