Holey_yan

20140520_KUAS_VisualC++_Exam11

May 19th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. /************************************/
  2. /** 20140520 Exam 11               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/05/20 am.11:40   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. void main()
  13. {
  14.     srand(time(NULL));
  15.     int random_array[100] = {EOF};
  16.     for(int i = 0; i < 100; i++)
  17.     {
  18.         random_array[i] = rand() % (15 - 1 + 1) + 1;
  19.     }
  20.  
  21.     int counter = 0;
  22.  
  23.     for(int i = 100 - 1; i > 0; i--)
  24.     {
  25.         for(int j = 0; j <= i - 1; j++)
  26.         {
  27.             if (random_array[j] > random_array[j + 1])
  28.             {
  29.                 int temp = 0;
  30.                 temp = random_array[j];
  31.                 random_array[j] = random_array[j + 1];
  32.                 random_array[j + 1] = temp;
  33.                 counter++;
  34.             }
  35.         }
  36.     }
  37.  
  38.     int sum = 0;
  39.     for(int i = 0; i < 100; i++)
  40.     {
  41.         sum += random_array[i];
  42.     }
  43.    
  44.     int number_counter[15][2] = {0};
  45.     for(int i = 0; i < 100; i++)
  46.     {
  47.         if(i < 15)
  48.         {
  49.             number_counter[i][0] = i + 1;
  50.         }
  51.         number_counter[random_array[i] - 1][1]++;
  52.     }
  53.     int max[2] = {0};
  54.     for(int i = 0; i < 15; i++)
  55.     {
  56.         if(number_counter[i][1] > max[1])
  57.         {
  58.             max[0] = number_counter[i][0];
  59.             max[1] = number_counter[i][1];
  60.         }
  61.     }
  62.  
  63.     printf("Random integer = {");
  64.     for(int i = 0; i < 100; i++)
  65.     {
  66.         if(i == 99)
  67.         {
  68.             printf("%d}\n", random_array[i]);
  69.             break;
  70.         }
  71.         printf("%d, ", random_array[i]);
  72.     }
  73.     printf("\n");
  74.  
  75.     printf("Number of cahng times: %d.\n"\
  76.            "Mean: %2.2f.\n"\
  77.            "Mode: %2.2f.\n"\
  78.            "Median: %2d.\n\n"\
  79.            ,counter, (float)sum / 100, (float)(random_array[50] + random_array[51]) / 2, max[0]);
  80.    
  81.     system("pause");
  82. }
Advertisement
Add Comment
Please, Sign In to add comment