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); } }