patterncMinor
Array manipulation exercise
Viewed 0 times
arrayexercisemanipulation
Problem
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
-
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
#include
static void printArray(int array[],int startIndex,int endIndex){
if(startIndex = endIndex){
printf(" %i ",array[startIndex--]);
}
}
printf("\n");
}
static int cmpfunc(const void a,const void b)
{
if((int)a > (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 -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 > 1){
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++;
}
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");
-
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
#include
static void printArray(int array[],int startIndex,int endIndex){
if(startIndex = endIndex){
printf(" %i ",array[startIndex--]);
}
}
printf("\n");
}
static int cmpfunc(const void a,const void b)
{
if((int)a > (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 -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 > 1){
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++;
}
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");
Solution
@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.
-
Hope this is a code pasting mistake. Remove this
-
The searching of elements in array can be improved using Binary Search Algo rather than sequential search.
Rest all looks good.
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.
Code Snippets
./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
.....int arr[] = { 47, 71, 5, 58, 95, 22, 61, 0, 47 };
identifyTheIndices(arr,sizeof(arr)/sizeof(arr[0]));Context
StackExchange Code Review Q#40141, answer score: 4
Revisions (0)
No revisions yet.