| | |
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:
Solved Threads: 0
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:
what is my mistake? I can't Understand.
please help me!
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:
C++ Syntax (Toggle Plain Text)
#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!
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.
What you really have is three arrays of char, not strings, so strcpy is not really working right.
C++ Syntax (Toggle Plain Text)
char student[3][4]; // for(i=0;i<number;i++) 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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jan 2009
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
Your array is not wide enough to hold the names. To hold names with three characters ( "ali" ) you need to have 4 columns.
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 //*
C++ Syntax (Toggle Plain Text)
#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++; } }
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.
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
c++ Syntax (Toggle Plain Text)
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[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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jan 2009
Posts: 12
Reputation:
Solved Threads: 0
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
when showing student there is nothing to display on screen.
please help me!
here is new bubble sort:
student array
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:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
#define number 20 #define length 31 char student[number][length];
Last edited by mahdiahmadirad; Jan 27th, 2009 at 4:46 am.
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....
C++ Syntax (Toggle Plain Text)
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;
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jan 2009
Posts: 12
Reputation:
Solved Threads: 0
Your answer was exactly truth. thank u very much. you corrected my mistake perfectly. that is the answer.
thank u code:
can you correct mistakes of this code? no, it hasn't any mistake!
thank u code:
C++ Syntax (Toggle Plain Text)
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!
Last edited by mahdiahmadirad; Jan 27th, 2009 at 3:51 pm.
![]() |
Similar Threads
- Bubble sorting and finding a location of a number in array (C++)
- Inventory Program Help (Java)
- Help with Sorting and displaying with bubble sort (C++)
- ASM 2-D Bubble Sort!! (Assembly)
- Multidimensional array sort problem (C++)
- bubble sort function w/ array (C++)
- Link List Sorting Problem (C++)
- bubble sorting in an array (C)
Other Threads in the C++ Forum
- Previous Thread: declaration of array in header file
- Next Thread: Ad-hoc Polymorphism & Messaging Mechanism | need explanation
Views: 1157 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






