Bubble Sort Code Problem...

This is a discussion on Bubble Sort Code Problem... within the C Programming forums, part of the General Programming Boards category; Im not sure why this bubble sort code isn't working at all, could someone give me a hint ? Help ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    15

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    15
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by h4rrison.james View Post
    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.)

    Quote Originally Posted by h4rrison.james View Post
    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. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    15
    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 ?
    Last edited by h4rrison.james; 01-22-2009 at 06:27 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by h4rrison.james View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    15
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Bubble Sort Query
    By coolboarderguy in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 12:50 AM
  3. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 05:27 AM
  4. bubble sort
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 06-03-2002, 05:46 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21