Program Random Questions

This is a discussion on Program Random Questions within the C++ Programming forums, part of the General Programming Boards category; Hello,
I am writing a program which assigns hypothetically to each student (an actual program I have created) and make ...

Program Random Questions

Hello,

I am writing a program which assigns hypothetically to each student (an actual program I have created) and make sure each student receives an unique question assign from 50 questions to 10 students to have 5 questions each. However, the program assigns some students are being assign the same question and others receive the same question twice. Here is my program:

I would use a single 50 element array, not a two dimensional array. It will be harder to shuffle the elements in a two dimensional array.

Using random_shuffle is a good choice, but before you even worry about shuffling the array, I'd make sure you have working code that initializes it and prints it out unshuffled. You should be entering the values into the array first, and then shuffling them, so get that working first, then add the shuffle. The values in your array can be 1-50, but remember that the valid indexes are 0-49.

Initially I find your post vexing: you seem to show that you know STL material without realizing that arrays are zero-based (which would indicate that you haven't learned the basics).

"Accelerated C++" starts with vectors, so maybe...

If you select random numbers between 1-50, all the numbers selected could be 48 right? It's not probable, but its possible. However, the probability is close to 1 that when you randomly select 50 numbers between 1-50 there will be duplicate numbers.

This:

Code:

random_shuffle (a[i][j] = 1 + rand() % (50 - 1));

shuffles one number: the element a[i][j] in the array. If you have a series that consists of one number and you shuffle it, how does that change the order of the series? In addition, random_shuffle() does not accept just one argument. You have to give it two arguments that delimit a range of things to shuffle. In your case, give it the address of the first element in the array and the address of the last element in the array.

Try this:

1) Put the numbers 1-50 in a one dimensional array.
2) random_shuffle() the array.
3) Start at the beginning of the array and read the numbers in order into your 2d array of you want to group them that way.

You can actually shuffle a 2d array too. If you get the above working, then try reading the numbers 1-50 into a 2d array and shuffle that.