Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bubble sort

Reply
Thread Tools

bubble sort

 
 
zfareed@mail.com
Guest
Posts: n/a
 
      08-28-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-28-2006
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


 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      08-28-2006

> 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




 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      08-28-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: C++ priority queue with bubble-up and bubble-down? mlimber C++ 0 08-05-2008 06:53 PM
Selection sort and bubble sort lovecreatesbea...@gmail.com C Programming 22 10-19-2007 06:16 AM
Number of exchanges in a bubble sort SneakyElf@gmail.com C++ 3 09-03-2007 08:36 PM
Double Bubble Sort Algorithm Fails to Sort AT ALL Protoman C++ 8 04-05-2006 01:22 PM
Two-Dimensional Bubble Sort Scott Lyons C++ 1 05-03-2004 06:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57