A Problem With 2D Array Bubble Sorting

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 12
Reputation: mahdiahmadirad is an unknown quantity at this point 
Solved Threads: 0
mahdiahmadirad mahdiahmadirad is offline Offline
Newbie Poster

A Problem With 2D Array Bubble Sorting

 
0
  #1
Jan 26th, 2009
Hi Dears!
I Wrote A Bubble Sort for sorting 2D array of characters. and logically it seems correct, but unfortunately not works. please help me.
this is my output:
unsorted:
mah
ali
sal

Sorted:
ali
ali
mah

Here is my Code so far:
  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define number 3
  5. #define length 3
  6. char student[number][length];
  7. int i,j,k;
  8. bool done;
  9. void BubbleSort (void);
  10.  
  11. void main(){
  12. for(i=0;i<number;i++)
  13. for(j=0;j<length;j++)
  14. cin>>student[i][j];
  15.  
  16. BubbleSort();
  17. cout<<endl;
  18.  
  19. for(i=0;i<number;i++){
  20. for(j=0;j<length;j++){
  21. cout<<student[i][j];}
  22. cout<<endl;}
  23. getchar();
  24. }
  25. void BubbleSort (void)
  26. {
  27. bool done = false;
  28. int limit = 0,size=number;
  29. while (!done)
  30. {
  31. done = true;
  32. for (int n=0; n<size-limit-1 ; n++)
  33. if (strcmp(student[n], student[n+1]) > 0)
  34. {
  35. char temp[10];
  36. strcpy(temp,student[n]);
  37. strcpy(student[n], student[n+1]);
  38. strcpy(student[n+1], temp);
  39. done = false;
  40. }
  41. limit++;
  42. }
  43. }

what is my mistake? I can't Understand.
please help me!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: A Problem With 2D Array Bubble Sorting

 
0
  #2
Jan 26th, 2009
Your array is not wide enough to hold the names. To hold names with three characters ( "ali" ) you need to have 4 columns.

What you really have is three arrays of char, not strings, so strcpy is not really working right.
  1. char student[3][4];
  2. //
  3. for(i=0;i<number;i++)
  4. cin >> student[i]; //limit name to 3 char max
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 12
Reputation: mahdiahmadirad is an unknown quantity at this point 
Solved Threads: 0
mahdiahmadirad mahdiahmadirad is offline Offline
Newbie Poster

Re: A Problem With 2D Array Bubble Sorting

 
0
  #3
Jan 26th, 2009
Originally Posted by vmanes View Post
Your array is not wide enough to hold the names. To hold names with three characters ( "ali" ) you need to have 4 columns.
thank u Dear. I corrected the code according to your guidance. but it is not working yet! although it worked prroper with "mah,ali,sal" inputs but for another names not. for example:
input:
mah
aaa
ali

output:
aaa
mah
ali

what u think?
really how can I use 3 length for array length. because this is a small part of a complete project if I wide array length i will have some problems in other places.
willy-nilly here is my corrected code:
changes signned with //*
  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define number 3
  5. //*
  6. #define length 4
  7. char student[number][length];
  8. int i,j,k;
  9. bool done;
  10. void BubbleSort (void);
  11.  
  12. void main(){
  13. for(i=0;i<number;i++)
  14. //*
  15. for(j=0;j<length-1;j++)
  16. cin>>student[i][j];
  17.  
  18. BubbleSort();
  19. cout<<endl;
  20.  
  21. for(i=0;i<number;i++){
  22. //*
  23. for(j=0;j<length-1;j++){
  24. cout<<student[i][j];}
  25. cout<<endl;}
  26. getchar();
  27. }
  28. void BubbleSort (void)
  29. {
  30. bool done = false;
  31. //*
  32. int limit = 0,size=number-1;
  33. while (!done)
  34. {
  35. done = true;
  36. for (int n=0; n<size-limit-1 ; n++)
  37. if (strcmp(student[n], student[n+1]) > 0)
  38. {
  39. char temp[number];
  40. strcpy(temp,student[n]);
  41. strcpy(student[n], student[n+1]);
  42. strcpy(student[n+1], temp);
  43. done = false;
  44. }
  45. limit++;
  46. }
  47. }
why srtcpy(,) is not nice here?
thank u very very much!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: A Problem With 2D Array Bubble Sorting

 
0
  #4
