I already have excercise two completed it is necessary to complete excercise 3 &
ID: 3673755 • Letter: I
Question
I already have excercise two completed it is necessary to complete excercise 3 & 4, i need help completing excercise 3 & 4 if my coding from excercise two is needed let me know and I will provide it and also please provide pseudocode and sample outputs thanks. e Bockmarks Help 16.729 (Exercise 2) Create-checkArray.cpp In this part of the lab, you will create a function checkArraySort 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) o 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 o O if the array is not sorted o 1 if the array is sorted in ascending order You can modify the main program that you developed in fileO.cpp to call checkArraysort, and to print out one of the following statements: .If -1 If O " If 1 The array is sorted in descending order!" "The array is not sorted!" "The array is sorted in ascending order!" to test your program. Note: In order to check the sorting of strings / words, you can use the comparisonExplanation / Answer
Algorithm:
---------------
Firstly we check for array is sorted or not
Then check for array size.. start index and last index.. Here condidtion is , first <= last
Then calculate for key matches with mid value, if found rerurn index
otherwise, check value is less than mid value.. then call recursively to search in left sub array
otherwise, check value is greater than mid value.. then call recursively to search in right sub array
implementation:
---------------------
int searchArray1(String *arr[], int first, int size, string key) {
if(first<=size && checkSort(arr,size) == 1){
int mid = (first+size)/2;
if(key == arr[mid]){ //if found at center
return mid;
}
else if(key < arr[mid]){ //if less than mid value
return searchArray1(arr,first,mid-1,key);
}
else{ //if greater than mid value
return searchArray1(arr,mid-1,size,key);
}
}
return -1; //key not found
}
---------------------------------------------------------------------------------------------------------------------------------
Algorithm:
----------------
check ffor array is sorted ..
while loop with condition, first never greater than size
Ten calculate middle value
if key less than middle, then update size to middle-1
if key is greater than middle, then update first = middle+1
if key found return index
if not found return -1;
------------------------------------
implementation:
------------------------------
int searchArray2(String *arr[], int first, int size, string key) {
{
if(checkSort(arr,size) == 1){
//check for first must not grater than size
while (first <= size)
{
//calculating mid value
int middle = (first + size) / 2;
// if key in left sub array
if (key < A[middle])
{
//set size index value
size = middle - 1;
}
// if key in right sub array
else if (key > A[m])
{
//set first index value
first = middle + 1;
}
//if key found
else
{
return middle;
}
}
}
//no key found
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.