bubble sort

Z

zfareed

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
 
V

Victor Bazarov

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
 
G

Gernot Frisch

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
 
B

Bart

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

template error 10
trouble with STL 4
linker error w/ templates 5
Number of exchanges in a bubble sort 3
Dev C++ compile problem 2
Bubble Sort in C 48
sort input 7
Vecotor of Pairs 8

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top