Thread: fix this code

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    fix this code

    The user need to type largest (3,7,11,9) at the prompt. It means that I have 3 numbers in the array and I need to find the largest integer in the array containing 7, 11, 9.

    I was able to get the program to read 3 as teh size of the array but how can I instruct the program to put the 7, 11, 9 in the array??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	char *nargs;
    	int num[sizeof nargs];
    	int largestsofar = 0; 
        int i;
    	
    	if (argc==2)
            {
    		nargs=malloc(strlen(argv[1]+1));
    	    strcpy (nargs, argv[1]);
    		}
    
    	for(i=0;i<sizeof(nargs)/sizeof(char);i++) 
    	{ 
    		if (largestsofar < num[i]) 
    			largestsofar = num[i];
    	}
    
    	printf("The largest integer is: %d\n",largestsofar);
    	printf("%d",nargs);
    
    	free(nargs);
       
        return 0;
    }

  2. #2
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    thanks for the help.....but what if I want the user to type parenthesis like largest (4,5,6,7,9)

    your code get rids of the commas. I tried to get rid of the () but it does not seem to work, I was trying to add && || to the code!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to fix code
    By anik18 in forum C Programming
    Replies: 19
    Last Post: 05-29-2009, 06:06 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Replies: 1
    Last Post: 05-26-2006, 08:13 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM