Checking for repeats using loops.

Good day everyone, I have two questions as of now. I am working on a homework problem where I place 20 (1-20) jersey's into 20 lockers randomly.

I am using UNIX. Also, I am using emacs as a text editor and g++ to compile.

My first question relates to the output of the program, it outputs the lockers 1-20 twice...

My second question is what flaws are there in my loops to check if the numbers were repeated?

Any advice and/or pointers would be appreciated, thanks.

*************************************************
***************** Updated Code *******************
*************************************************

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
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main() // START MAIN()
{
double times;
int jersey [20];
 int temp [20];

cout << "Please enter how many times you would like to run this program > ";
cin >> times;
 srand((unsigned)time(0)); // RANDOM SEED BASED OFF TIME


for(int x=0; x<times; x++) // RUN PROGRAM X TIMES
   { // start number of times run loop

   for(int index1=0; index1<20; index1++)
      { // start the seed loop

      jersey [index1] = rand()%20+1;
      temp[index1] = jersey[index1];

        if(index1 < 0)
        {
        int forTemp = (index1-1);

        for(int index2 = forTemp; index2>0; index2--)
          {
            while(int condition = 0)
              if(jersey[index1] == jersey[index2])
              {
               jersey [index1] = rand()%20+1;
              }
            else
              {
                condition = 1;
              }

          }
                }
       cout << "Locker " << index1 << " has jersey number " << jersey [index1] << endl;

      } // end the seed loop

   }  // end number of times run loop

} // END MAIN()
Last edited on
Use code blocks when posting code please. Put ["code"] before the code and ["/code"] after (without the quotes).

Edit:
 
if(jersey[index1] = jersey[index2])


This is assigning the value in jersey[index2] to jersey[index1]. You need to use ==, not =, in order to compare two values.
Last edited on
Thank you for both pieces of information freddy92!

Alright, so now when I run it there is only one display of the array instead of two. That is much more friendly to look at.
Last edited on
1
2
3
4
for(int q = 0; q>=20; q++)
  {
   cout << "Locker " << q << " has jersey number " << jersey [q] << endl;
  }


Two things:
1) This loop isn't needed, as the numbers have already been outputed in a previous loop.
2) This loop doesn't work, as the condition is q >= 20. q starts at 0, so this will never be true.
When I looked at my code to remove that for loop, I had already removed it during my troubleshooting and checking of my loops and if they were configured properly.

As far as I can tell, everything is working, except that I have only one case where the program checks for repeats. Here is my current code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
      if(index1 == 1)
        {
        int forTemp = (index1-1);

        for(int index2 = forTemp; index2>0; index2--)
          {
            while(int condition = 0)
              if(jersey[index1] == jersey[index2])
              {
               jersey [index1] = rand()%20+1;
              }
            else
              {
                condition = 1;
              }

           }
        }


Can you give me some ideas/pointers on creating a loop to do this? I would like to avoid running this group for 1-19.
Last edited on
Off the top of my head, the easiest way to ensure no repeats would be to create an array of 20 ints and assign them numbers 1-20. Then, create a for loop that randomly generates a number between 0 and the number of jersies/lockers you have yet to assign a number minus 1. Take the result and go to that index in the array, and assign that value to the locker.

Then here's where it gets fun. You don't want to use the number you just assigned again, and you probably did not use the one at the end of the array. So, just take the last number in the array, and set the index you just assigned to that instead. Then decrement your number of lockers remaining and repeat until all have been assigned.

Hopefully that made sense, though it probably didn't. If you want help understanding it just ask, I'll be back in about a half hour. It's not all that complicated or a lot of code.
So you are saying make an array from 1 - 20 (0-19 your choice) and as the program chooses a random variable you find the number and remove it from the first array?

ex) (in rough terms)


create arrayA(0-19)
create arrayB(0-19)

enter a randomised between (0-19) and enter into the first entry in arrayA

find randomised number in arrayB and remove it from arrayB making it one entry shorter (0-18)

***
so after looking online, i found that this was not possible in c++, would creating 20 variables and working the other way around work better?

random number1 == a1
if random number2 == a1 re randomise?
***
rinse, lather, and repeat?

Hopefully this is what you ment, ill try typing up some code to do this and ill check back. Thank you by the way for the help, it is greatly appreciated.
Last edited on
As a glutton for punishment, I keep looking at my code, lines 27-44, looking for some error in the logic... should I just scrap it and try another approach?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int arr[20]

for (int i = 0; i < 20; i++) //Populate array with 0-19
nums[i] = i;

int size = 20; // Size of the array.  Cannot be const as we will be decrementing it.

for (int i = 0; i < 20; i++) // Loop to ranomly put jersies in lockers
{
int index = (INSERT RANDOM NUMBER GENERATING THING HERE) % size;
(ARRAY OF LOCKERS)[i] = nums[index]; //Take num from randomly picked index, put it into locker
nums[index] = nums[size-1]; // Replace the number you just used with the last one in the array
size--;  // Decrement the # of numbers left that have not been picked.
}


There's some code that should be mostly accurate; it's been a while since I did C++. You could make it better but the general idea should get you on the right path.
Topic archived. No new replies allowed.