Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<stdlib.h>
- #include <time.h>
- int main()
- {
- double arr[20];
- double tab[20] = {0};
- //Create an array with 20 randomly sampled values from an interval
- //10.0 - 100.0 with a precision up to 1 decimal point, and then sort the array using insertion sort.
- srand(static_cast<unsigned int>(time(nullptr)));
- std::cout<<"------------------------"<<std::endl<<"Numbers generated: "<<std::endl<<"------------------------"<<std::endl;
- for (int i = 0; i<20; i++)
- {
- arr[i] = (rand() % 1000 + 101) / 10.0;
- std::cout<< arr[i]<< ", ";
- }
- std::cout<<std::endl<<"------------------------"<<std::endl<<"Array in order:"<<std::endl<<"------------------------"<<std::endl;
- double current;
- current = arr[0];
- tab[0] = arr[0];
- for (int i = 1; i<19; i++)
- {
- if (arr[i] > tab[i-1])
- {
- tab[i] = arr[i];
- break;
- }
- else
- {
- for (int k = i; k>0; k--)
- {
- if (arr[k] < tab[k-1])
- {
- tab[k] = tab[k-1];
- tab[k-1] = arr[k];
- }
- else
- {
- tab[k-1] = arr[k];
- }
- }
- }
- }
- for (int i = 0; i<19; i++)
- {
- std::cout<<tab[i]<<", ";
- }
- std::cout<<std::endl;
- std::cout<<"------------------------"<<std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement