Modify the code to display their respective arrays one line at a time using the
ID: 3663754 • Letter: M
Question
Modify the code to display their respective arrays one line at a time using the index as a label for the index’s value. /* The purpose of this program is to demonstrate linear search. We will create an array and fill it with random numbers and then search for particular numbers in the array. */ #include <iostream> // Used for input and output. #include <conio.h> // Used for getch(). #include <cstdlib> // Used for random number generation. #include <time.h> // Used to seed the random number generator. using namespace std; // Forward declarations of functions. void pressAnyKey(); void displayArray(int array[], int length); void randomizeArray(int array[], int length); int linearSearch(int array[], int arraySize, int target); void searchAndDisplayItem(int array[], int arraySize, int target); // Global variables and constants. const int LENGTH = 100; // The length of our demonstration array. int main() { int demo[LENGTH]; // Declare, initialize, and display array. displayArray(demo, LENGTH); pressAnyKey(); // Pause to take in the experience :-). randomizeArray(demo, LENGTH); // Randomize values in array and display. displayArray(demo, LENGTH); pressAnyKey(); // Search for some selected values in our array and display results. int index = linearSearch(demo, LENGTH, 37); if (index >= 0) { cout << "The value 37 was found at index " << index << ". "; } else { cout << "The value 37 was not found in the array. "; } // Use helper function for linear search. searchAndDisplayItem(demo, LENGTH, 43); searchAndDisplayItem(demo, LENGTH, 47); searchAndDisplayItem(demo, LENGTH, 53); pressAnyKey(); // End main function and end program. return 0; } // Display values of array - each on their own line. void displayArray(int array[], int length) { int i = 0; while (i < length) { cout << array[i] << " "; i++; } cout << endl; } // Give a random value to each element of the array. void randomizeArray(int array[], int length) { srand ( (unsigned) time(NULL)); // Seed random number generator. int i = 0; do { array[i] = rand() % 100 + 1; // A random number in the range of 1 to 100 is assigned. i++; } while (i < length); } int linearSearch(int array[], int arraySize, int target) { // Go through each index, one at a time sequentially and check to see if the index's value is the target. for (int index = 0; index < arraySize; index++) { if (array[index] == target) { return index; // Return the index of the found target. } } return -1; // Return -1 as a flag to indicate that the target was not found. } void searchAndDisplayItem(int array[], int arraySize, int target) { // Search for some selected values in our array and display results. int index = linearSearch(array, arraySize, target); if (index >= 0) { cout << "The value " << target << " was found at index " << index << ". "; } else { cout << "The value " << target << " was not found in the array. "; } } // Utility functions. // Press any key to continue. void pressAnyKey() { cout << "Press any key to continue" << endl; _getch(); // Waits and gets next character entered. }
Explanation / Answer
Here is the modified display function
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.