Randomly Generate Unique Sorted Values for 1D Array in C++



//Randomly Generate Sorted Array
#include<iostream>
#include<iomanip>
#include<time.h>
#include<stdlib.h>
using namespace std; 

int main()
{
int i, n, arr[50];
cout<<"===============================\n";
cout<<"Randomly Generate Sorted Array\n";
cout<<"===============================\n";
cout<<"Enter size of Array (Max. 50): ";
cin>>n;
srand((unsigned)time(NULL));
arr[0]=rand()%5+1;
for(i=1;i<n;i++)
{
arr[i]=rand()%5+arr[i-1]+1;
}
cout<<"Randomly Generated Sorted Array Values...\n";
for(i=0;i<n;i++)
{
cout<<setw(4)<<arr[i];
}
return 0;
}

Click to Watch Video.

No comments

Post your comments

Powered by Blogger.