1

I have just started a C programming course and so far have only done the basics like printf, read a little on variables etc on the course book. The teacher has tasked us with writing a program that will take a value entered by the user, this will be the number of students in the class (25 at the moment but can be variable). It will then list the number of students randomly and place them in 3 columns. The purpose is to sort students into groups of 3 randomly, then display on the screen in columns.

Now my question is not for the code that defeats the object of me attempting the exercise, but how to structure it. I can see that using an array can be used to display the list, but really after some pointers on how best to approach the problem in blocks, then I can attempt to program each block.

8
  • IMHO, in an educational setting, the structuring of the problem is more important than the implementation itself. Have you tried asking the instructor for help?
    – user40980
    Commented Nov 19, 2012 at 15:24
  • 1
    +1 for Now my question is not for the code that defeats the object of me attempting the exercise. What you're really asking about is what sort of structures should or could be used.
    – user53019
    Commented Nov 19, 2012 at 15:28
  • Yes and he has given a few pointers, I have listed down what I want to do but do. Taking a number the user has entered, then generate a list from that number, then randomize that list? Lost of questions. However I don't have a lesson for 5 days and that is when the exercise needs handing in. The teacher knows it's a tough exercise for us at this stage, but the idea is to get us thinking so not worried about having a fully working code. At the moment I am drawing a blank so after a little guidance.
    – Ant
    Commented Nov 19, 2012 at 15:32
  • Office hours? TAs? student tutoring services? I personally am a fan of the socratic method for learning - which involves the instructor asking questions of the student to make them aware of they already know. The Q&A format is poor for this style of learning. I also know that the solutions/suggestions posed by someone who has been in the industry for decades may be far beyond what you were taught and thus clear that you sought outside help and the answer is not entirely your own.
    – user40980
    Commented Nov 19, 2012 at 15:40
  • @Ant - if you need more specific guidance beyond my answer below, then please consider updating your question with what you've tried so far. MichaelIT's advice / concern is valid as well. You must be able to demonstrate to your professor that it's still your code.
    – user53019
    Commented Nov 19, 2012 at 15:40

3 Answers 3

1

The biggest challenge you face is the fact that the number of entries to randomize is variable. C requires more effort with managing variable structures, and that's probably the real intent of the exercise.

You should also consider the input structure as well as an output structure.

One approach to solving this is to take in all the inputs, placing them in an array or similar structure while they are read in. You can then randomly select entries from that input array and either push them to the output structure or push them to the output device. This gives a randomization of the input values along with a potential randomization of their output location.

Another approach would be to push the input values to their randomly selected output structure as they are provided. This only provides a randomization of their output location, and a modest appearance of randomization to their input.

A 25+ entry array would be just as good of a place to start with on the input structure. It's also pretty easy to tap based upon the index.

A simply 3x9 array would be a good starting point to consider for storing the results. You could also use 3 separate arrays for storing the results. Honestly, I'm not sure there's an advantage either way. If you had additional requirements on the output, that might affect the structures.

A final aspect to consider is input array overflow (ie too many values being provided) as well as handling too many entries into a given "column" of the output structure. It's possible that one column will fill up before the others. What limits do you want to place on how many entries each column can have?

0

Perhaps the most instructive thing to do for yourself is to try to implement it in various ways, and you'll see by yourself what are the advantages and disadvantages of each.

...I mean, this is obviously a "learning by doing" exercise.

1
  • Arnaud good plan I figured a few flow diagrams will help me visual what is happening.
    – Ant
    Commented Nov 19, 2012 at 19:52
0

Assuming creating your own random number generator isn't supposed to be part of the assignment, then you have three parts to the assignment.

  1. Create structure with the proper size to hold the number of students input.
  2. Use a random number generator to assign positions.
  3. Output the randomized groups in the correct format.

The two main hangups I can see is creating a properly sized structure based on dynamic input is a bit more difficult in C than more modern languages, and handling collisions when randomly assigning spots.

How to hold the data is really a question of how much you need to do with it, if only printing is required a single array will work fine, if more is required to be done based on groups it may be beneficial to use a 2 dimensional array so individual groups are easier to access.

5
  • Why randomly assign spots? When dealing cards from a deck to a hand, there are no collisions. The key is to make certain that the deck is shuffled first.
    – user40980
    Commented Nov 19, 2012 at 16:07
  • @MichaelT but then you have to build a deck and shuffle it
    – Ryathal
    Commented Nov 19, 2012 at 16:11
  • The question is "is shuffling a fixed sized array easier than collision handling?" I believe the answer is yes and there are a number of resources available for the question "how to shuffle a deck in c" for someone unfamiliar with it.
    – user40980
    Commented Nov 19, 2012 at 16:13
  • @MichaelT but the array isn't fixed size
    – Ryathal
    Commented Nov 19, 2012 at 16:15
  • The deck array? or the hand array (which has an upper bound of ceil(decksize/handsize))? Why would this matter?
    – user40980
    Commented Nov 19, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.