Like Tree6Likes
  • 1 Post By laserlight
  • 3 Post By laserlight
  • 1 Post By iMalc
  • 1 Post By iMalc

[HELP]Bubblesort

This is a discussion on [HELP]Bubblesort within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { int swapCount = 0; int i, j; const int arraySize = ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    [HELP]Bubblesort

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    int swapCount = 0;
    int i, j;
    const int arraySize = 11;
    int array [arraySize] = (10,9,22,1,3,6,9,2,13,18,20);
    
    for(i < arraySize-1; i++;)
    {
            for(j < arraySize-1; j++;)
    {
                            if(array[j-1] > array[j])
    
                            {
                            int tempInt = array[j-1];
                            array[j-1] = array[j];
                            array[j] = tempInt;
                            }
                    }
    
    
    }
    }
    Produces this as an error:
    bubblesort.cpp: In function 'int main()':
    bubblesort.cpp:10: error: array must be initialized with a brace-enclosed initializer
    Also i need to add a working counter into this.
    TL;DR: Need help fixing the error and adding a working counter in to count how many swaps it does.
    Thanks in advanced for any help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,762
    Quote Originally Posted by ShiroAzure's compiler
    bubblesort.cpp:10: error: array must be initialized with a brace-enclosed initializer
    In other words, this:
    Code:
    int array [arraySize] = (10,9,22,1,3,6,9,2,13,18,20);
    should be:
    Code:
    int array [arraySize] = {10,9,22,1,3,6,9,2,13,18,20};
    ShiroAzure likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Hrmm

    When i run it it comes back with "Segmentation Fault"
    also i still cant get my counter working, any hints? :P

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,762
    Look at your for loops carefully. Normally, there are three parts, but you only use two of them.
    inequity, ShiroAzure and iMalc like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Confused

    ahh im stumped lol im still learning c++ so i have no idea what im missing xD

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    I'm really tempted to post what your for loops should look like, but really you can probably easily find 1 billion examples with Google.
    So, I'm giving you a nudge to at least go and find some example for-loops and see if you can work out how yours is different. Searching the web is a really important thing to get used to as a software developer as you never stop needing to do it, not even after 20 years of programming.
    Here is a useful link.
    Last edited by iMalc; 11-04-2011 at 05:51 PM.
    ShiroAzure likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Thanks iMalc, that link has helped alot, i checked out some examples and for some reason my code still isnt quite working...


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    int swapCount = 0;
    int i, j;
    const int arraySize = 11;
    int array [arraySize] = {10,9,22,1,3,6,9,2,13,18,20};
    
    for(i=0; i < arraySize-1; ++i){
            for(j=0; j < arraySize-1; ++j){
                            if(array[j-1] > array[j]){
                            int tempInt = array[j-1];
                            array[j-1] = array[j];
                            array[j] = tempInt;
                            }
                    }
    
            cout << "After Sorting: " << endl;
            for(i=0; i < arraySize-1; ++i){
            cout << array[j] << " ";
            }
    }
    }
    this code outputs : 20 20 20 20 20 20 20 20 20 20
    The output is clearly wrong and it only outputs 10 numbers instead of 20, ahhhh headache >.< lol
    Last edited by ShiroAzure; 11-04-2011 at 06:30 PM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Hmm, nobody else responded for two days. Wow sorry nobody replied earlier.
    It looks like the first problem is that your indentation is not correct. The reason that people on here usually very heavily strees the importance of indentation is that it really does solve a lot of problems. With proper indentation, your code looks like this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int swapCount = 0;
        int i, j;
        const int arraySize = 11;
        int array [arraySize] = {10,9,22,1,3,6,9,2,13,18,20};
        for (i=0; i < arraySize-1; ++i)
        {
            for (j=0; j < arraySize-1; ++j)
            {
                if (array[j-1] > array[j])
                {
                    int tempInt = array[j-1];
                    array[j-1] = array[j];
                    array[j] = tempInt;
                }
            }
            cout << "After Sorting: " << endl;
            for (i=0; i < arraySize-1; ++i)
            {
                cout << array[j] << " ";
            }
        }
    }
    Now, take note of where your final loop lies. That's right, at a glance you can now see that it's inside your sorting loop - oops!
    Format code perfectly - always!

    The next issue is a buffer overrun. the 'j' loop starts at zero and yet the code inside it refers to array[j-1]. Your inner loop should start at 1. It should also go up to less than arraySize, not less than arraySize-1.
    ShiroAzure likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Thank you very much iMalc for all your help, i got it working now! Yay! Im still amazed at how one wrong placement can mess up an entire code lol but hey, thats what the learning stage is all about right? :P Thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubblesort
    By mikemhz in forum C Programming
    Replies: 7
    Last Post: 10-25-2011, 08:27 PM
  2. Bubblesort Help
    By xanimeangiex in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2010, 04:33 PM
  3. Need help with Bubblesort
    By kburyanek in forum C Programming
    Replies: 9
    Last Post: 11-13-2009, 01:41 PM
  4. old and new bubblesort?
    By ingenting in forum C Programming
    Replies: 2
    Last Post: 05-11-2009, 01:41 PM
  5. bubblesort
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 10:45 PM

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