# Thread: Help needed in this simple bubble-sort program.

1. ## Help needed in this simple bubble-sort program.

Hello, I've very recently started to learn to program (in general) with my first language being the ANSI C. I've found an assignment which I'm trying to work on, but without any luck so far.
Write a short program that will calculate 10 random integers and store them in one-dimensional array, then present stored integers as they are and sorted in decreasing order. Use bubble sort algorithm and rand() function. Use array indexes only! The range of integers should be passed to your program as execution parameters.
I encountered two problems, 1st being the fact that bubble sort procedure doesn't work at all, 2nd - the program doesn't want to calculate more than 15 random integers. Here's the code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int sort (int numbers[], int array_size)
{
int b,j,temp;
for(b= (array_size-1);b>0;b--)
{
for(j=1; j<=b; j++)
{
if (numbers[j-1]>numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
int main (int argc, char* argv[])
{
int a,i,rgn,in,lowerlim,upperlim,store[argc-1];
lowerlim = atoi(argv[1]);
upperlim = atoi(argv[2]);
rgn = atoi(argv[3]);
srand(time(NULL));
i = 0;
printf("Unsorted elements:  ");
for (a = 0;a<(rgn);a++)
{
store[i] = (rand() % upperlim + lowerlim);
printf("%d ",store[i]);
i++;
}
printf("\nSorted elements:  ");
sort(store,rgn);
for (a=0;a<(rgn);a++)
{
printf("%d ",store[i]);
i++;
}
printf("\n");
return 0;
}```
I it may look terryfing to some, but like I've said, I'm very new to programming. I worked to fix this program for almost a day, but without any results. I'd be happy for any help/comments/remarks etc. regarding any part of the program that can be made better/simpler.

2. 1. Use more horizontal space (yes, it's a style thing)
2. store[argc - 1] is very probably not what you want. You know there will only be up to 10 numbers, so use store[10].
3. To calculate a random number between two limits the formula is (rand() % (hi - lo + 1)) + lo (thanks to GReaper).

I haven't checked your sort function.

Have fun!

Edit: formula corrected

3. Code:
```for (a=0;a<(rgn);a++)
{
printf("%d ",store[i]);
i++;
}```
This is wrong as alredy i is greater than rgn, it won't print the sorted array.
Sorting is working fine.

Initialize i again to '0' or the following code also works fine.
Code:
```for (a=0;a<(rgn);a++)
{
printf("%d ",store[a]);
}```

4. It worked! Thank you both for your help, I can't believe the solution was that easy.

5. Originally Posted by qny
To calculate a random number between two limits the formula is (rand() * (hi - lo)) + lo.
rand() % (high - low + 1) + low

6. Originally Posted by GReaper
rand() % (high - low + 1) + low

Unless you interpret "between" to mean a half-open interval [low,high), which includes the low number but excludes the high number. But this really depends on context and actual requirements for the program.