{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

array

# array -...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #include <iostream> #include <string> #include <cassert> using namespace std; int tally(const string a, int n, string target) { if (n < 0) return -1; if (n == 0) return 0; int k=0; //initialize k to represent the tally int i; for(i=0; i<n; i++) { if (a[i] == target) k++; } return k; } int findFirst(const string a, int n, string target) //works well { if(n<0) return -1; if (n == 0) return 0; for(int i=0; i<n; i++) { if (a[i] == target) return i; } return -1; } bool findFirstSequence(const string a, int n, string target, int& begin, int& end) { if(n<0) return false; bool hasSequence = false; // used this bool statement to determine whether has sequence or not for(int i=0; i<n; i++) { if (i != (n-1) && a[i] == target && a[i+1] == target) // for strings that actually have consecutivity { // have the i!= n-1 otherwise it will think that it is part of a sequence hasSequence = true; begin = i; int r = 0; while (a[i+r] == target && (i+r) < n) { r++; } if ((i+r) == n) // this if statement is necessary in order to avoid a segmentation fault { end = i+r-1; return true; } end = i+r-1; //needs the minus one because it adds an r++ even for the last time return true; } } if(hasSequence == false) { for(int j=0; j<n; j++) // what to do if there is only one, no sequence { if (a[j] == target) { begin = j; end = j; return true; } } } return false; } int positionOfMin(const string a, int n) { if(n<=0) return -1; if (n == 1) return 0; int lowest =0; // will be used to represent the current minimum value for(int i=0; i<n; i++)...
View Full Document

{[ snackBarMessage ]}

### Page1 / 7

array -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online