Advertisement
Usow_Maxim

example find array max 5

Oct 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. bool coincidence(int amax[], int Size, int index){
  6.     for (int i = 0; i < Size; i++){
  7.         if (amax[i] == index){
  8.             return true;
  9.         }
  10.     }
  11.     return false;
  12. }
  13.  
  14. int main()
  15. {
  16.     int ASize = 10;
  17.     int MSize = 5;
  18.     srand(time(0));
  19.  
  20.     int* arr = new int[ASize];
  21.     for(int i = 0; i < ASize; i++){
  22.         arr[i] = rand() % 50 + 1;
  23.     }
  24.  
  25.     int amax[MSize];
  26.     for(int i = 0; i < MSize; i++){
  27.         amax[i] = 0;
  28.     }
  29.  
  30.     //Поиск самых больших элементов с исключением найденных.
  31.     for (int i = 0; i < MSize; i++){
  32.         int thelongerelement = 0;
  33.         int thelongindex = 0;
  34.         //Перебираем массив
  35.         for(int j = 0; j < ASize; j++){
  36.             if (coincidence(amax, ASize, j)){
  37.                 continue;
  38.             }
  39.             if(thelongerelement < arr[j]){
  40.                 thelongerelement = arr[j];
  41.                 thelongindex = j;
  42.             }
  43.         }
  44.         amax[i] = thelongindex;
  45.     }
  46.  
  47.     //Вывод текущего массива
  48.     printf("array:\n");
  49.     for(int i = 0; i < ASize; i++)
  50.         printf("arr[%d]: %d\n", i, arr[i]);
  51.  
  52.     //Вывод самых больших элементов.
  53.     printf("\narray max:\n");
  54.     for (int i = 0; i < MSize; i++){
  55.         printf("amax[%d]: %d\n", i, arr[amax[i]]);
  56.     }
  57.  
  58.     delete[] arr;
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement