Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE 20
- int getMode(int* A)
- {
- int maxNumIndex = 0;
- for (int i = 1; i < SIZE; i++)
- if (A[i] > A[maxNumIndex])
- maxNumIndex = i;
- int* B = new int[A[maxNumIndex]];
- for (int i = 0; i < A[maxNumIndex]; i++)
- B[i] = 0;
- for (int i = 0; i < A[maxNumIndex]; i++)
- B[A[i]]++;
- int modeIndex = 0;
- for (int i = 1; i < SIZE; i++)
- if (B[i] > B[modeIndex])
- modeIndex = i;
- return modeIndex;
- }
- int main()
- {
- srand(time(NULL));
- int* A = new int[SIZE];
- for (int i = 0; i < SIZE; i++)
- {
- A[i] = (1 + rand() % 20); // [0:20]
- printf("%4d", A[i]);
- if (i % 10 == 0) printf("\n");
- }
- printf("\n");
- printf("Mode is:\t%d\n", A[getMode(A)]);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment