Click here to Skip to main content
16,015,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I have two menus [menu1, menu2] I want to choose different names for these two menus according to the random number was generated(either 1 or 2), so, I used menu1[r1].c1[r1] and menu2[r2].c2[r2] to check if it is equal to zero in this case I choose name form menu1[r1].n1[r1] and menu2[r2].n2[r2].
So if all menu1[r1].c1[r1] =1 ,the program must generate different random number, the output must be mee1,mee2,me16,mee4,mee5,me13,mee7,mee8,mee9,me10,me11,me12,mee6,me14,me15,mee3

or in different arrange according to the random number that generates

What I have tried:

---
Posted
Updated 26-Feb-18 5:25am
v6
Comments
phil.o 25-Feb-18 14:22pm    
I'm actually trying, but I still have a very hard time understanding your requirement. What disturbs me the most is your usage of arrays as I feel that your are over-engineering your solution.
Please give me some time, I'll try to post a second solution trying to simplify things a bit.
Lilyanaa 25-Feb-18 14:38pm    
thanks for your efforts, actually I don't know why the if conditions do not work, I just want the output consists of 16 names, eight from each menu and without duplicate in names
phil.o 25-Feb-18 14:45pm    
OK, I see; actually you do not need the member variables in your structures being arrays (excepts for the names which are arrays of characters, bt I'll show you how to declare them). And you are using the member variable c to know whether a specified name already has been attributes, right?
Problem is: out of 16 random numbers 1 or 2, nothing guarantees that you will get 8 ones and 8 twos; you could have 10 ones and 6 twos, or 5 ones and 11 twos.
phil.o 25-Feb-18 14:43pm    
So let me rephrase to be sure I understood:
you want in your final result an array of 16 f structures, each having a random distribution of "mee1", "mee2", ..., "me16" name, each name being unique and coming from menu1 or menu2 array depending on the random number?
Lilyanaa 25-Feb-18 15:04pm    
Yes, so I use if(count1<=8) to guarantee that doesn't choose more than 8, if the new random number be 1 too, it must generate new random number and in the same time the .c1 be all = 1 because all the 8 names were chosen

To generate a random number either 1 or 2, you can use the following code:
C
#include <stdlib.h>

int random_number = (rand() % 2) + 1;


Hope this helps.
 
Share this answer
 
Comments
Lilyanaa 25-Feb-18 12:05pm    
is the same, r = rand() % (2 + 1 - 1) + 1 ;
phil.o 25-Feb-18 12:27pm    
Yes, it does. But, in this case, this produces exactly a random number which is either 1 or 2.
So, if that is not what you want, please try to rephrase your requirement.
Lilyanaa 25-Feb-18 12:39pm    
I was explained what I want in my question, can u help, please?
phil.o 25-Feb-18 12:44pm    
No, because I tried my best, but I cannot understand your requirement.
That's why I am asking you to rephrase it, because for now, I will be totally unable to help you.
Once I understand what it is you need, things should go much faster.
Lilyanaa 25-Feb-18 12:49pm    
Ok, I will try, thanks
I think I almost understood what you want to do, but I think you cannot do it the way you have coded (see my comment).
The problem is: if you take a random number (1 or 2) 16 times, nothing guarantees that you will have the same number of ones and twos. Even by setting a flag in the c member variable as you did, that completely messes up with the algorithm. I think having an array of 16 names that you would shuffle would be much cleaner.
You could do this way:
C
#include <time.h>

struct f
{
   const char* name;
};

// Shuffles an array
void shuffle(f menu[], size_t count);

// The size of the array
const int SIZE = 16;

int main() {
   // Declarations
   int i;
   struct f menu[SIZE];

   // Initializations
   menu[ 0].name = "mee1";
   menu[ 1].name = "mee2";
   menu[ 2].name = "mee3";
   menu[ 3].name = "mee4";
   menu[ 4].name = "mee5";
   menu[ 5].name = "mee6";
   menu[ 6].name = "mee7";
   menu[ 7].name = "mee8";
   menu[ 8].name = "mee9";
   menu[ 9].name = "me10";
   menu[10].name = "me11";
   menu[11].name = "me12";
   menu[12].name = "me13";
   menu[13].name = "me14";
   menu[14].name = "me15";
   menu[15].name = "me16";

   // Shuffle the numbers
   shuffle(menu, SIZE);
}

// Algorithm from https://benpfaff.org/writings/clc/shuffle.html
void shuffle(f menu[], size_t count) {
   size_t i, j;
   f t;
   srand((unsigned int)time(NULL));
   if (count > 1) {
      for (i = 0; i < count - 1; ++i) {
         j = i + rand() / (RAND_MAX / (count - i) + 1);
         t = menu[j];
         menu[j] = menu[i];
         menu[i] = t;
      }
   }
}

I changed your original algorithm a lot, but that is because I do not think you could achieve this the way you had done it. I kept the f structure, but got rid of str1 and str2 as they only introduced some unnecessary complexity.
I am aware this is a big change, and not knowing the exact requirement this may not be a valid solution. If it is not valid, my apologies. I don't think I would be able to only slightly modify your solution and achieve the same result.

As I said, I do not have a C compiler. I tested it on a VS 2017 C++ project and I actually get expected shuffling of names.
 
Share this answer
 
Comments
Lilyanaa 25-Feb-18 17:20pm    
No, I must use two menus, logically my code must be correct if .c1 more than 8, must generate new random number, but I don't know why it doesn't work, anyway thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900