1 #include 2 using namespace std; 7 8 /** 15 ) 23 24 "/ 12 int find next(int values[], int size, int searched value, int start) Finds the next occurrence of an element in an array. @param values an array of values @param searched value the value to search for @param start the position at which to start the search @return the position of the first match at position > start, or 1 if the element was not found 27 28 29 } int main() int a[] (3, 1, 4, 1, 5, 9, 2, 6); int found at find next(a, 8, 1, 2); cout << "find next(3, 1, 4, 1, 5, 9, 2, 6, 8, 1, 2) << endl; << "Found at: found at << endl; cout << endl; cout << "find next({ 3, found at 1, 4, 1, 5, 9, 2, 6), 8, 7, 3) << endl; find next (a, 8, 7, 3); cout << "Found at: << found at << endl; return 0;

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 9PE
icon
Related questions
Question
In Section 6.2.7, you learned how to find the position of an element in an array. Suppose you want to find the position of the second match. You could remove the first match and start over, but that would be wasteful. Instead, adapt the algorithm of Section 6.2.7 so that it starts the search at a
given position, not zero. Complete the following code.
find_next.cpp
10
20
53
#include <iostream>
using namespace std;
/**
24
Finds the next occurrence of an element in an array.
@param values an array of values
*/
int find_next(int values [], int size, int searched_value, int start)
{
15 }
@param searched value the value to search for
@param start the position at which to start the search
CodeCheck
@return the position of the first match at position > start,
or -1 if the element was not found
int main()
{
int a[] = { 3, 1, 4, 1, 5, 9, 2, 6 };
int found at = find next (a, 8, 1, 2);
cout << "find_next({ 3, 1, 4, 1, 5, 9, 2, 6, 8, 1, 2)" <<< endl;
cout << "Found at: <<found_at << endl;
cout << endl;
cout << "find_next({ 3, 1, 4, 1, 5, 9, 2, 6, 8, 7, 3)" << endl;
found at find next (a, 8, 7, 3);
cout << "Found at: <<found_at << endl;
return 0;
Reset
Transcribed Image Text:In Section 6.2.7, you learned how to find the position of an element in an array. Suppose you want to find the position of the second match. You could remove the first match and start over, but that would be wasteful. Instead, adapt the algorithm of Section 6.2.7 so that it starts the search at a given position, not zero. Complete the following code. find_next.cpp 10 20 53 #include <iostream> using namespace std; /** 24 Finds the next occurrence of an element in an array. @param values an array of values */ int find_next(int values [], int size, int searched_value, int start) { 15 } @param searched value the value to search for @param start the position at which to start the search CodeCheck @return the position of the first match at position > start, or -1 if the element was not found int main() { int a[] = { 3, 1, 4, 1, 5, 9, 2, 6 }; int found at = find next (a, 8, 1, 2); cout << "find_next({ 3, 1, 4, 1, 5, 9, 2, 6, 8, 1, 2)" <<< endl; cout << "Found at: <<found_at << endl; cout << endl; cout << "find_next({ 3, 1, 4, 1, 5, 9, 2, 6, 8, 7, 3)" << endl; found at find next (a, 8, 7, 3); cout << "Found at: <<found_at << endl; return 0; Reset
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT