Write a function in c using for loops The function must not
Write a function in c++ using for loops:
The function must not use any function templates from the algorithms portion of the Standard C++ library.
bool locateSequence(const string a[], int n, string target, int& begin, int& end);
Find the earliest occurrence in a of one or more consecutive strings that are equal to target; set begin to the position of the first occurrence of target, set end to the last occurrence of target in that earliest consecutive sequence, and return true. If n is negative or if no string in a is equal to target, leave begin and end unchanged and return false. Here\'s an example:
Notwithstanding each function\'s behavior described below, all functions that return an int must return 1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we\'re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array.
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
using namespace std;
bool locateSequence(const string a[], int n, string target, int& begin, int& end); // function declaration
bool locateSequence(const string a[], int n, string target, int& begin, int& end) { // function definiton
   
 bool found = false; // locla varaibles
   
 if(n <= 0) { // check for the size of the array
 return false; // return false
 }
   
 for(int counter = 0; counter < n; counter++){ // loop over the size of the array
        if((a[counter] == target) && a[(counter + 1)] == target){ // check for the occurrence
            begin = counter; // assign the data
            end = (counter + 1);
            found = true;
            break; // break the loop
        } else if ((a[counter] == target)){ // if found only once
        begin = 1; // return the standard values
        end = 1;
        found = true;      
        } else {
        continue; // else loop over the data
        }
    }
   
    if(found == true) { // check for the flag
    return true;
    } else {
    return false;
    }
 }
int main() // driver method
 {
 string d[9] = {\"jon\", \"daenerys\", \"samwell\", \"samwell\", \"margaery\", \"margaery\", \"margaery\", \"samwell\", \"samwell\"}; // reuired array
 int b; // local variables
 int e;
 bool b1 = locateSequence(d, 9, \"samwell\", b, e); // returns true and sets b to 2 and e to 3
 cout << \"Found at \" << b << \" and \" << e << endl; // data to be printed
 bool b2 = locateSequence(d, 9, \"daenerys\", b, e); // returns true and sets b to 1 and e to 1
 cout << \"Found at \" << b << \" and \" << e << endl; // data to be printed
 bool b3 = locateSequence(d, 9, \"cersei\", b, e); // returns false and leaves b and e unchanged
 cout << \"Found at \" << b << \" and \" << e << endl; // data to be printed
   
 return 0;
 }
OUTPUT :
Found at 2 and 3
 Found at 1 and 1
 Found at 1 and 1
Hope this is helpful.


