# Thread: sorting random numbers with bubblesort

1. ## 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)
32 8 21 41 1
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];
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;
}
for (i=0; i < N; i++)
printf("%d\n",numbers[i]);
}```

2. 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. it runs perfectly up to the part where it asks for random numbers but then says that there is a segmentation fault.

4. You fail to initialize i, then use it and it's unknown value as an index for array numbers.

5. where in the code did i fail to do that?

6. 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. Originally Posted by Sam19911
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)
32 8 21 41 1
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 ?

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. a better algorithm like what?

9. 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. İ 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. 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. Originally Posted by Sam19911
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);

for (int i = 0; i < num; i++)
printf("%d  ",numbers[num]);

printf("\n\n");
return 0;
}```

13. Originally Posted by CommonTater
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. Originally Posted by Sam19911
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. No help is needed