468,771 Members | 1,553 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Problem With Swapping 2d Char Arrays, Bubble Sort

Hi dears!
I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping this array. please help me.

my scenario:
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 Idea:
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.
someone help!

here is new bubble sort function:
Expand|Select|Wrap|Line Numbers
  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. }
  30.  
and definiton of student array, if u wanna know:
Expand|Select|Wrap|Line Numbers
  1. #define number 20
  2. #define length 31
  3.  
  4. char student[number][length];
  5.  
Jan 27 '09 #1
7 6386
newb16
687 512MB
but the result is NULL.
result of what?

Anyway, you haven't allocated memory for 'mapped'. Then you don't swap mapped when you swap 'students', swap them as well or make some compare() function that compares two strings accordingto your layout.
Jan 27 '09 #2
Banfa
9,058 Expert Mod 8TB
instead of

char students[20][30];

why don't you use

Expand|Select|Wrap|Line Numbers
  1. struct student_t {
  2.     char first[15];
  3.     char last[15];
  4. } students[20];
  5.  
Then it is easy to isolate the first and last names.

NOTE the structure uses the same amount of space as your array (assuming no padding) and provides space for names up to 14 characters in length.
Jan 27 '09 #3
id don't want use structures. it is a beginners program for practicing.
Jan 27 '09 #4
@newb16
result of output.
so, if i want to allocate memory for mapped, how should i define and declare it. i know how to allocate memory for 1d arrays:

Expand|Select|Wrap|Line Numbers
  1. char* mapped = new char[15];
  2.  
what about 2d arrays. is this true?
Expand|Select|Wrap|Line Numbers
  1. char* mapped = new char[number][15];
  2.  
Jan 27 '09 #5
Banfa
9,058 Expert Mod 8TB
Firstly I would say a structure with 2 arrays in it is easier to manage than using a single array of char to hold 2 strings and therefore better suited for a beginner.

Secondly since you are using C++ you should be using std::string not arrays of char, you may as well learn the right way of doing things from the outset.

The allocation you would need would be
Expand|Select|Wrap|Line Numbers
  1.     char (*mapped)[15] = new char[20][15];
note, the parenthesise are important as they change the declaration from an array of pointers to a pointer to an array.

And finally in your original algorithm you use mapped to decided if entries in student need swapping but you never change the order of entries in mapped so the condition in the algorithm is always feed a list that still needs sorting.
Jan 27 '09 #6
thank u dear Banfa!
I learned how to allocate 2D arrays. very usefull. but I found out my mistake in another way and now swapping the original array fixed. I should swap mapped array and student array concerted and till i just swap student it keeps making the same comparisons over and over and over....
so the corrected code:
Expand|Select|Wrap|Line Numbers
  1. while (!done)
  2. {
  3.     done = true;
  4.     for (int n=0; n<number-1 ; n++)
  5.         if (strcmp(mapped[n], mapped[n+1]) > 0)
  6.         {
  7.                 char temp1[length+1],temp2[length+1];
  8.  
  9.                         strcpy(temp1,student[n]);
  10.                         strcpy(temp2, mapped[n]);
  11.  
  12.             strcpy(student[n], student[n+1]);
  13.                         strcpy(mapped[n], mapped[n+1]);
  14.  
  15.             strcpy(student[n+1], temp1);
  16.                         strcpy(mapped[n+1], temp2);
  17.  
  18.                  done = false;
  19.         }
  20. }
  21.  
thank u very much!
Jan 27 '09 #7
thank u dear Banfa!
I learned how to allocate 2D arrays. very usefull. but I found out my mistake in another way and now swapping the original array fixed. I should swap mapped array and student array concerted and till i just swap student it keeps making the same comparisons over and over and over....
so the corrected code:
Expand|Select|Wrap|Line Numbers
  1. while (!done)
  2. {
  3.     done = true;
  4.     for (int n=0; n<number-1 ; n++)
  5.         if (strcmp(mapped[n], mapped[n+1]) > 0)
  6.         {
  7.                 char temp1[length+1],temp2[length+1];
  8.  
  9.                         strcpy(temp1,student[n]);
  10.                         strcpy(temp2, mapped[n]);
  11.  
  12.             strcpy(student[n], student[n+1]);
  13.                         strcpy(mapped[n], mapped[n+1]);
  14.  
  15.             strcpy(student[n+1], temp1);
  16.                         strcpy(mapped[n+1], temp2);
  17.  
  18.                  done = false;
  19.         }
  20. }
  21.  
thank u very much!
Jan 27 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

270 posts views Thread by Jatinder | last post: by
10 posts views Thread by VA | last post: by
3 posts views Thread by ritchie | last post: by
5 posts views Thread by jab3 | last post: by
8 posts views Thread by meendar | last post: by
19 posts views Thread by rmr531 | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.