+1 (315) 557-6473 

C++ Program to Manipulate Arrays Assignment Solution.


Instructions

Objective
Write a C++ assignment program to manipulate arrays in various ways.

Requirements and Specifications

Your assignment is to produce a library that provides functions for many common manipulations of arrays of strings. For example, one function will find where a string occurs in an unordered array of strings. Another will change the order of strings in an array. For each function you must write, this specification will tell you its interface (what parameters it takes, what it returns, and what it must do). It's up to you to decide on the implementation (how it will do it). The source file you turn in will contain all the functions and a main routine. You can have the main routine do whatever you want, because we will rename it to something harmless, never call it, and append our own main routine to your file. Our main routine will thoroughly test your functions. You'll probably want your main routine to do the same. If you wish, you may write functions in addition to those required here. We will not directly call any such additional functions. If you wish, your implementation of a function required here may call other functions required here.
The program you turn in must build successfully, and during execution, no function (other than main) may read anything from cin or write anything to cout.
Source Code
#include
#include
#include
using namespace std;
int enumerate(const string a[], int n, string target) {
 if (n < 0) {
  return -1;
 }
 int counter = 0;
 for(int i = 0; i
  if (a[i] == target) {
   counter++;
  }
 }
 return counter;
}
int locate(const string a[], int n, string target) {
 if (n < 0) {
  return -1;
 }
 for(int i = 0; i
  if (a[i] == target) {
   return i;
  }
 }
 return -1;
}
bool locateSequence(const string a[], int n, string target, int& begin, int& end) {
 if (n < 0) {
  return false;
 }
 int start = -1;
 for(int i = 0; i
  if (start >= 0) {
   if (a[i] != target) {
    begin = start;
    end = i-1;
    return true;
   }
  }
  else {
   if (a[i] == target) {
    start = i;
   }
  }
 }
 if (start >= 0) {
  begin = start;
  end = n-1;
  return true;
 }
 return false;
}
int locationOfMin(const string a[], int n) {
 if (n <= 0) {
  return -1;
 }
 string mins;
 int pos = -1;
 for(int i = 0; i
  if (pos == -1 || a[i] < mins) {
   mins = a[i];
   pos = i;
  }
 }
 return pos;
}
int moveToEnd(string a[], int n, int pos) {
 if (pos < 0 || pos >= n) {
  return -1;
 }
 string val = a[pos];
 for(int i = pos; i
  a[i] = a[i+1];
 }
 a[n-1] = val;
 return pos;
}
int moveToBeginning(string a[], int n, int pos) {
 if (pos < 0 || pos >= n) {
  return -1;
 }
 string val = a[pos];
 for(int i = pos; i>0; i--) {
  a[i] = a[i-1];
 }
 a[0] = val;
 return pos;
}
int locateDifference(const string a1[], int n1, const string a2[], int n2) {
 for(int i = 0; i
  if (a1[i] != a2[i]) {
   return i;
  }
 }
 return min(n1, n2);
}
int eliminateDups(string a[], int n) {
 if (n < 0) {
  return -1;
 }
 int retained = n;
 for(int i = 0; i
  string val = a[i];
  for(int j = i+1; j
   if (val == a[j]) {
    moveToEnd(a, retained, j);
    retained--;
    j--;
   }
  }
 }
 return retained;
}
bool subsequence(const string a1[], int n1, const string a2[], int n2) {
 if (n1 < 0 || n2 < 0) {
  return false;
 }
 if (n2 == 0) {
  return true;
 }
 int match = 0;
 for(int i = 0; i
  if (a1[i] == a2[match]) {
   match++;
  }
 }
 return match == n2;
}
int makeMerger(const string a1[], int n1, const string a2[], int n2, string result[], int max) {
 if (n1 < 0 || n2 < 0 || n1+n2 > max) {
  return -1;
 }
 int i1 = 0;
 int i2 = 0;
 while(i1 < n1 || i2 < n2) {
  if (i1 < n1 && i2 < n2) {
   if (a1[i1] < a2[i2]) {
    result[i1+i2] = a1[i1];
    i1++;
   }
   else {
    result[i1+i2] = a2[i2];
    i2++;
   }
  }
  else if (i1 < n1) {
   result[i1+i2] = a1[i1];
   i1++;
  }
  else {
    result[i1+i2] = a2[i2];
    i2++;
  }
 }
 return n1 + n2;
}
int divide(string a[], int n, string divider) {
 if (n < 0) {
  return -1;
 }
 int i = 0;
 int j = n-1;
 while(i < j) {
  while(a[i] < divider) {
   i++;
  }
  while(a[j] >= divider) {
   j--;
  }
  if (i < j) {
   string sw = a[i];
   a[i] = a[j];
   a[j] = sw;
   i++;
   j--;
  }
 }
 int result = i;
 i = result;
 j = n-1;
 while(i < j) {
  while(a[i] <= divider) {
   i++;
  }
  while(a[j] > divider) {
   j--;
  }
  if (i < j) {
   string sw = a[i];
   a[i] = a[j];
   a[j] = sw;
   i++;
   j--;
  }
 }
 return result;
}
int main() {
 string h[7] = { "samwell", "jon", "margaery", "daenerys", "", "tyrion", "margaery" };
 assert(enumerate(h, 7, "margaery") == 2);
 assert(enumerate(h, 7, "") == 1);
 assert(enumerate(h, 7, "sansa") == 0);
 assert(enumerate(h, 0, "margaery") == 0);
 assert(locate(h, 7, "margaery") == 2);
 assert(locate(h, 2, "margaery") == -1);
 int bg;
 int en;
 assert(locateSequence(h, 7, "daenerys", bg, en) && bg == 3 && en == 3);
 string g[4] = { "samwell", "jon", "daenerys", "tyrion" };
 assert(locationOfMin(g, 4) == 2);
 assert(locateDifference(h, 4, g, 4) == 2);
 assert(subsequence(h, 7, g, 4));
 assert(moveToEnd(g, 4, 1) == 1 && g[1] == "daenerys" && g[3] == "jon");
 string f[4] = { "daenerys", "tyrion", "margaery", "jon" };
 assert(moveToBeginning(f, 4, 2) == 2 && f[0] == "margaery" && f[2] == "tyrion");
 string e[5] = { "daenerys", "daenerys", "daenerys", "margaery", "margaery" };
 assert(eliminateDups(e, 5) == 2 && e[1] == "margaery");
 string x[4] = { "cersei", "jon", "jon", "sansa" };
 string y[4] = { "daenerys", "jon", "margaery", "tyrion" };
 string z[10];
 assert(makeMerger(x, 4, y, 4, z, 10) == 8 && z[5] == "margaery");
 assert(divide(h, 7, "margaery") == 3);
 cout << "All tests succeeded" << endl;
}