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; Code: #include <stdio.h> #include <stdlib.h> void bubblesort(int a[],int n); int main() { int comp, pass, tmp, n; int a[]; pass ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Help understanding bubblesort hw?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
      void bubblesort(int a[],int n);
       
       int main()
    {
        
        int comp, pass, tmp, n;
        int a[];
        pass = 1;
        while(comp <= n - pass)
         {
            if(a[comp-1] > a[comp])
             {
                tmp = a[comp-1]; 
                a[comp-1] = a[comp]; 
                a[comp] = tmp;
             }
            comp = comp + 1;
         }
        pass = pass + 1;
        system("pause");
        return 0;  
    }
    is what i have so far and it tells me that:
    array size missing in 'a'

    also im having trouble understanding the homework

    Write two functions, both accepts two arguments, an integer array: a and an integer: n. The first function outputs the n elements stored

    in the array, and the second function sorted the array using the basic bubble sort as provided in the notes with an improvement. The

    improvement is based on the following observation. When there is no exchange between any data in a pass of the bubble sort, the sequence

    is already sorted in non-decreasing order. As a result, there is no need to continue and hence can terminate the sort. Add an output

    statement to show the number of passes that the sorting algorithm has actually taken.

    Then write a C program to test your solution. It first requests n, the number of integers to be sorted. Then it input n integers. Then

    it calls the output function to output the original sequence of integers, then calls the bubble sort, and finally calls the output

    function to output the final sorted sequence.

    can anyone explain this more clearly for me?

    thank you!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by beretta View Post
    is what i have so far and it tells me that:
    array size missing in 'a'
    hmmm....could there be something obvious you have failed to investigate?!!??!
    Code:
    int a[];
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    well i tried it without that string, and it still gives me the same error sir. the point is that i don't know what size the array will be, im going to have to add to the program an input.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by beretta View Post
    the point is that i don't know what size the array will be.
    Yep. So pick something, or allocate dynamically with malloc().
    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. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    well i used 4, then i added

    printf("Please enter an integer: ");
    scanf("%d", &n);

    but when i do that nothing happens =\

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Code:
    #include <stdio.h>
    #include <stdlib.h>
      
       
    int main()
    {
       void bubblesort(int a[],int n); 
        int cmp, pass, tmp, n, a[4];
        printf("Please enter an integer: ");
        scanf("%d", &n);
        {
          pass = 1;
          while(cmp <= n)
          {
            cmp = 1;
            while(cmp <= n - pass)
             {
               if(a[cmp-1] > a[cmp])
                             {
                                tmp = a[cmp-1]; 
                                a[cmp-1] = a[cmp]; 
                                a[cmp] = tmp;
                             }
               cmp = cmp + 1;
             }
            pass = pass + 1;
          }
        }
        return 0;  
    }
    have that so far. does it look like im in the right direction?
    Last edited by beretta; 04-20-2009 at 12:31 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll want this part of your code in a loop will you not?

    Code:
        printf("Please enter an integer: ");
        scanf("%d", &n);
    May I suggest you use a #define Size 4, and then use a for loop from 0 to < Size to fill the array?

    Also, you declared a sort function, but so far, haven't created it, or called it.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    explain please?

    and i thought n would only be called initially.

    i also wasn't instructed or taught on using #define size 4, we have to use it as given =\

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Ok, i think i understand what you mean, first i need to set up the sort as in define it. then i can call it in the actual program, so like this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
      
    void bubblesort(int a[],int n) 
        {
          int cmp, pass, tmp;
          pass = 1;
          while(cmp <= n)
          {
            cmp = 1;
            while(cmp <= n - pass)
             {
               if(a[cmp-1] > a[cmp])
                             {
                                tmp = a[cmp-1]; 
                                a[cmp-1] = a[cmp]; 
                                a[cmp] = tmp;
                             }
               cmp = cmp + 1;
             }
            pass = pass + 1;
          }
        }    
    int main()
    {
        
        int cmp, pass, tmp, n, a[4];
        printf("Please enter an integer: ");
        scanf("%d", &n);     
        
    
        system("pause");
        return 0;  
    }

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    I think this is even a "neater" version, but tell me if i'm wrong.

    Code:
    /*Defining the bubblesort*/ 
    void bubblesort(int a[],int n) 
        {
          int cmp, pass, tmp;
          pass = 1;
          while(cmp <= n)
          {
            cmp = 1;
            while(cmp <= n - pass)
             {
               if(a[cmp-1] > a[cmp])
                             {
                                tmp = a[cmp-1]; 
                                a[cmp-1] = a[cmp]; 
                                a[cmp] = tmp;
                             }
               cmp = cmp + 1;
             }
            pass = pass + 1;
          }
        }    
    
    /* The actual method of sorting */
    #include <stdio.h>
    #include <stdlib.h>
    
    void bubblesort(int a[],int n); 
    
    int main()
    {
        int a[100];
        int cmp, pass, tmp, n;
        printf("Please enter an integer: ");
        scanf("%d", &n);
        
        
        system("pause");
        return 0;  
    }

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
          int cmp, pass, tmp;
          pass = 1;
          while(cmp <= n)
    Look at these 3 lines of code. On the third line, you are checking if cmp is less than or equal to n. You have never assigned any value to cmp though.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    yes i did, its 1

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No you didn't. Look again.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    cmp = 1;
    while(cmp <= n - pass)

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I'm talking about the code I posted.

Page 1 of 5 12345 LastLast
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, 05:21 PM
  2. Replies: 10
    Last Post: 12-05-2008, 12:47 PM
  3. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 06:53 AM
  4. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 07:46 PM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM

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