Thread: sorting random numbers with bubblesort

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    A high level description in English or whichever is a programmer's first language. i.e.

    1. Prompt user for the lowest & highest value.
    2. Prompt the user for the number of random values.
    3. Select and store random values until number of values equals number from #2.
    ...

  2. #2
    Banned
    Join Date
    Nov 2011
    Posts
    8
    İ am probably having a problem returning these functions in the main function.(like you said)what can I write in the main function so that I can use these functions and also because of bubblesort being a void is it wrong to delete it while writing it in the main function.I believe this is has to be done first.so what i mean what can i write to the main function so that i use use the functions outside the main function.because as u said it returns nothing.so if we can go step by step.it will be more helpful
    After that we can go on with the errors in the functions.

  3. #3
    Banned
    Join Date
    Nov 2011
    Posts
    8
    Also I wanted to add that n is not unknown it is given in the main function and when the program runs it asks it from the user.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sam19911 View Post
    Also I wanted to add that n is not unknown it is given in the main function and when the program runs it asks it from the user.
    But then you declare a new copy of N inside your random number function. This internal variable masks (hides) the one in main and is used in an uninitialized state in your function. You should be passing N into the function, not counting upon it's existence elsewhere...

    A good function performs 1 task... not half of your program's chores.


    Code:
    #include <stdlib.h>
    #include <stdko.h>
    #include <time.h>
    
    void MakeLuckyNumbers(int *array, int min, int max, int num)
      {  int i;
    
          for (i=0; i < num; i++)  
            array[i]= min + (rand() % (max - min));
      }
    
    int main()
      {
         int min = -1;
         int max = -1;
         int num = -1; 
         int numbers[100];
         
         printf("Lucky number generator... \n\n");
         srand(time(NULL));
    
         do 
           {
              printf("How many numbers? : ");
           }   
         while (scanf("%d", &num) < 1 || num < 0 || num > 100);
    
    
        do 
           {
              printf("The highest number? : ");
           }   
         while (scanf("%d", &max) < 1 || max < 0);
    
        do 
           {
              printf("The lowest number? : ");
           }   
         while (scanf("%d", &min) < 1 || min < 0 || min > max);
    
    
        MakeLuckyNumbers(numbers,min,max,num);
    
        printf("Your lucky numbers are...\n");
        for (int i = 0; i < num; i++)
          printf("%d  ",numbers[num]);
    
    
        printf("\n\n");
        return 0;
    }
    Last edited by CommonTater; 11-11-2011 at 02:51 AM. Reason: fixed random generator

  5. #5
    Banned
    Join Date
    Nov 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    But then you declare a new copy of N inside your random number function. This internal variable masks (hides) the one in main and is used in an uninitialized state in your function. You should be passing N into the function, not counting upon it's existence elsewhere...A good function performs 1 task... not half of your program's chores.
    Code:
    #include #include #include void MakeLuckyNumbers(int *array, int min, int max, int num)  {  int i;      for (i=0; i < num; i++)          array[i]= min + (rand() % (max - min));  }int main()  {     int min = -1;     int max = -1;     int num = -1;      int numbers[100];          printf("Lucky number generator... \n\n");     srand(time(NULL));     do        {          printf("How many numbers? : ");       }        while (scanf("%d", &num) < 1 || num < 0 || num > 100);    do        {          printf("The highest number? : ");       }        while (scanf("%d", &max) < 1 || max < 0);    do        {          printf("The lowest number? : ");       }        while (scanf("%d", &min) < 1 || min < 0 || min > max);    MakeLuckyNumbers(numbers,min,max,num);    printf("Your lucky numbers are...\n");    for (int i = 0; i < num; i++)      printf("%d  ",numbers[num]);    printf("\n\n");    return 0;}
    using my program how can i fix this N problem?and can u sent a program with bubblesort function?because i think bubblesort works differently than other functions and that while we are using it in the main function we have to do something different.and i want to ask if i can use this algorithm if i do some changes.will it work?because i am starting this that this program will never work.if this algorithm will do can u tell me using my program where in my algorithm i have to do the changes.it would be helpful if u could write it right next to where the changes are needed.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sam19911 View Post
    using my program how can i fix this N problem?and can u sent a program with bubblesort function?because i think bubblesort works differently than other functions and that while we are using it in the main function we have to do something different.and i want to ask if i can use this algorithm if i do some changes.will it work?because i am starting this that this program will never work.if this algorithm will do can u tell me using my program where in my algorithm i have to do the changes.it would be helpful if u could write it right next to where the changes are needed.
    I set you on the right track... showed you an example of how to do this...
    Now it's up to you to study and learn, then fix your own code.

    I am not going to do your homework for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 10-21-2011, 11:09 PM
  2. Sorting and Binning Random Numbers Problem....
    By meadmead in forum C Programming
    Replies: 4
    Last Post: 10-11-2011, 06:01 PM
  3. Sorting random numbers using pointing arrays.
    By jFran in forum C Programming
    Replies: 8
    Last Post: 05-10-2011, 01:56 PM
  4. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  5. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 04:47 AM

Tags for this Thread