Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 10 Nov 2006 Example using dynamic arrays.

Similar presentations


Presentation on theme: "CS 1400 10 Nov 2006 Example using dynamic arrays."— Presentation transcript:

1 CS 1400 10 Nov 2006 Example using dynamic arrays

2 Problem: Find the mode of an array The mode of a list of numbers is defined as the number that occurs most frequently in the list Initial pseudocode; allocate a new array big enough for the list read the list of numbers into the array find and output the mode of the array deallocate the array

3 First refinement… int size, *array; cout << “Enter size of the list: “; cin >> size; array = new int[size]; read the list of numbers into the array find and output the mode of the array delete[] array;

4 Second refinement… int main() {int size, *array; cout << “Enter size of the list: “; cin >> size; array = new int[size]; FillArray(array, size); cout << “Mode is: “ << FindMode(array, size); delete[] array; }

5 read the list of numbers into the array void FillArray (int *p, int size) {cout << “Enter “ << size << “ values: “; for (int n=0; n<size; n++) cin >> p[n]; }

6 find and output the mode of the array int FindMode (int *p, int size) {allocate a same-sized array to hold value counts for each value in p; count the number of times this value occurs in p and save in counts array find the position of the maximum in counts array deallocate the counts array return the value in p at this position }

7 First refinement… int FindMode (int *p, int size) {int *counts, value, position; counts = new int[size]; for each value in p; count the number of times this value occurs in p and save in counts array find the position of the maximum in counts array delete[] counts; return p[position]; }

8 Second refinement… int FindMode (int *p, int size) {int *counts, value, position; counts = new int[size]; for (int n=0; n<size; n++) {count the number of times this value occurs in p and save in counts array } find the position of the maximum in counts array delete[] counts; return p[position]; }

9 Third refinement… int FindMode (int *p, int size) {int *counts, value, position; counts = new int[size]; for (int n=0; n<size; n++) {value = p[n]; counts[n] = CountValue(p, size, value); } find the position of the maximum in counts array delete[] counts; return p[position]; }

10 Fourth refinement… int FindMode (int *p, int size) {int *counts, value, position; counts = new int[size]; for (int n=0; n<size; n++) {value = p[n]; counts[n] = CountValue(p, size, value); } position = FindPosOfMax(counts, size); delete[] counts; return p[position]; }

11 count number of times value occurs in p int CountValue (int *q, int size, int value) {int count = 0; for each value in array q; if this cell of array q equals value count++; return count; }

12 First refinement… int CountValue (int *q, int size, int value) {int count = 0; for (int n=0; n<size; n++) if (q[n] == value) count++; return count; }

13 find the position of the maximum in w int FindPosOfMax (int *w, int size) {int position = 0, max = w[0]; for each remaining cell in array w (past index 0) if this cell is greater than max; set max to this cell set position to this index return position; }

14 First refinement… int FindPosOfMax (int *w, int size) {int position = 0, max = w[0]; for (int n=1; n<size; n++) if (w[n] > max) {set max to this cell set position to this index } return position; }

15 Second refinement… int FindPosOfMax (int *w, int size) {int position = 0, max = w[0]; for (int n=1; n<size; n++) if (w[n] > max) {max = w[n]; position = n; } return position; }


Download ppt "CS 1400 10 Nov 2006 Example using dynamic arrays."

Similar presentations


Ads by Google