Project 4 Solution

Project 4 Solution - / Project 4 Solution #include ...

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

View Full Document Right Arrow Icon
// Project 4 Solution #include <string> using namespace std; int tally(const string a[], int n, string target) { if (n < 0) return -1; int count = 0; for (int k = 0; k < n; k++) if (a[k] == target) count++; return count; } int findFirst(const string a[], int n, string target) { if (n < 0) return -1; for (int k = 0; k < n; k++) if (a[k] == target) return k; return -1; } int positionOfMin(const string a[], int n) { if (n <= 0) return -1; int minPos = 0; // assume to start that min is at index 0 for (int k = 1; k < n; k++) if (a[k] < a[minPos]) minPos = k; return minPos; } int moveToEnd(string a[], int n, int pos) { if (n < 0 || pos < 0 || pos >= n) return -1;
Background image of page 1

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

View Full DocumentRight Arrow Icon
// save the element that is to be moved string toBeMoved = a[pos]; // shift o the left those elements past the one to be moved for (int k = pos; k < n-1; k++) a[k] = a[k+1]; // place the moved element at the end a[n-1] = toBeMoved; return pos; } int moveToBeginning(string a[], int n, int pos) { if (n < 0 || pos < 0 || pos >= n) return -1;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 6

Project 4 Solution - / Project 4 Solution #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