468,513 Members | 1,760 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

bubble sort

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[i] Lstname[i+1]) {

swap(Lstname[i], 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 '06 #1
3 6613
zf*****@mail.com 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[i] Lstname[i+1]) {

swap(Lstname[i], 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)[s], T (&a2)[s])
{
for (size_t i = 0; i < s; ++i)
swap(a1[i], a2[i]);
}

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
Aug 28 '06 #2
void bubbleSort (char Lstname[][10], int n)
{

bool swapped = true;

while(swapped){
swapped = false;

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

swap(Lstname[i], 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


Aug 28 '06 #3
zf*****@mail.com 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[i] Lstname[i+1]) {

swap(Lstname[i], 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.

Aug 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Gram | last post: by
34 posts views Thread by Mark Kamoski | last post: by
4 posts views Thread by Chris | last post: by
reply views Thread by Slowjam | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.