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:

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define number 3
#define length 3
char student[number][length];
int i,j,k;
bool done;
void BubbleSort (void);

void main(){
		for(i=0;i<number;i++)
	  	for(j=0;j<length;j++)
      cin>>student[i][j];

      BubbleSort();
      cout<<endl;

	  	for(i=0;i<number;i++){
	  	for(j=0;j<length;j++){
      cout<<student[i][j];}
      cout<<endl;}
	getchar();
}
void BubbleSort (void)
{
     bool done = false;
     int limit = 0,size=number;
      while (!done)
      {
          done = true;
          for (int n=0; n<size-limit-1 ; n++)
          if (strcmp(student[n], student[n+1]) > 0)
           {
                char temp[10];
    				 strcpy(temp,student[n]);
                strcpy(student[n], student[n+1]);
                strcpy(student[n+1], temp);
                done = false;
           }
        limit++;
      }
}

what is my mistake? I can't Understand.
please help me!

Recommended Answers

All 7 Replies

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.

char student[3][4];
//
for(i=0;i<number;i++)
  cin >> student[i];  //limit name to 3 char max

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 //*

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define number 3
//*
#define length 4
char student[number][length];
int i,j,k;
bool done;
void BubbleSort (void);

void main(){
	for(i=0;i<number;i++)
	//*
      for(j=0;j<length-1;j++)
      cin>>student[i][j];

      BubbleSort();
      cout<<endl;

	for(i=0;i<number;i++){
	//*
      for(j=0;j<length-1;j++){
      cout<<student[i][j];}
      cout<<endl;}
	getchar();
}
void BubbleSort (void)
{
     bool done = false;
     //*
      int limit = 0,size=number-1;
      while (!done)
      {
          done = true;
          for (int n=0; n<size-limit-1 ; n++)
          if (strcmp(student[n], student[n+1]) > 0)
           {
                char temp[number];
    		strcpy(temp,student[n]);
                strcpy(student[n], student[n+1]);
                strcpy(student[n+1], temp);
                done = false;
           }
        limit++;
      }
}

why srtcpy(,) is not nice here?
thank u very very much!

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.

void BubbleSort (void)
{
   bool done = false;
   //*
   int limit = 0,size=number; // don't subtract one from the limit here
   while (!done)
   {
      done = true;
      for (int n=0; n<size-limit-1 ; n++)
         if (strcmp(student[n], student[n+1]) > 0)
         {
            char temp[length];  //this must be size 4, same as array width
            strcpy(temp,student[n]);
            strcpy(student[n], student[n+1]);
            strcpy(student[n+1], temp);
            done = false;
         }
         //limit++;   //this serves no purpose
   }
}

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 ) 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.

thank u dear vmanes!
your comment was helpful. my problem solved.
all the best.

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=student 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:

void BubbleSort (void)
{
   char mapped[number][21]={""};
  	for(i=0;i<number;i++)
		for(j=0;j<15;j++)
			mapped[i][j]=student[i][j+15];

	cout<<endl<<"mapped List:"<<endl;
	for(i=0;i<number;i++)
		cout<<mapped[i]<<endl;
                //now bubble sorting
				bool done = false;
				while (!done)
				{
				    done = true;
				    for (int n=0; n<number-1 ; n++)
				    if (strcmp(mapped[n], mapped[n+1]) > 0)
				    {
				       char temp[length+1];
    			               strcpy(temp,student[n]);
				       strcpy(student[n], student[n+1]);
				       strcpy(student[n+1], temp);
				       done = false;
				     }
				}
	cout<<endl<<"Sorted Student List:"<<endl;
	for(i=0;i<number;i++)
		cout<<student[i]<<endl;//nothing displayed!!!
}

student array

#define number 20
#define length 31

char student[number][length];

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....

strcpy(temp,student[n]);
            strcpy(temp2, mapped[n]);
            strcpy(student[n], student[n+1]);
            strcpy(mapped[n], mapped[n+1]);
            strcpy(student[n+1], temp);
            strcpy(mapped[n+1], temp);
            done = false;

Your answer was exactly truth. thank u very much. you corrected my mistake perfectly. that is the answer.
thank u code:

char * MyFriend ="vmanes";
int i=0;
cout<<MyFriend<<" is number "<<i+1<<endl;

can you correct mistakes of this code? no, it hasn't any mistake! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.