Fill array with random numbers c++

Using the srand() and rand() C++ library functions, fill an array of 1000 floating-point numbers with random numbers that have been scaled to the range 1 to 100. Then determine and display the number of random numbers having values between 1 and 50 and the number having values greater than 50. Then modify it to also count and display the number of values between 1 and 25, 26 and 50, 51 and 75 and 76 to 100. Provide: 1. Snapshot of program run showing results for values 1 to 50 and 51 to 100.
   2. Snapshot of program run showing results for values 1 to 25, 26 to 50, 51 to 75 and 76 to 100.

Answer

if (arr[5]>50) coutくくarr [] ]<4t ; count-count+1 cout<<nNumber of random value greater than 50: <<count; cout<< n ////י CAUsersiR.AN tsVisual Studio 20081ProjectslexpDebuglexp.exe Random numbers greater than 5093 84 80 87 87 100 70 72 78 72Random numbers between 1 and 25:2 24 24 24 24 24 20 24 4 24 4 4 20 40 45 40 Number of random value between 1 and 25:256 48 48Random numbers between 51 and 75:67 70 73 72 74 72 64 74 73 74 73 74 72 70 72 70 72 72 73 60 73 70 70 73 74 73 74 72 64 70 74 Copyable Code: #include <iostream> #include<cstdlib> #include<ctime> using namespace std; //main int main () { //initialization of variables float arr[1000]; int count=0; srand(time(NULL)); //loop to assign values into array for(int j=0;j<1000;j++) { arr[j]=(float(rand()%100))+1; } //random numbers between 1 and 50 cout<<“n Random numbers between 1 and 50:”; for(int j=0;j<1000;j++) { if(arr[j]>=1 && arr[j]<=50) { cout<<arr[j]<<“t”; count=count+1; } } cout<<“nNumber of random value between 1 and 50:”<<count; cout<<“n———————————————————“; //random numbers greater than 50 cout<<“n Random numbers greater than 50:”; count=0; for(int j=0;j<1000;j++) { if(arr[j]>50) { cout<<arr[j]<<“t”; count=count+1; } } cout<<“nNumber of random value greater than 50:”<<count; cout<<“n———————————————————“; ////random numbers between 1 and 25 cout<<“n Random numbers between 1 and 25:”; count=0; for(int j=0;j<1000;j++) { if(arr[j]>=1 && arr[j]<=25) { cout<<arr[j]<<“t”; count=count+1; } } cout<<“nNumber of random value between 1 and 25:”<<count; cout<<“n———————————————————“; //random numbers between 26 and 50 cout<<“n Random numbers between 26 and 50:”; count=0; for(int j=0;j<1000;j++) { if(arr[j]>=26 && arr[j]<=50) { cout<<arr[j]<<“t”; count=count+1; } } cout<<“nNumber of random value between 26 and 50:”<<count; cout<<“n———————————————-“; //random numbers between 51 and 75 cout<<“n Random numbers between 51 and 75:”; count=0; for(int j=0;j<1000;j++) { if(arr[j]>=51 && arr[j]<=75) { cout<<arr[j]<<“t”; count=count+1; } } cout<<“nNumber of random value between 51 and 75:”<<count; cout<<“n———————————————————“; //random numbers between 76 and 100 cout<<“n Random numbers between 76 and 100:”; count=0; for(int j=0;j<1000;j++) { if(arr[j]>=76 && arr[j]<=100) { cout<<arr[j]<<“t”; count=count+1; } } cout<<“nNumber of random value between 76 and 100:”<<count; cout<<“n———————————————————“; return 0; }

Leave a Comment