bubble sort

Discussion in 'C++' started by zfareed@mail.com, Aug 28, 2006.

  1. Guest

    I am trying to sort a 2-dim array of students lastnames. I keep getting
    errors that I cannot assign arrays.

    void bubbleSort (char Lstname[][10], int n)
    {

    bool swapped = true;

    while(swapped){
    swapped = false;

    for (int i = 0; i < 40 - 1 ; ++i)
    if (Lstname > Lstname[i+1]) {

    swap(Lstname, Lstname[i+1]);
    swapped = true;
    }
    }
    }
    I get these error messages on compilation:

    C:\Documents and Settings\Admin\My
    Documents\C++\assignments\prog5.cpp:215: instantiated from here
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:130: error: invalid
    initializer
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:131: error: ISO C++
    forbids assignment of arrays
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:132: error: ISO C++
    forbids assignment of arrays

    Execution terminated
     
    , Aug 28, 2006
    #1
    1. Advertising

  2. wrote:
    > I am trying to sort a 2-dim array of students lastnames. I keep
    > getting errors that I cannot assign arrays.


    Well, you really need to turn your attention to 'std::string' and
    stop using arrays of char to represent strings.

    >
    > void bubbleSort (char Lstname[][10], int n)
    > {
    >
    > bool swapped = true;
    >
    > while(swapped){
    > swapped = false;
    >
    > for (int i = 0; i < 40 - 1 ; ++i)
    > if (Lstname > Lstname[i+1]) {
    >
    > swap(Lstname, Lstname[i+1]);


    You're trying to swap arrays here. The default implementation of
    'swap' simply tries to assign the values. You need to provide your
    own (custom) implementation (specialisation) of 'swap', something
    like

    template<class T, size_t s>
    void swap(T (&a1), T (&a2))
    {
    for (size_t i = 0; i < s; ++i)
    swap(a1, a2);
    }

    and place it outside your 'bubbleSort'

    > swapped = true;
    > }
    > }
    > }
    > I get these error messages on compilation:
    >
    > C:\Documents and Settings\Admin\My
    > Documents\C++\assignments\prog5.cpp:215: instantiated from here
    > C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:130: error: invalid
    > initializer
    > C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:131: error: ISO C++
    > forbids assignment of arrays
    > C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:132: error: ISO C++
    > forbids assignment of arrays
    >
    > Execution terminated


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 28, 2006
    #2
    1. Advertising


  3. > void bubbleSort (char Lstname[][10], int n)
    > {
    >
    > bool swapped = true;
    >
    > while(swapped){
    > swapped = false;
    >
    > for (int i = 0; i < 40 - 1 ; ++i)
    > if (Lstname > Lstname[i+1]) {
    >
    > swap(Lstname, Lstname[i+1]);


    as said: this tries to swap an array of chars.
    Another C++ solution is to change

    char Lstname[][10], int n

    to

    std::vector<std::string>& Lstname
     
    Gernot Frisch, Aug 28, 2006
    #3
  4. Bart Guest

    wrote:
    > I am trying to sort a 2-dim array of students lastnames. I keep getting
    > errors that I cannot assign arrays.
    >
    > void bubbleSort (char Lstname[][10], int n)
    > {
    >
    > bool swapped = true;
    >
    > while(swapped){
    > swapped = false;
    >
    > for (int i = 0; i < 40 - 1 ; ++i)
    > if (Lstname > Lstname[i+1]) {
    >
    > swap(Lstname, Lstname[i+1]);
    > swapped = true;
    > }
    > }
    > }
    > I get these error messages on compilation:
    >
    > C:\Documents and Settings\Admin\My
    > Documents\C++\assignments\prog5.cpp:215: instantiated from here
    > C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:130: error: invalid
    > initializer
    > C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:131: error: ISO C++
    > forbids assignment of arrays
    > C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:132: error: ISO C++
    > forbids assignment of arrays
    >
    > Execution terminated


    If you're learning this in a course you should demand that your
    instructor teaches you about real C++ strings, instead of character
    arrays. In the mean time google for 'std::string' and use that instead.

    Regards,
    Bart.
     
    Bart, Aug 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Lyons

    Two-Dimensional Bubble Sort

    Scott Lyons, May 3, 2004, in forum: C++
    Replies:
    1
    Views:
    389
    Scott Lyons
    May 3, 2004
  2. Protoman
    Replies:
    8
    Views:
    516
    Richard Herring
    Apr 5, 2006
  3. Replies:
    3
    Views:
    463
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Sep 3, 2007
  4. Selection sort and bubble sort

    , Oct 16, 2007, in forum: C Programming
    Replies:
    22
    Views:
    1,804
    user923005
    Oct 19, 2007
  5. mlimber
    Replies:
    0
    Views:
    839
    mlimber
    Aug 5, 2008
Loading...

Share This Page