# Thread: Bubble Sort Code Problem...

1. ## Bubble Sort Code Problem...

Im not sure why this bubble sort code isn't working at all, could someone give me a hint ?
Help would be much appreciated. Thanks.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int array[2];
int temp[2][2];

int checkint(char s[]) //Checks if string is purely numerical
{
int length = strlen(s); // Define string length
for (int i = 0; i < length; i++) // For each character
{
if (isdigit(s[i])); // If numerical, then do nothing
else return 0; //If not, then return 0
}
return 1;
}

static void bubble_sort(int r[], int y)
{
int i, j, temp;
for (i = 0; i < y-1; i++);
for (j = 0; j < y-i-1; j++);
if (r[j] > r[j + 1])
{
temp = r[j];
r[j] = r[j + 1];
r[j + 1] = temp;
}
}

int main(int argc, char *argv[])
{
int temp = (argc - 2);
array[temp];
if(argc < 2)  //Check if enough arguments have been supplied
{
printf("Too few arguments for this program.\n");
exit(EXIT_FAILURE); //If not then exit
}
else
{
for (int i = 1; i < argc; i++) //For every argument provided (excluding 0)
{
int ans = atoi(argv[i]); //Find the int equivalent
int var = (i-1);
array[var] = ans; //Store it in the array
}

bubble_sort(array, argc);
for (int a = 0; a < argc-1; a++)
printf("%d ", array[a]);
printf("\n");

}
// Exit indicating success
exit(EXIT_SUCCESS);
return 0;
}```

2. Your belief that "array[temp]" does anything at all is misguided.

You appear to be lying to your bubble_sort function about how many numbers are in your array (is it temp or argc?).

3. Im a little confused with arrays, do they have to be initialized to a certain size before you can use them ? That is all I was trying to achieve with the array[temp] line. And there should be argc-1 numbers in the array, because argc returns a count of the arguments starting at 1 (or so I believe), but arrays start counting at 0.

4. Originally Posted by h4rrison.james
Im a little confused with arrays, do they have to be initialized to a certain size before you can use them ?
Yes. And once you type "array[2]" then you've had your chance and blew it, as arrays cannot be resized later. And if it was a declaration then naturally it would have to have a type in front of it. (You could, of course, redeclare a new array with the same name, hiding the one in global scope. But then you would never use the global array, so you might as well not have it anymore.)

Originally Posted by h4rrison.james
That is all I was trying to achieve with the array[temp] line. And there should be argc-1 numbers in the array, because argc returns a count of the arguments starting at 1 (or so I believe), but arrays start counting at 0.
If you type "myprogram 1 2 3 4 5 6 7 8", then argc is 9 (there are nine total arguments, the name of the program and the eight numbers). So you were "declaring" an array with 7 elements, filling in 8 numbers, and telling bubble_sort to sort 9 numbers.

5. Code:
```		if (isdigit(s[i])); // If numerical, then do nothing
else return 0; //If not, then return 0```
A case of "backwards if-statement. Test for "not isdigit" instead, and return 0 if that is true.

--
Mats

6. Ok but when if i were to declare the array "array" in the main function and not use the global scope one then you could not create pointers to the array that are passed onto functions and used as inputs could you ?

The reasonI had it set up like that was I have another function I removed from the code because it was working as far as I could see, and I thought the bubble sort was the only issue. I was also unaware you could not resize arrays. Is there no way then of declaring an array (in global scope) but not naming its size till later (in a local function) ?

matsp: I can see now that the if statement does sortof read in reverse, but if corrected I would still need the "else; //Else, do nothing" statement wouldnt I ?

Also, if you type "int r[4];" then aren't you declaring an array of 5 elements, since it starts from zero ? Or do you declare the ACTUAL amount of elements you want, not the number of the last element when you start counting from zero ?

7. Originally Posted by h4rrison.james
Ok but when if i were to declare the array "array" in the main function and not use the global scope one then you could not create pointers to the array that are passed onto functions and used as inputs could you ?
You can, as long as you do not have pointers to the array around when the function that has the variable returns.
matsp: I can see now that the if statement does sortof read in reverse, but if corrected I would still need the "else; //Else, do nothing" statement wouldnt I ?
No, if you don't have anything to do in the else, you can just not have an else, e.g.

Also, if you type "int r[4];" then aren't you declaring an array of 5 elements, since it starts from zero ? Or do you declare the ACTUAL amount of elements you want, not the number of the last element when you start counting from zero ?
No, the number when declaring arrays is the number of elements you want, so int arr[4] means an array with 4 elements, number 0..3.

--
Mats

8. Thanks for your help guys, you cleared up my understanding alot. Also I found that the two for statements in the bubble_sort functions had ";" after them, killing the loop, so I've got it working now