I was requested to fill in a structure with all available combinations of 4 variables (colour,shape,nr,p).
All these variables can take 3 values so I have a total of 81 combinations. I will use these combinations later on to create a card game.
The combinations for each:
- colour : r,g,b
- shape : d,e,f
- nr : 1,2,3
- p : j,k,l
I was advised that 4 loops are sufficient to do this, but can't find out how they should be formed. What I am thinking of is fill in the first 27 positions with a value for colour, then the other 27 positions with colour, and then the rest with another colour.. Then starting to fill in the shape, and after that the nr etc.. But that way I have too many loops and not only 4 that should be sufficient according to the instructions.
This is part of a bigger exercise but if I don't fill in the structure I can't move on to the significant stuff!
What am I missing?
#include <stdio.h>
#include <stdlib.h>
int i;
typedef struct {
unsigned char colour;
unsigned char shape;
unsigned char nr;
unsigned char p;
} CARDS;
int main()
{
CARDS cards[81];
for (i=0;i<27;i++)
cards[i].colour='r';
for(i=27;i<54;i++)
cards[i].colour='g';
for (i=54;i<81;i++)
cards[i].colour='b';
for (i=0;i<6)
cards[i].shape = 'd';
for (i=0;i<12)
cards[i].shape = 'e';
// a lot more loops..
}
81permutations you should use four nested loops. - Weather Vane4permutations of each4features my answer side-stepped the nested loops, but could have used them. They are shown in an answer to this question, below. - Weather Vane