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

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

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

    ive been tryin to figure out how to program this for nearly 6 hours straight no joke. still i couldnt. Basically i just want to create a program that can separate odd and even numbers from the generated random numbers and then storing it to the array.

    my code so far..
    btw im using linux emacs for this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int arr[10];
       int ran_num, i, eve_num, odd_num;
    
      srand(time(0));
    
      for(i = 0; i <= 10; i++)
        {
           ran_num = (10.0*rand()/(RAND_MAX+1.0));
           arr[ran_num]++;
         }
    
        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);
    
    return 0;
    
    }

    I also tried swaping codes .. i gave everything i got but still it wouldnt work. I also tried inserting the IF Else statement inside For and its giving me wrong answer too.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One way to do this is to sort the array, either by using qsort from <stdlib.h>, or implementing the sort yourself. However, instead of comparing by the value of the elements, compare by the odd/even property of the elements, e.g., by using the modulo operator %.
    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

  3. #3
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Do you have to save the numbers into the array. Just expand your for loop the go around your if statements and every time you use arr[] replace it with ran_num.

  4. #4
    Registered User svanski's Avatar
    Join Date
    Oct 2011
    Posts
    8
    This would be my first C program. I am not very familiar with syntax so... this is code is very far from elegant.
    I am sure there are many things you could change and make it more readable/better looking.

    If you do not understand any part of my code feel free to ask.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
       int e=0;  /* counter for the even numbers  */
       int o=0; /*counter for the odd numbers    */
    
       int i, eve_num[10], odd_num[10]; /* evn_num/odd_num are size 10 arraies where we are going to write even/odd numbers. i is loop counter   */
       int ran_num=0; /* random number holder. You might want to switch from int to unsigned int. up to you  */
    
      srand(time(NULL));
    
      for(i = 0; i <= 10; i++){   /* for loop where we randomly choose 10 numbers  */
    
           ran_num = rand();
    
    
         if (ran_num %2 == 0){     /* nested loop in case we get even number  */
               eve_num[e] = ran_num;   /* place the even number into the array. e is array location counter that we update on next line  */
               e++;
         }
    
         else{                    /* same idea as above. However, for odd case   */
               odd_num[o] = ran_num;
              o++;
         }
      }
    
      int ev =0;
      for(ev; ev<e ;ev++){      /* loop where we are going to print the array that contains even numbers  */
              printf("Even No.s : %d\n", eve_num[ev]);
      }
    
      int od =0;  /* loop for printing odd numbers */
      for(od; od<o; od++){
         printf("Odd No.s  : %d\n", odd_num[od]);
      }
    
    
     return 0;
    
    }

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Set ran_num = rand();
    Then do something like this:

    Code:
    if (ran_num % 2 == 0)
    {
    printf("%i\n", ran_num); eve_num++;
    } else {
    printf("%i\n", ran_num); odd_num++;
    }
    Then print out eve_num and odd_num.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    If you want to have a random num 1-10 do something like this:

    Code:
    ran_num = rand() % 10 + 1;
    Then change the 10 to the max num you want to use.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dachichk View Post
    This would be my first C program. I am not very familiar with syntax so... this is code is very far from elegant.
    I am sure there are many things you could change and make it more readable/better looking.

    If you do not understand any part of my code feel free to ask.
    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.

  8. #8
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    The OP pretty much has everything that i posted. I don't want to give everything for that very reason. Also, what is wrong with what I posted...it worked with no errors for me.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rmatze View Post
    The OP pretty much has everything that i posted. I don't want to give everything for that very reason. Also, what is wrong with what I posted...it worked with no errors for me.
    Wasn't talking to you... was talking to dachichk.... unless you are using two accounts, which is prohibited here.

  10. #10
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Quote Originally Posted by CommonTater View Post
    unless you are using two accounts
    That could get weird if I had split personalities and one personality was helping the other code.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rmatze View Post
    That could get weird if I had split personalities and one personality was helping the other code.
    Trust me, I've seen FAR worse than that in my days...

    At one point (on a different forum) we caught out one user with more than 10 accounts all of which were being used to attack one happless soul.

  12. #12
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Quote Originally Posted by CommonTater View Post
    Trust me, I've seen FAR worse than that in my days...

    At one point (on a different forum) we caught out one user with more than 10 accounts all of which were being used to attack one happless soul.
    Wow, must have really had it out for that person.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rmatze View Post
    Wow, must have really had it out for that person.
    Yes, there are some really sick __cks out there.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Anyway, back to the topic at hand...

    > for(i = 0; i <= 10; i++)
    Both sets of code suffer from array overrun.

    For array[N], the correct loop is for ( i = 0 ; i < N ; i++ )

    Not <=
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    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]++;
         }
    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

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