Thread: Assigning values from argv[]

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Oregon, USA
    Posts
    9

    Assigning values from argv[]

    Hi,

    I'm currently working on a program that accepts user input on the command line then outputs the mean, median,
    mode, and variance of the data. I'm not concerned with how to do the calculations for the output, however when I'm encountering a problem when I try to use atoi() to assign the strings entered to a single dimensional array to be passed to the function to put all the numbers entered into order. At this point the only function I've finished is sort() which sorts them into order.

    Example of ideal i/o:

    Input: ./mmm 15 18 13 17 18
    Output: 15 18 13 17 18

    Example of actual i/o:

    Input: ./mmm 15 18 13 17 18
    Output: 0 0 0 0 0 0

    Thanks for any help you can give. And I apologize if it's not the best written program, I'm still a beginnning programmer and am bound to make a lot of mistakes.

    - London Fog

    Code:
    int main( int argc, char *argv[])
    {
    	int args_main[MAX]; /* Array used to hold user input. (MAX is defined as 128 in common.h) */
    	int *args_main_p = &args_main[0]; /* Pointer used for allocation of memory */
    	int control_1  = 0, control_2 = 1; /* Variable used for program control (ie. For loops etc) */
    
    	args_main_p = (int*) malloc(MAX*sizeof(int)); /* Allocating memory for user input */
    
    	if( args_main_p != NULL ) /* If memory allocates successfully */
    	{
    
    		for( control_1 = 0 ; control_1 < MAX ; control_1++) // Initializing array
    		{
    			args_main[control_1] = 0;
    
    		}
    	
    		if( argc < 2 ) /* If no arguments are entered */
    		{
    			printf("\n Enter --help for help. (Duh)\n\n");
    			exit(1);
    		}
    	
    		if( argv[1][0] == '-' && argv[1][1] == '-' && argv[1][2] == 'h' && argv[1][3] == 'e' && argv[1][4] =='l' )
    		{
    			printf("\n Enter at least between 2 and 128 integers seperated by spaces.");
    			printf("\n The program will output the mean, median, mode");
    			printf("\n and variance of those integers.");
    			printf("\n Ex Input: 15 18 13 17 18 \n");
    			printf("\n Ex Output: \n mean: 16.2 \n median: 17 \n mode: 18 \n variance: 0 \n");
    			exit(1);
    		}
    
    		do
    		{
    			args_main[control_1] = atoi(argv[control_2]);
    			control_1++; control_2++;
    		} while( atoi(argv[control_2]) >= 1 && atoi(argv[control_2]) <= 9 ); 	
    
    		control_1 = 0;
    			
    		do						// Checking to make sure user
    		{					       // values were passed to args_main
    			printf("%d  ", args_main[control_1]); // correctly
    			control_1++;
    		} while( control_1 < argc-1 );	
    	
    		printf("\n Calling function sort() ");
    	
    		sort( args_main, argc ); /* Call function sort */
    		
    		return 0;
    	}
    	else /* If memory allocation fails */
    	{
    		printf("\n Memory allocation err. Killing program.\n");
    		exit(-1);
    	}
    }
    Last edited by London Fog; 12-16-2009 at 01:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Multiple Values to Array/ Vector
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2009, 01:15 PM
  2. Question about assigning values
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 07:36 AM
  3. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  5. Assigning values to arrays
    By napkin111 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2003, 08:52 PM