Holey_yan

20140513_KUAS_VisualC++_Exam10

May 13th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. /************************************/
  2. /** 20140513 Exam 10               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/04/13 pm.11:40   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. void main()
  13. {
  14.     printf("Enter a number: ");
  15.     int input = 0, random_array[9999];
  16.     scanf("%d", &input);
  17.  
  18.     srand(time(NULL));
  19.     for(int i = 1; i <= input; i++)
  20.     {
  21.         random_array[i] = rand() % (10 - 1 + 1) + 1;
  22.     }
  23.  
  24.     printf("Random array = {");
  25.     for(int i = 1; i <= input; i++)
  26.     {
  27.         if(i != input)
  28.         {
  29.             printf("%d, ", random_array[i]);
  30.         }
  31.         else if(i == input)
  32.         {
  33.             printf("%d}\n", random_array[i]);
  34.         }
  35.     }
  36.  
  37.     printf("Answer = {");
  38.     switch(input % 2)
  39.     {
  40.         case 0:
  41.             for(int i = 1; i <= input / 2; i ++)
  42.             {
  43.                 if(i != input / 2)
  44.                 {
  45.                     printf("%d, ", random_array[i] + random_array[input + 1 - i]); 
  46.                 }
  47.                 else if(i == input / 2)
  48.                 {
  49.                     printf("%d}\n", random_array[i] + random_array[input + 1 - i]);    
  50.                 }
  51.             }
  52.             break;
  53.  
  54.         case 1:
  55.             for(int i = 1; i <= input / 2 + 1; i ++)
  56.             {
  57.                 if(i != input / 2 + 1)
  58.                 {
  59.                     printf("%d, ", random_array[i] + random_array[input + 1 - i]);
  60.                 }
  61.                 else if(i == input / 2 + 1)
  62.                 {
  63.                     printf("%d}\n", random_array[i]);
  64.                 }
  65.             }
  66.             break;
  67.     }
  68.     system("pause");
  69. }
Advertisement
Add Comment
Please, Sign In to add comment