vakho

MODA

Oct 25th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 20
  6.  
  7. int getMode(int* A)
  8. {
  9.     int maxNumIndex = 0;
  10.     for (int i = 1; i < SIZE; i++)
  11.         if (A[i] > A[maxNumIndex])
  12.             maxNumIndex = i;
  13.  
  14.     int* B = new int[A[maxNumIndex]];
  15.     for (int i = 0; i < A[maxNumIndex]; i++)
  16.         B[i] = 0;
  17.  
  18.     for (int i = 0; i < A[maxNumIndex]; i++)
  19.         B[A[i]]++;
  20.  
  21.     int modeIndex = 0;
  22.     for (int i = 1; i < SIZE; i++)
  23.         if (B[i] > B[modeIndex])
  24.             modeIndex = i;
  25.  
  26.     return modeIndex;
  27. }
  28.  
  29. int main()
  30. {
  31.     srand(time(NULL));
  32.  
  33.     int* A = new int[SIZE];
  34.     for (int i = 0; i < SIZE; i++)
  35.     {
  36.         A[i] = (1 + rand() % 20); // [0:20]
  37.         printf("%4d", A[i]);
  38.         if (i % 10 == 0) printf("\n");
  39.     }
  40.     printf("\n");
  41.  
  42.     printf("Mode is:\t%d\n", A[getMode(A)]);
  43.  
  44.     system("PAUSE");
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment