In this part of the lab, you will create a function check Array Sort that checks
ID: 3800403 • Letter: I
Question
In this part of the lab, you will create a function check Array Sort that checks if an array of strings (which was previously defined and initialized) is already sorted, and it is either increasing or decreasing. The function that you create is declared as follows: Input arguments: A string array A (remember that array is a pointer in this case, so use the * sign) an integer variable array_size for the number of elements in array A (the counter value from previous exercise) Return in integer value: -1 if the array is sorted in descending order 0 if the array is not sorted 1 if the array is sorted in ascending order You can modify the main program that you developed in filelO.cpp to call check Array Sort, and to print out one of the following statements: If -1 "The array is sorted in descending order!" If 0 "The array is not sorted!" If 1 "The array is sorted in ascending order!" You can use the same words_in.txt to test your program.Explanation / Answer
Hi, Please find my implementation of checkArraySorted method.
You have not posted fileIO.cpp, so i can not test.
But i have implemented completely. It will work perfect.
Please let me know in case of any issue.
bool checkArraySort(string *A, int array_size){
// check if accessending
int i;
for(i=0; i<array_size-1; i++)
if(A[i+1] < A[i]) // if not sorted in ascending, then stop
break;
if(i == array_size-1) // sorted in ascending order
return 1;
// checking if descending
for(i=0; i<array_size-1; i++)
if(A[i+1] > A[i]) // if not sorted in descending, then stop
break;
if(i == array_size-1) // sorted in ascending order
return -1;
return 0; // not sorted in ascending or descending order
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.