Advertisement
Guest User

paste1

a guest
Nov 21st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include<stdlib.h>
  3. #include <time.h>
  4. int main()
  5. {
  6.  
  7.     double arr[20];
  8.     double tab[20] = {0};
  9. //Create an array with 20 randomly sampled values from an interval
  10. //10.0 - 100.0 with a precision up to 1 decimal point, and then sort the array using insertion sort.
  11.  
  12.     srand(static_cast<unsigned int>(time(nullptr)));
  13.     std::cout<<"------------------------"<<std::endl<<"Numbers generated: "<<std::endl<<"------------------------"<<std::endl;
  14.     for (int i = 0; i<20; i++)
  15.     {
  16.         arr[i] = (rand() % 1000 + 101) / 10.0;
  17.          std::cout<< arr[i]<< ", ";
  18.     }
  19.     std::cout<<std::endl<<"------------------------"<<std::endl<<"Array in order:"<<std::endl<<"------------------------"<<std::endl;
  20.     double current;
  21.     current = arr[0];
  22.     tab[0] = arr[0];
  23.     for (int i = 1; i<19; i++)
  24.     {
  25.         if (arr[i] > tab[i-1])
  26.         {
  27.             tab[i] = arr[i];
  28.             break;
  29.         }
  30.         else
  31.         {
  32.             for (int k = i; k>0; k--)
  33.             {
  34.                 if (arr[k] < tab[k-1])
  35.                 {
  36.                     tab[k] = tab[k-1];
  37.                     tab[k-1] = arr[k];
  38.  
  39.                 }
  40.                 else
  41.                 {
  42.                     tab[k-1] = arr[k];
  43.  
  44.                 }
  45.  
  46.  
  47.             }
  48.         }
  49.  
  50.  
  51.     }
  52.     for (int i = 0; i<19; i++)
  53.     {
  54.     std::cout<<tab[i]<<", ";
  55.     }
  56.     std::cout<<std::endl;
  57.     std::cout<<"------------------------"<<std::endl;
  58.  
  59.  
  60.  
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement