Thread: sorting random numbers with bubblesort

  1. #1
    Banned
    Join Date
    Nov 2011
    Posts
    8

    sorting random numbers with bubblesort

    I have a program that i have been working for days.but i can't figure out why it won't work.i am running out of time.this is what i have done so far.it is suppose to run like this.
    >Enter Min Value(e.g.1)
    >Enter Max Value(e.g.48)
    >Enter Number of Random Variables(e.g.6)
    >your lucky Numbers:
    32 8 21 41 1
    >Your lucky numbers(sorted):
    1 8 21 23 32 41
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <time.h>
    int genrand(int minv,int maxv);
    int generate_int_rnd_number(int min_value,int max_value);
    void bubbleSort(int numbers[],int array_size);
    int main()
    {
    int min_value,max_value,N,i,numbers[100],array_size;
    printf("Enter Min Value:");
    scanf("%d",&min_value);
    printf("Enter Max Value:");
    scanf("%d",&max_value);
    printf("Enter Number of Random Variables:");
    scanf("%d",&N);
    srand(time(0));
    numbers[i]=generate_int_rnd_number(min_value,max_value);
    numbers[i]=(numbers[100],array_size);
    return 0;
    }
    int genrand(int minv,int maxv)
    {
    return (rand()%(maxv-minv)+minv);
    }
    int generate_int_rnd_number(int min_value,int max_value)
    {
    int i,N,numbers[i];
    printf("Your Lucky Numbers:\n");
    for (i=0; i < N; i++)
    {
    numbers[i]=genrand(min_value,max_value);
    printf("%d\n",numbers[i]);
    }
    }
    void bubbleSort(int numbers[],int array_size)
    {
    int i,temp,j,N;
    for (i=0; i < array_size-1; i++)
    for (j=0; j < array_size-i-1; j++)
    if (numbers[j] > numbers[j+1])
    {
    temp=numbers[j];
    numbers[j]=numbers[j+1];
    numbers[j+1]=temp;
    }
    printf("Your Lucky Numbers(sorted):\n");
    for (i=0; i < N; i++)
    printf("%d\n",numbers[i]);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First... indent your code properly and leave blank lines between sections. Done properly you will have a visual indication of how your code works.

    So, what's the problem with it?
    What error messages are you getting?

  3. #3
    Banned
    Join Date
    Nov 2011
    Posts
    8
    it runs perfectly up to the part where it asks for random numbers but then says that there is a segmentation fault.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You fail to initialize i, then use it and it's unknown value as an index for array numbers.

  5. #5
    Banned
    Join Date
    Nov 2011
    Posts
    8
    where in the code did i fail to do that?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    int main()
    {
    int min_value,max_value,N,i,numbers[100],array_size;
    
    numbers[i]=generate_int_rnd_number(min_value,max_value);
    numbers[i]=(numbers[100],array_size);
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Sam19911 View Post
    I have a program that i have been working for days.but i can't figure out why it won't work.i am running out of time.this is what i have done so far.it is suppose to run like this.
    >Enter Min Value(e.g.1)
    >Enter Max Value(e.g.48)
    >Enter Number of Random Variables(e.g.6)
    >your lucky Numbers:
    32 8 21 41 1
    >Your lucky numbers(sorted):
    1 8 21 23 32 41
    Code:
    int generate_int_rnd_number(int min_value,int max_value);
    
    int main()
    {
    int min_value,max_value,N,i,numbers[100],array_size;
    
    numbers[i]=generate_int_rnd_number(min_value,max_value);
    numbers[i]=(numbers[100],array_size);
    return 0;
    }
    
    
    int generate_int_rnd_number(int min_value,int max_value)
    {
       int i,N,numbers[i];  // what is the value of i ?
    
       printf("Your Lucky Numbers:\n");
       
       for (i=0; i < N; i++)  // what is the value of N ?
      {
         numbers[i]=genrand(min_value,max_value);
         printf("%d\n",numbers[i]);
      }
    }
    First, do you understand that i and N are separate local variables inside the generate_int_rnd_number() function? And that when you declare them, all you are doing is instantiating an identifier that points to some memory address which contains some unknown "junk" of zeros and ones? If I told you that i's value at that time was 32,500 - would you then think that declaring numbers[i] was wise?
    On the same note, what is N's value? for (i = 0 ; i < N ; i++ ) ... you have no idea what N is at this point.

    Lastly, this function is declared as type int and you attempt to assign the function call to numbers[i] yet your function returns nothing.

    A better algorithm than what you have would probably help you figure out what to do in code.

  8. #8
    Banned
    Join Date
    Nov 2011
    Posts
    8
    a better algorithm like what?

  9. #9
    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.
    ...

  10. #10
    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.

  11. #11
    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.

  12. #12
    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

  13. #13
    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.

  14. #14
    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.

  15. #15
    Banned
    Join Date
    Nov 2011
    Posts
    8
    No help is needed

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