Back to all questions
Question

C++ computer science help with arrays

//Write the function called ReplaceVariable in the given interval such that

// output : you decide what is the output

// input : an array of int, the target value we are searching for, an int which is the starting index of search, an int which is the ending point of searchspace

// function: Search in the array from given start to end index and replace the given target value with 1000

Answer

create a method with a signature of array pointer of type int and three more parameters of type int. Call the method with the int main() method passing in a number of int values as the array from standard input or statically defined. Collect three more int values as starting and ending index into the array.

Within the new function ensure that both the beginning and ending index are within the bounds of the size of the passed array.

Within the function define a for or while loop that starts and ends at the specificed beginning and ending index. Iterate the start index variable with a post process ++ operator within the for or while loop for each iteration after comparing the array value at that index to the target value.

If at any time during the comparison if the array value at index x is equal to the target value then assign the array value at that index to 1000 with an equal statement and then break the loop if we are just looking for the first instance. With small arrays this is the approach that works the fastest as a linear search. For larger arrays a DIVIDE & CONQUER algorithm is the better approach by using multiple arrays to do parallel searching within the bounds of the start and end index. Therefore your loop would change to start index of 0 and an end index of the length of the multiple sub arrays and global index location being a simple math addition given the location within the specific sub array.