Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- int main()
- {
- //create array (make the size the # of numbers you want to generate)
- int array[40];
- //temp variable for swapping
- int temp;
- srand(time(NULL));
- //creating array in order (starting from 1 ending at whatever number you choose (40 in this case)
- for(int c=0;c<40;c++)array[c]=c+1;
- //displaying each number (unnecessary, just for visualization)
- for(int c=0;c<40;c++)cout<<array[c]<<" ";
- //for loop to swap numbers
- for(int c=0;c<40;c++)
- {
- //generating random number from 1-n
- int x=rand()%40+1;
- //setting original number to temp
- temp=array[c];
- //replacing new number with old one
- array[c]=array[x];
- //setting temp to the new number
- array[x]=temp;
- }
- cout<<endl;
- //displaying your now randomly sorted numbers
- for(int c=0;c<40;c++)cout<<array[c]<<" ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement