Array manipulation exercise While trying to learn more about arrays in C, I tried to write some code that did the following:


Read a stream of numbers from the stdin and store them in an array
Print the array in the order the numbers have been stored (i.e. print the original array)
Print the array in reversed order
Sort and print the array
Given an array, search whether an entered value is present in an array. If it is present, print the index otherwise print that the value doesn't exist.


It will be great if somebody could review it and let me know how to improve it.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

static void printArray(int array[],int startIndex,int endIndex){
    if(startIndex &lt; endIndex){
        while(startIndex &lt;= endIndex){
            printf(" %i ",array[startIndex++]);
        }
    }else{
        while(startIndex &gt;= endIndex){
            printf(" %i ",array[startIndex--]);
        }
    }
    printf("\n");
}

static int cmpfunc(const void * a,const void * b)
{
    if(*(int*)a &gt; *(int*)b) return 1; else return -1;
}

static void sortedArray(int* originalArray){
    qsort((void*)originalArray,(sizeof(originalArray)/sizeof(originalArray[0])),sizeof(originalArray[0]),cmpfunc);
    return;
}

static int getIndex(int value,int array[],int size){
    int i;
    for(i = 0;i &lt; size;i++){
        if(array[i] == value){
            return i;
        }
    }
    return -1;
}

static void identifyTheIndices(int *arbitraryArray,int size){
    char buf[10];
    printf("Enter the value to search for..enter q to exit\n");
    while(fgets(buf,sizeof buf,stdin) != NULL){
                if(buf[0] == 'q'){
            break;
        }
        char *end;
        int value = (int) strtol(buf,&amp;end,0);
        if(end != buf){
            int currentIndex = getIndex(value,arbitraryArray,size);
            if(currentIndex &gt; -1){
                printf("Found the entered value %i at index %i\n",value,currentIndex);
            }else{
                printf("Entered value %i doesn't exist\n",value);
            }
        }
    printf("Enter the value to search for..enter q to exit\n");
    }
}

int main(int argc,char **argv)
{
    int counter = 0;
    if(argc &gt; 1){
        int originalArray[argc-1];
        while(counter &lt; (argc - 1)){
            int currentValue = atoi(argv[counter+1]);
            printf("Reading input value %i into array \n",currentValue);
            originalArray[counter] = currentValue;
            counter++;      
        }
        int size = sizeof(originalArray)/sizeof(originalArray[0]);
        printf("Printing out the original array\n");
        printArray(originalArray,0,size - 1);
        printf("Printing out the array in reverse\n");
        printArray(originalArray,size - 1,0);
        printf("Sorting the array in ascending order\n");
        qsort((void*)originalArray,size,sizeof(originalArray[0]),cmpfunc);
        printf("Printing out the sorted array\n");
        printArray(originalArray,0,size-1);
        int arr[] = { 47, 71, 5, 58, 95, 22, 61, 0, 47 };
        identifyTheIndices(arr,sizeof(arr)/sizeof(arr[0]));
    }
    return 0;
}
@sc_ray pretty good effort if you are new to C programming.

A few points in your code which you can improve:


Taking array values from command-line is not good. Its better you use scanf and proper data-type. Below are logs when I pass a very long int value as cmd-line argument. It fails to store the value and gives garbage.

./a.out 7 999999999999 0 123
Reading input value 7 into array
Reading input value 2147483647 into array
Reading input value 0 into array
Reading input value 123 into array
Printing out the original array
7  2147483647  0  123
.....

Hope this is a code pasting mistake. Remove this arr array not needed.

int arr[] = { 47, 71, 5, 58, 95, 22, 61, 0, 47 };
identifyTheIndices(arr,sizeof(arr)/sizeof(arr[0]));

The searching of elements in array can be improved using Binary Search Algo rather than sequential search.


Rest all looks good.