Arrange Binary in Order

I have this program which generates four unique random numbers from array a[]. Each random number is compared to the average.
If the value is < average
replace the value with 0 otherwise 1.

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;


Sample Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// index use for array a[]..a[11],a[17]...
Index 11
Index 17
Index 20
Index 7

4 Random Numbers
//the values from a[]
10 12 15 17 
Binary values
0 0 1 1 
Sum 295 Sum Random 54 Average 13.39

20 30 40 27 19 8 10 1 2 9 7 0 4 8 12 6 5 0 15 6 1 3 


Expected Output
1
2
3
4
20 30 40 27 19 8 10 1 2 9 7 0 4 8 12 6 5 0 15 6 1 3 
//the array above should be the one below

20 30 40 27 19 8 10 1 2 9 7 1 4 8 12 6 5 0 15 6 0 3 


The problem is some of the 1s and 0s are out of order.

Any ideas on how to fix this?
Why are you using 18 on line 41? Shouldn't you use sizeof( a ) instead of a hard-coded value?
there are 22 values in the array minus the 4 random numbers which is 18
Nvm what I said first. I ran the code myself and the output was actually correct:

1
2
3
4
5
6
7
8
9
10
11
12
Index 11
Index 17
Index 20
Index 7

4 Random Numbers
15 10 12 17
Binary Values
1 0 0 1
Sum 295 Sum Random 54 Average 13.39

20 30 40 27 19 8 10 1 2 9 7 1 4 8 12 6 5 0 15 6 0 3


Somehow the order of your random numbers gets changed and therefor the wrong stuff gets swapped out. I did have to hardcore my values to simulate your exact situation though, so an error might occur while generationg the list.
Last edited on
i already got it
Could you share what happened? Since the code actually worked for me I'm curious what could've caused it.
Topic archived. No new replies allowed.