In: Computer Science
#include<iostream>
using namespace std;
const int NUM = 20;
int main() {
// write code to Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100].
// Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program.
// Write code to print out the shuffled array.
return 0;
}
Please Use C++ to solve
#include<iostream>
using namespace std;
const int NUM = 20;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int arr[NUM], i, j;
//assigning the array with 20 randomly generated integers in the range [1, 100].
for(i = 0; i < NUM; i++)
arr[i] = rand() % 100 + 1;
cout<<"Array before shuffling\n";
for(i = 0; i < NUM; i++)
cout<<arr[i]<<" ";
cout<<"\n";
j = -1;
i = 0;
//shuffling the array
while(i < NUM)
{
if(arr[i] < 50)
{
j++;
swap(&arr[i], &arr[j]);
}
i++;
}
//printing the shuffled array
cout<<"Array after shuffling\n";
for(i = 0; i < NUM; i++)
cout<<arr[i]<<" ";
cout<<"\n";
return 0;
}
IF YOU LIKED THE ANSWER, PLEASE UPVOTE!!!