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 ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    30

    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:
    Code:
    #include<iostream>
    
    using std::cout;
    using std::ios;
    
    #include<iomanip>
    
    using std::setw;
    using std::setprecision;
    using std::setiosflags;
    
    #include<algorithm>
    #include<cstdlib>
    #include<ctime>
    
    int main()
    {
        
        srand( time( 0 ) ); 
        int a[10][5], i , j;
            cout << setw(8);
        
        for( i = 1; i <=10; i++)
        {
           for( j = 1; j <=5; j++)
           {
            random_shuffle (a[i][j] = 1 + rand() % (50 - 1));
               cout << a[i][j] << setw(8);
           }
        cout << '\n';
        }
    
    return 0;
    }
    I want to use either
    Code:
    random_shuffle or swap
    so I can do my program but I am not sure if I putted in the right place.
    Please help me fix my problem.
    Thank you for some one who can guide me to solve the mistake I made.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    There is some example code in your last thread that might help.

    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.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 11-19-2005 at 10:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 11:40 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Random numbers works in one program, not in another
    By Shadow12345 in forum C++ Programming
    Replies: 27
    Last Post: 09-30-2002, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21