Write a function in c using for loops and give a list of 2 t

Write a function in c++ using for loops and give a list of 2 test data:

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:

string d[9] = { \"jon\", \"daenerys\", \"samwell\", \"samwell\", \"margaery\", \"margaery\", \"margaery\", \"samwell\", \"samwell\" };

int b;

int e;

bool b1 = locateSequence(d, 9, \"samwell\", b, e); // returns true and // sets b to 2 and e to 3

bool b2 = locateSequence(d, 9, \"daenerys\", b, e); // returns true and // sets b to 1 and e to 1

bool b3 = locateSequence(d, 9, \"cersei\", b, e); // returns false and // leaves b and e unchanged 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.

Check the function by using the following assert statement in int main:

Solution

// C++ code

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;

bool locateSequence(const string a[], int n, string target, int& begin, int& end)
{
   bool flag = false;

   if (n < 0)
       return -1;
  
   for (int i = 0; i < n; i++)
   {
        if(flag && a[i]!=target)
        {
            return true;
        }
       if (a[i] == target)
       {
            end = i;
            if (!flag)
           {
               begin = i;
               flag = true;
           }
       }
   }
   return flag;
}

int main ()
{
    string d[9] = { \"jon\", \"daenerys\", \"samwell\", \"samwell\", \"margaery\", \"margaery\", \"margaery\", \"samwell\", \"samwell\" };

   int b;
   int e;

   bool b1 = locateSequence(d, 9, \"samwell\", b, e); // returns true and // sets b to 2 and e to 3
   cout << b1 << endl;  
   // output: 1
   bool b2 = locateSequence(d, 9, \"daenerys\", b, e); // returns true and // sets b to 1 and e to 1
   cout << b2 << endl;
   // output: 1
   bool b3 = locateSequence(d, 9, \"cersei\", b, e); // returns false
   cout << b3 << endl;
   // output: 0

   string h[7] = { \"samwell\", \"jon\", \"margaery\", \"daenerys\", \"\", \"tyrion\", \"margaery\" };
    int bg;
    int en;
    assert(locateSequence(h, 7, \"daenerys\", bg, en) && bg == 3 && en == 3);

return 0;
}

Write a function in c++ using for loops and give a list of 2 test data: The function must not use any function templates from the algorithms portion of the Stan
Write a function in c++ using for loops and give a list of 2 test data: The function must not use any function templates from the algorithms portion of the Stan

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site