1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
srand(time(NULL));
// 1D array
int a[] = { 20, 30, 40, 27, 19, 8, 10, 17, 2, 9, 7,
15, 4, 8, 12, 6, 5, 10, 15, 6, 12, 3 };
int random[4];
//sum of four random numbers
double sum2 = 0;
//sum of a[]
double sum = 295;
unordered_set< int > unique;
unordered_set< int >::iterator it;
while( unique.size() < 4 )
{
//generate four random numers from array a[]
random[unique.size()] = rand() % 22;
unique.insert( a[random[unique.size()]] );
}
//display index for array a[]
for(int i = 0; i < 4; ++i)
{
cout << "Index " << random[i] << "\n";
}
cout << "\n";
cout << "4 Random Numbers " << "\n";
//display four random numbers from array a[]
for( it = unique.begin(); it != unique.end(); ++it )
{
cout << *it << " ";
sum2 += *it;
}
cout << "\n";
double average;
average = ( sum - sum2 ) / 18;
cout << "Binary Values " << "\n";
int i = 0;
for( it = unique.begin(); it != unique.end(); ++it, ++i )
{
// if value from a[] is less than average replace the value with
// 0 otherwise 1
if( *it < average )
a[random[i]] = 0;
else
a[random[i]] = 1;
//display 1s and 0s
cout << a[random[i]] << " ";
}
cout << "\n";
cout.setf(ios::fixed);
cout << "Sum " << (int)sum << " Sum Random " << (int)sum2
<< setprecision(2) << " Average " << average << "\n" << "\n";
//display values of a[] with 1s and 0s
for(int i = 0; i < 22; ++i)
{
cout << a[i] << " ";
}
cout << "\n";
return 0;
|