Hi, this is my problem that I have to do.
Write a C program thatsorts an unsorted array of 5 integer numbers in ascending order byswapping numbers repeatedly. Specifically, first prompt the user toinput 5 numbers and store the numbers in an array. Then, if thenumbers in the array are not in ascending order, ask the user toprovide the indices of two numbers in the array that the user wantsto swap. If the user does not enter valid indices (i.e., outside 0 to4), then re-prompt the user to enter new indices. Continue promptingthe user to provide indices of numbers to swap until the arraybecomes sorted in ascending order. Finally, when the array becomessorted, the program should print “The array is now sorted inascending order”.
ImplementationRequirements
- Use the #define directive to define the size of the array.
- Implement a function called “checkArrayOrder” to check whether an array is in ascending order or not. The prototype of the function is
“intcheckArrayOrder(int arr[], int size);”. The function should return
- 1 if the array passed to the function is in ascending order
- 0 if the array passed to the function is not in ascending order
Define the function prototype in your program.
- Implement a function called “swap”, whose prototype is
“intswap(int arr[], int size, int i, int j);” which swaps the numbersin positions “i” and “j” of the array passed to the functionand returns
- 1 if the indices “i” and “j” are within bounds (i.e., between 0 and 4) and the swap operation is performed correctly.
- 0 if the indices “i” and “j” are outside bounds so that the swap operation cannot be performed.
Define the function prototype in your program.
- Use the function “checkArrayOrder” whenever your program needs to check whether an array is in ascending order or not; and the function “swap” to swap the numbers the user specifies (Note that, you should check whether the user provided indices are valid inside the function “swap” and not inside “main”).
here is my code so far
I'm just not sure how to proceed.Code:#define ARRAY_SIZE 5 void main() { int i, array[ARRAY_SIZE]; printf("Enter %d numbers\n", ARRAY_SIZE); for(i=0; i<ARRAY_SIZE; ++i) scanf("%d", &array[i]); } int checkArrayOrder(int array[], int size) { for (int i=0; i<size-1; i++) { if (array[i] > array[i+1]) { } else { printf("The array is now in ascending order."); } } } int swap(int array[], int size, int i, int j) { for (int i=0; i<size-1; i++) { if ((i||j < 0) || (i||j > 4)) { printf("Invalid Indices."); } else { size=0; size = array[i]; array[i] = array[j]; array[j] = size; } }
I'm not really sure how to progress the checkArrayOrder function to the swap function (in the if statement).
If anybody could help me with this it would be much appreciated.
Thank you for all your help in advance.