Thread: Need help: sorting random numbers rand() to odd and even in C

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's the logic, or rather, the algorithm, that you have in mind?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kasuka View Post
    Hi guys, thanks for replies. Basically what ive been tryin to achieve is to generate 10,000 numbers
    thats why im using
    Code:
      for(i = 0; i <= 10; i++)
        {
           ran_num = (10.0*rand()/(RAND_MAX+1.0));
           arr[ran_num]++;
         }
    If you want to generate 10,000 random ints in an array you don't do it that way, you're going to run into all kinds of array bounds errors and parts of the array will be left unused...

    Try it like this...
    Code:
    int array = malloc(10000 * sizeof(int));  // it's too big for the stack.
    int i;
    
    for (i = 0; i < 10000; i++)
      array[i] = rand();
    ... see how easy that is?


    and store it to the array and then divide them to even and odd
    Code:
        if (arr[ran_num] %2 == 0)
           {
              printf("%d\n", arr[ran_num]);
              eve_num++;
           }
        else
           {
              printf("%d\n", arr[ran_num]);
              odd_num++;
           }
     
         printf("Even No.s : %d\n", eve_num);
         printf("Odd No.s  : %d\n", odd_num);
    i know the logic but im consfused to write it in codes
    This part of your code is basically ok... except for the way you pick stuff out of the array...
    Code:
    int odd_nums = 0;
    int even_nums = 0;
    
    for (i = 0; i < 10000; i++)
      if (array[i] & 1)   // true for odd, false for even
        odd_nums++;   
      else
        even_nums++;
    
      printf("/nThere were %d odd numbers and %d even numbrers", odd_nums, even_nums);
    Again... very easy to do.

    The trick is to think like the computer... little tiny steps, in strict order...

  3. #18
    Registered User svanski's Avatar
    Join Date
    Oct 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Please don't just haul off and post complete solutions like that. You may put a smile on the OP's face but you totally deny them any chance of learning anything about programming. He's learned nothing of process or procedure... all he really knows from you is how to copy and past (scoop and poop) code for his own homework...

    Plus there are errors in your code.
    Point is taken. I will keep your advice in mind, for my future posts.

  4. #19
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Thank you so much guys ... im finally getting the idea here ..i will try to code it once more see whats gonna happen.

  5. #20
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    and oops i forgot to add ... i was really confused about this stuff because its not just random numbers i mean ... i have to set the random numbers from No.s 0-9. Thats why i have arr[10]. So generate 10k numbers (which can be 0, 1,2,..9) and then divide these numbers to Odd and Even. On my previous code .. it can generate 10k random numbers from 0 - 9. Ive been trying to separate the Odd and even numbers and get the sum (of how many odd numbers. of how many even numbers)

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If there's a limit on your rand() numbers, you do that like this...
    Code:
    #define LIMIT 10
    
    int array = malloc(10000 * sizeof(int));  // it's too big for the stack.
    int i;
    
    for (i = 0; i < 10000; i++)
      array[i] = rand() % LIMIT;
    Look up the modulous operator ( % ) in your C documentation.

    The thing is, as I mentioned before, you need to stop guessing...
    Analyse the problem, Plan a solution, Write the code, Test your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting and Binning Random Numbers Problem....
    By meadmead in forum C Programming
    Replies: 4
    Last Post: 10-11-2011, 06:01 PM
  2. Sorting random numbers using pointing arrays.
    By jFran in forum C Programming
    Replies: 8
    Last Post: 05-10-2011, 01:56 PM
  3. Replies: 28
    Last Post: 04-23-2010, 06:52 PM
  4. Rand() won't be random
    By EvilPickles in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 01:47 AM
  5. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 04:47 AM