{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Project 4 Solution

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

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

// 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;

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

View Full Document
// 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;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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
Ask a homework question - tutors are online