Jan 26th, 2009
Your bubble sort has a few errors still. Most importantly, it was never comparing to the last name in the list. Here's a corrected version.
  1. void BubbleSort (void)
  2. {
  3. bool done = false;
  4. //*
  5. int limit = 0,size=number; // don't subtract one from the limit here
  6. while (!done)
  7. {
  8. done = true;
  9. for (int n=0; n<size-limit-1 ; n++)
  10. if (strcmp(student[n], student[n+1]) > 0)
  11. {
  12. char temp[length]; //this must be size 4, same as array width
  13. strcpy(temp,student[n]);
  14. strcpy(student[n], student[n+1]);
  15. strcpy(student[n+1], temp);
  16. done = false;
  17. }
  18. //limit++; //this serves no purpose
  19. }
  20. }

Also, you really shouldn't be reading in the names one character at a time. That's inefficient, and limits the names to a specific size.

To use the string functions (strcmp, strcpy) you must have valid strings. Just declaring the array, then placing some characters into it does not make an array of strings. What you end up with is
mah?
ali?
aaa? where the ? is some random value

Using string input ( cin >> students[i] ) puts the null terminator (ASCII value 0) at the end of the names. This requires the array width be at least one larger than the largest name you expect, so 4 in this case.
You can cheat a little by initializing the array to all null terminators like so
char student[number][length] = { "" }; When you fill in the 3x3 characters, the end column will already hold the null, and your strings are valid.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 12
Reputation: mahdiahmadirad is an unknown quantity at this point 
Solved Threads: 0
mahdiahmadirad mahdiahmadirad is offline Offline
Newbie Poster

Re: A Problem With 2D Array Bubble Sorting

 
0
  #5
Jan 27th, 2009
thank u dear vmanes!
your comment was helpful. my problem solved.
all the best.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 12
Reputation: mahdiahmadirad is an unknown quantity at this point 
Solved Threads: 0
mahdiahmadirad mahdiahmadirad is offline Offline
Newbie Poster

Re: A Problem With 2D Array Bubble Sorting

 
0
  #6
Jan 27th, 2009
Now I have a new problem.
assume that we have a big 2d char array for example students[20][30] for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name.
no i want to sort this array according to last name.

my scenario: i defined char mapped[number][16]={""} and mapped 2nd 15 chars of student array. so mapped[i]=student[i] but the result is NULL. I don't now why!!!
when showing student there is nothing to display on screen.
please help me!

here is new bubble sort:
  1. void BubbleSort (void)
  2. {
  3. char mapped[number][21]={""};
  4. for(i=0;i<number;i++)
  5. for(j=0;j<15;j++)
  6. mapped[i][j]=student[i][j+15];
  7.  
  8. cout<<endl<<"mapped List:"<<endl;
  9. for(i=0;i<number;i++)
  10. cout<<mapped[i]<<endl;
  11. //now bubble sorting
  12. bool done = false;
  13. while (!done)
  14. {
  15. done = true;
  16. for (int n=0; n<number-1 ; n++)
  17. if (strcmp(mapped[n], mapped[n+1]) > 0)
  18. {
  19. char temp[length+1];
  20. strcpy(temp,student[n]);
  21. strcpy(student[n], student[n+1]);
  22. strcpy(student[n+1], temp);
  23. done = false;
  24. }
  25. }
  26. cout<<endl<<"Sorted Student List:"<<endl;
  27. for(i=0;i<number;i++)
  28. cout<<student[i]<<endl;//nothing displayed!!!
  29. }

student array
  1. #define number 20
  2. #define length 31
  3.  
  4. char student[number][length];
Last edited by mahdiahmadirad; Jan 27th, 2009 at 4:46 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: A Problem With 2D Array Bubble Sorting

 
0
  #7
Jan 27th, 2009
When you do the swaps of the students' full names, you must also do same swaps to the mapped array! Otherwise, you just keep making the same comparisons over and over and over....
  1. strcpy(temp,student[n]);
  2. strcpy(temp2, mapped[n]);
  3. strcpy(student[n], student[n+1]);
  4. strcpy(mapped[n], mapped[n+1]);
  5. strcpy(student[n+1], temp);
  6. strcpy(mapped[n+1], temp);
  7. done = false;
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 12
Reputation: mahdiahmadirad is an unknown quantity at this point 
Solved Threads: 0
mahdiahmadirad mahdiahmadirad is offline Offline
Newbie Poster

Re: A Problem With 2D Array Bubble Sorting

 
0
  #8
Jan 27th, 2009
Your answer was exactly truth. thank u very much. you corrected my mistake perfectly. that is the answer.
thank u code:
  1. char * MyFriend ="vmanes";
  2. int i=0;
  3. cout<<MyFriend<<" is number "<<i+1<<endl;

can you correct mistakes of this code? no, it hasn't any mistake!
Last edited by mahdiahmadirad; Jan 27th, 2009 at 3:51 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1157 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC