| | |
Multidimensional array sort problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Well this is my problem, I wrote a function to sort a 2d char array but right now the second dimension bound cannot be variable so it has to be hardset (which I don't particularly like)
This is what I have
Output:
Any suggestions? (I'm also not completely sure if this is sorting correctly)
This is what I have
c++ Syntax (Toggle Plain Text)
void multi_charSort(char array[][5]) { int i,j; char temp2[5]; for(i=0;i<5;i++) { for(j=0;j<i;j++) { if((array[i][0]>='A' && array[i][0]<='Z') && (array[j][0]>='A' && array[j][0]<='Z')) { if(array[i]>array[j]) { for (int k=0;k<5;k++) { temp2[k]= array[i][k]; array[i][k]=array[j][k]; array[j][k]=temp2[k]; } } } else if((array[i][0]>='a' && array[i][0]<='z') && (array[j][0]>='a' && array[j][0]<='z')) { if(array[i]>array[j]) { for (int k=0;k<5;k++) { temp2[k]= array[i][k]; array[i][k]=array[j][k]; array[j][k]=temp2[k]; } } } } } }
Output:
C++ Syntax (Toggle Plain Text)
2d char Array initialized! Printing char Array: 1: Shaw 2: Tom 3: Bill 4: Adam Sorting charArray! 1: Adam 2: Bill 3: Tom 4: Shaw
Last edited by ShawnCplus; Jul 2nd, 2007 at 2:54 am.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
Sorry ShawnCplus, but it looks like your code won't sort properly. At some places, you are comparing pointers to the strings instead of the characters in the strings. Try using a strcmp(array[i], array[j]) instead of array[i]>array[j] in the sort part (as this is not comparing the strings, but merely the pointers).
Do you really need to use a multidimensional array? It seems a bit wastefull of space and really inflexible (as you said, the bound must be hardset) - try using an array of char*. This allows you to have any length strings, and makes sorting faster (the swap function just swaps the pointers, not the actual data in the strings).
The following code does the job. Note that I have used a NULL pointer to indicate the end of my array of char pointers.
Output:
Do you really need to use a multidimensional array? It seems a bit wastefull of space and really inflexible (as you said, the bound must be hardset) - try using an array of char*. This allows you to have any length strings, and makes sorting faster (the swap function just swaps the pointers, not the actual data in the strings).
The following code does the job. Note that I have used a NULL pointer to indicate the end of my array of char pointers.
c++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> #include <string.h> using namespace std; // bubble sort void sort(char *st[]){ for(int i = 0; st[i] != NULL; i++){ for(int j = i+1; st[j] != NULL; j++){ if(strcmp(st[i], st[j]) > 0){ char *temp = st[i]; // swap pointers st[i] = st[j]; st[j] = temp; } } } } int main(){ char *strings[] = {"Shawn", "Tom", "Bill", "Adam", NULL}; // using NULL to indicate last entry printf("Before sort:\n"); for(int i = 0; strings[i] != NULL; i++) printf("\t%d: %s\n", i+1, strings[i]); sort(strings); printf("After sort:\n"); for(int i = 0; strings[i] != NULL; i++) printf("\t%d: %s\n", i+1, strings[i]); getch(); return 0; }
Output:
C++ Syntax (Toggle Plain Text)
Before sort: 1: Shawn 2: Tom 3: Bill 4: Adam After sort: 1: Adam 2: Bill 3: Shawn 4: Tom
Last edited by dougy83; Jul 2nd, 2007 at 4:48 am.
Yeah, I fixed it. The line that was actually checking the alphabetical order wasn't comparing the characters. And switching to a char* array forces me to use strcmp and the sub-point of it was to sort the array without any extra functions. But using NULL was fairly ingenious *thumbs up*. It's not like it really matters it was just something to do while I was bored. (Bad things happen when I'm bored... mainly to my computer)
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
> It's not like it really matters it was just something to do while I was bored.
I guess this explains it all. ;-)
I guess this explains it all. ;-)
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
strcmp does not have to be an external function. If library functions a problem, then write your own, e.g. :
But really, the library functions are there to use - of course all of them can be written again for every program you ever write, but what's the point? And by writing your own you introduce more possibilities for errors.
c++ Syntax (Toggle Plain Text)
char strcmp_(char *s1, char *s2){ int i = -1; do{ i++; if(s1[i] > s2[i]) return 1; // s1 larger else if(s1[i] < s2[i]) return -1; // s2 larger }while(s1[i] && s2[i]); return 0; // equal }
But really, the library functions are there to use - of course all of them can be written again for every program you ever write, but what's the point? And by writing your own you introduce more possibilities for errors.
Last edited by dougy83; Jul 3rd, 2007 at 3:26 am.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
•
•
•
•
dougy, if either one is NULL, you'll dereference it at least once in the loop body. It's little mistakes like this that make those library functions so important...
•
•
•
•
And by writing your own you introduce more possibilities for errors.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: designing classes
- Next Thread: interesting syntax
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop developer 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 multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






