Help understanding bubblesort hw?

This is a discussion on Help understanding bubblesort hw? within the C Programming forums, part of the General Programming Boards category; Originally Posted by beretta didn't work for me That post??? I tested it. What seems wrong?...

  1. #61
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by beretta View Post
    didn't work for me
    That post??? I tested it. What seems wrong?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #62
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    set n = sizeof(a) / sizeof(a[0] if you want to use sizeof().

    It stinks though, because you may (and frequently will), make your array larger than you need, and just use part of it for your data.

    Then sizeof() just sucks eggs, and you'll need n (some variable), to tell your program how many numbers, or strings or whatever, you need to have sorted, in your array. Sizeof() can never do that.


    OK, off my soapbox, down to business:

    1) If you're going to have the user input the values into a[], then remove the numbers now being put into a, by the program, at initialization.

    2) set n to *something* that you want, that tells bubblesort what size the array is (or what number of elements it needs to sort).

    3)Put the user input inside a while loop.

  3. #63
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've got to hit the sack for some zzzz's.

    Good luck, Baretta.

  4. #64
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    I've got to hit the sack for some zzzz's.
    Good idea Adak, you usually seem more lucid than this
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #65
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    =( )

  6. #66
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* The actual method of sorting */
    
    void bubblesort(int a[],int n);
    
    int main()
    {
        int n;
        int list = 0;
        int a[10] = { 4, 8, 2, 1, 6, 5, 3, 9, 8, 1};
        printf("Please enter an integer: ");
        scanf("%d", &n);
        printf("Before: ");
        while(list < sizeof(a) / sizeof(a[0]))
            printf("%d", a[list++]);
        printf("\n");
        bubblesort(a, sizeof(a) / sizeof(int));
        list = 0;
        printf("After: ");
        while(list < sizeof(a) / sizeof(a[0]))
            printf("%d", a[list++]);
        printf("\n");
        system("pause");
        return 0;  
    }
    
    
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
    {
        int cmp, pass, tmp, swch;
        pass = 0;
        cmp = 0;
        swch = 1;
        while(swch)
        {
            swch = 0;
            cmp = 1;
            while(cmp < n - pass)
            {
                if(a[cmp-1] > a[cmp])
                {
                     swch = 1;
                     tmp = a[cmp-1]; 
                     a[cmp-1] = a[cmp]; 
                     a[cmp] = tmp;
                }
                cmp++;
            }    
            pass++; 
        }     
        printf("Our Sort had %d comparisons, and %d passes\n", cmp, pass);
    }
    ok apparently i am suppose to make the n a variable that hte user types in:
    - program asks for n
    -then you type in the numbers you would like to sort

    so if n = 6 there will be six numbers to sort.

    any ideas, ill reply when i get back from work

  7. #67
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by beretta View Post
    ok apparently i am suppose to make the n a variable that hte user types in:
    - program asks for n
    -then you type in the numbers you would like to sort
    That is exactly what I was showing you in post #58! You do not have to do it that way, of course.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #68
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Yea i understand what you are saying, i'm workin on it right now

  9. #69
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    Question I see it a different way, tell me if i'm too far off

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int BubbleSort(int info, int next);//returns the lesser of the 2.
    
    void main(void)
    {
    
    int *ptr = malloc(10 * sizeof (int));// will hold entire input array
    int next=0;int info;
    
    int i;
    
    for (i=0;i<<*ptr;i++)
    {
    	
    	printf("Enter Number Please:");
    	scanf("%i",&info);
    	if (info<=next) 
    	{
    		*ptr=info;
    		
    	}
    	else *ptr=BubbleSort(info,next);
    	printf("%i, ",*ptr);
    	ptr++;
    }
    }
    int BubbleSort(int info, int next)
    {
    	int tempInfo;
    	tempInfo=info;
    	info=next;
    	next=tempInfo;
    	return info;
    }
    only I haven't figured out why nothing is being output by the printf("%i, ", *ptr);

    Am I close?


    - Edward

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Replies: 10
    Last Post: 12-05-2008, 11:47 AM
  3. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 05:53 AM
  4. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 11:56 AM

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