array

array - #include <iostream> #include...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
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

This note was uploaded on 11/10/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 7

array - #include <iostream> #include...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online