Given the trace program below. I basically need to store a 1 or a 0 to the corresponding a[r[i]] the problem is I need to be able to keep track of the index of vectors zero and one to check the last index that was used for each vector.

For example:
// the values of vector zero and one after reading the file
zero = { 0, 0, 0, 0, 0};
one = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

Four random numbers
2, 4, 16, 12
ave = 14. 50

(2) a[r[i]] < 14.50
      a[r[i]] = zero[0];
(4) a[r[i]] < 14.50
      a[r[i]] = zero[1];
(16) a[r[i]] < 14.50
      a[r[i]] = one[0];
(12) a[r[i]] < 14.50
      a[r[i]] = zero[2];

Four random numbers
15, 36, 48, 45
ave = 35

(15) a[r[i]] < 35
      a[r[i]] = zero[3];
(36) a[r[i]] < 35
      a[r[i]] = one[1];
(48) a[r[i]] < 35
      a[r[i]] = one[2];
(45) a[r[i]] < 35
      a[r[i]] = one[3];

My initial attempt to solve the problem

int countZero = 0;
int countOne = 0;

for( int i = 0; i < 4; ++i )
{
  if( a[r[i]] < ave )
  {
    if( !one.empty() )
    {
      a[r[i]] = zero[countZero];
      countZero++;
    }
    else
    // if there are no more available values retain the same value
    a[r[i]] = a[r[i]];
  }
  else
  {
    if( !one.empty() )
    {
      a[r[i]] = one[countOne];
      countOne++;
    }
    else
    // if there are no more available values retain the same value
    a[r[i]] = a[r[i]];
  }
    cout << a[r[i]] << " ";
}
 cout << "\n";

The code above only works for the first four numbers. The succeding random numbers are not replace with 1s and 0s but the values are retained.

Sample Output:
// correct
2 4 16 12
0 0 1   0

// the values are retained
15 36 48 45
15 36 48 45

// should be 
15 36 48 45
0  1  1   1

Any tips on how to solve this?

Any tips on how to solve this?

Not without a better description. There is no explanation what r contains.
You didn't tell us but a seems to contain your 4 random numbers.
The indexes into zero and one -- are those just guesses? It there any reason those numbers were chosen?

You really need to explain fully before anyone can really attempt to offer advice.

There is no explanation what r contains

You didn't tell us but a seems to contain your 4 random numbers.

array r[4] contains four random numbers 0-21 and is use as index for a[]. I will only need to access the four random numbers
from a[] and replace the values with a 1 or 0.

The indexes into zero and one -- are those just guesses? It there any reason those numbers were chosen?

each four random is compared to the ave
if random number < ave
replace the value of random number with the first index of zero[0]
else
replace the value of random number with the first index of one[0]

Example:

(2) a[r[i]] < 14.50
      a[r[i]] = zero[0];
(4) a[r[i]] < 14.50
      a[r[i]] = zero[1];
(16) a[r[i]] < 14.50
      a[r[i]] = one[0];
(12) a[r[i]] < 14.50
      a[r[i]] = zero[2];

/* 
 so for the next value of 
  a[r[i]] if its < ave 
    a[r[i]] = zero[3] // last use index was 2
  else
    a[r[i]] = one[1] // last use index was 0
*/

If you are only setting the valuse of the array to one or zero based on if they are higher or lower than the average than you can just do that. You dont to use an array of all ones or zeros to do that.

a[r[i]] = zero[i0++];
a[r[i]] = one [i1++];

Also, at least consider what NathanOliver said.

Edited 4 Years Ago by WaltP

If you are only setting the valuse of the array to one or zero based on if they are higher or lower than the average than you can just do that.

It would be easier if that's the case but unfortunately it's not. Not all values will be replace with a 1 or 0 it depends on the number of 1s and 0s that I read from the file.

For example:
From the random numbers there are 5 values that are less than the average but the 0s that I read from the file are only two. Then that means the first two values from the 5 random numbers will be replace with 0 the rest of the values will be the same.

zero[i0++];

one [i1++];

Is i0 and i1 variable names? If they are that means I have to have to counters for zero[] and one[].

Is i0 and i1 variable names? If they are that means I have to have to counters for zero[] and one[].

Of course.

Here's what I tried so far I have two counters for one[] and zero[]. Unfortunately it's not working

int counterZero = 0;
int counterOne = 0;

for( int i = 0; i < 4; ++i )
{
 if( a[r[i]] < ave )
 {
   // check if counterZero goes beyond the size of the zero[]
   // if so retain the same number
   if( counterZero > zero.size() )
      a[r[i]] = a[r[i]];  
   else
   {
     a[r[i]] = zero[counterZero];
     counterZero++;  
   }
 }
 else
 {
   // check if counterOne goes beyond the size of the one[] 
   // if so retain the same number
   if( counterOne > one.size() )
     a[r[i]] = a[r[i]];  
   else
   {
     a[r[i]] = one[counterOne];
     counterOne++;  
    }
}
  cout << a[r[i]] << " ";
}
cout << "\n";

So I changed

if( counterOne > one.size() )
if( counterZero > zero.size() )

since the code above is giving me segmentation fault and the code below did solve the segmentation fault but the output is still wrong.

if( counterZero >= zero.size() )
if( counterOne >= one.size() )

Here's the output:

/* For example
zero[] = { 0, 0, 0, 0 }
one[] = { 1, 1, 1, 1 }

*/

// this part is correct 
// ave = 13.94
// four random numbers from array a[]
17 16 5 6 

// there values after being compared to the average
// one[0], one[1], zero[0], zero[1]
1 1 0 0 
// new values of array a[]
20 30 40 27 19 8 10 1 2 9 7 15 4 8 12 1 0 10 15 0 12 3 

// the succeeding is wrong 

// ave = 33.28
22 48 55 63 
22 48 55 63 
// should be zero[2], one[2], one[3], 63
// 0, 1, 1, 63
12 13 14 15 22 24 26 28 30 33 36 39 36 40 44 48 45 50 55 54 60 63 

// this part is correct since there are no more available values from one[] 
// and zero[]

// ave = 35.17
54 50 24 26 
54 50 24 26 
36 45 54 63 30 40 50 60 22 23 44 55 12 24 36 48 13 26 39 14 28 15 


// ave = 128.17
108 110 132 130 
108 110 132 130 
108 117 126 135 110 120 130 140 110 121 132 143 108 120 132 144 117 130 143 126 140 135 
This question has already been answered. Start a new discussion instead.