The fault he pointed out is caused by not validatingvalidating the input values. The
same error could occur isif the value was input using scanf - and you saw in
one of your earlier questions that scanf has its own issues. To check the
input values you can use
printArraywould be more normal taking aconststart point and a length.static void printArray(const int array[], size_t n);and have another
printReverseArrayto print the reveredreversed array.cmpfuncshould return 0 if the items match.sortedArrayis unused. It is also wrong in that(sizeof(originalArray)/sizeof(originalArray[0])),gives something meaningless when
originalArrayis a pointer.sizeof(originalArray)gives the size of the pointer, not the array that it points to.Later on in
main, you do it right, withint size = sizeof(originalArray)/sizeof(originalArray[0]);because here
originalArrayis a real array, not a pointer. But you already had the array size (argc - 1), so this was unnecessary.the
arrayparameter togetIndexshould beconst. But the function is unreliable, as it fails for negative numbers. To make it work you need to separate the return value from the success/failure.static int getIndex(int value, int array[], int size, int *index);add some spaces, eg after
if,while,for, ';';and,etc.you use the variable names
array,originalArrayandarbitraryArrayto identify essentially the same thing - an array. Don't use multiple names for equivalent things without good reason.Your reading loop
int counter = 0; ... int originalArray[argc-1]; while(counter < (argc - 1)){ int currentValue = atoi(argv[counter+1]); printf("Reading input value %i into array \n",currentValue); originalArray[counter] = currentValue; counter++; }would be neater as:
--argc; int array[argc]; for (int i = 0; i < argc; ++i) { int v = atoi(argv[i + 1]); printf("Reading input value %i into array \n", v); array[i] = v; }shorter variable names are ok, and indeed preferable, where their scope is restricted.
would be neater as:
--argc;
int array[argc];
for (int i = 0; i < argc; ++i) {
int v = atoi(argv[i + 1]);
printf("Reading input value %i into array \n", v);
array[i] = v;
}
shorter variable names are ok, and indeed preferable, where their scope is restricted.