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
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 3×9 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?
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
Assuming creating your own random number generator isn’t supposed to be part of the assignment, then you have three parts to the assignment.
- Create structure with the proper size to hold the number of students input.
- Use a random number generator to assign positions.
- 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