# Thread: A Problem Between "creating an array using rand()" and "the use of bubble"

1. ## A Problem Between "creating an array using rand()" and "the use of bubble"

I would like to create an array which has 99 elements using function rand(). I created, and sorted with function bubblesort. But I met something that I cannot construe. The array was sorted as changed. The elements of array have changed. Why? What do you think about it? How it could be happen?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 99

void bubblesort(int[],int);

int main()
{
int response[SIZE];
int i;

srand(time(NULL));

for(i=0;i<SIZE;i++){
response[i]= 1 + rand()%9;
}

printf("An Array which is created with function rand() :\n");
for(i=0;i<SIZE;i++) {
if(i%10==0) {
printf("\n%3d",response[i]);
}
else
printf("%3d",response[i]);
}

bubblesort(response,SIZE);

printf("\n\n\nAfter using bubble sort, Array :\n");
for(i=0;i<SIZE;i++) {
if(i%10==0) {
printf("\n%3d",response[i]);
}
else
printf("%3d",response[i]);
}

return 0;
}

void bubblesort(int array[],int length)
{
int pass,compare,hold;

for(pass=0; pass<length; pass++){
for(compare=0; compare<length-1; compare++){
if(array[compare] > array[compare+1]){
//int hold;
hold = array[compare];
array[compare] = array[compare+1];
array[compare+1] = array[compare];
}
}
}
}```
*I tried to this programme with small-scale numbers. And it worked successfully. The programme with small-scale numbers is in the following:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10

void bubblesort(int[],int);

int main()
{
int response[SIZE];
int i;

srand(time(NULL));

for(i=0;i<SIZE;i++){
response[i]= 1+ rand() %3;
}

for(i=0;i<SIZE;i++){
printf("%3d",response[i]);
}

bubblesort(response,SIZE);

printf("\n\n\nAfter using bubble sort, Array :\n");
for(i=0;i<SIZE;i++) {
printf("%3d",response[i]);
}

return 0;
}

void bubblesort(int array[],int length)
{
int pass,hold,compare;

for(pass=0;pass<length;pass++) {
for(compare=0;compare<SIZE-1;compare++) {
if(array[compare]>array[compare+1]) {
hold = array[compare];
array[compare]=array[compare+1];
array[compare+1]=hold;
}
}
}

}```

2. I don't see anything wrong with your first program as far a the sorting goes. However it should not compile as presented. You seem to have at least one missing closing brace in your main() function. Your second program works correctly and if you change the size to a higher value the program seems to work correctly.

Jim

3. ## They are editted.

When I was preparing to share the code here, I forgot something like if-else expression to display well. Now, I editted the code again.

I notice that if preprocess #define SIZE 10 expression is incremented in the second program such as 20,30,...90 , the programme can be run correctly. But after the first programme is builded and run, it's displayed as folows or something like that:

I cannot find difference or differences between two programs above. Don't have to that both are being run properly?

Note: I editted inside loop-for, e.i. if(i%10==0)), in the second programme to display very well if you increment the preprocess SIZE.

Code:
```//SECOND PROGRAMME(SMALL-SCALE NUMBERS)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10

void bubblesort(int[],int);

int main()
{
int response[SIZE];
int i;

srand(time(NULL));

for(i=0;i<SIZE;i++){
response[i]= 1+ rand() %3;
}

printf("An Array which is created with function rand() :\n");
for(i=0;i<SIZE;i++) {
if(i%10==0) {
printf("\n%3d",response[i]);
}
else
printf("%3d",response[i]);

}

bubblesort(response,SIZE);

printf("\n\n\nAfter using bubble sort, Array :\n");
for(i=0;i<SIZE;i++) {
if(i%10==0) {
printf("\n%3d",response[i]);
}
else
printf("%3d",response[i]);

}

return 0;
}

void bubblesort(int array[],int length)
{
int pass,hold,compare;

for(pass=0;pass<length;pass++) {
for(compare=0;compare<SIZE-1;compare++) {
if(array[compare]>array[compare+1]) {
hold = array[compare];
array[compare]=array[compare+1];
array[compare+1]=hold;
}
}
}
}```

4. Compare your two "swap" routines. Are they the same?
From program 1:
Code:
```hold = array[compare];
array[compare] = array[compare+1];
array[compare+1] = array[compare]; // Check this line out.```
From program 2:
Code:
```hold = array[compare];
array[compare]=array[compare+1];
array[compare+1]=hold; // Is this line the same?```
Jim

5. Upss... Well. I don't know how I couldn't see it. Thank you so much for your responding and your affine , Jim.