| | |
help please
![]() |
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 0
the program i have to make is as follows:
use the string comparison function and the sorting array techniques to write a program that alphabetizes a list of strings. use 10 town names for your program.
and this si what i have made.... and it doesnt work:
any ideas on what is wrong? and what solution is there??
thank you
use the string comparison function and the sorting array techniques to write a program that alphabetizes a list of strings. use 10 town names for your program.
and this si what i have made.... and it doesnt work:
#include<iostream> using namespace std; #include<iostream> using namespace std; #include<cstring> using std::strncmp; int main() { const int ArraySize = 10; const char *Array[] = { "Toronto", "Montreal", "Alberta", "Quebec", "NewYork", "Calgary", "Edmonton", "NewJersey", "Ontario", "California" }; cout << "The unsorted arangement of the town names is: \n"<<endl; for ( int i = 0; i < ArraySize; i++ ) { cout <<" " << Array[i]; cout << endl << "\n"; } cout << "The sorted arangement of the town names is: \n"<<endl; for ( int next = 0; next < ArraySize; next++ ) { if ( strncmp(Array[next],Array[next-1],3) == -1) { Array[next] = Array[next]; } if( strncmp(Array[next],Array[next-1],3) == 1) { Array[next] = Array[next - 1]; } } for ( int j = 0; j < ArraySize; j++ ) { cout <<" " << Array[j]; cout << endl << "\n"; } return 0; }
thank you
Last edited by ~s.o.s~; Dec 2nd, 2006 at 1:43 pm. Reason: Added code tags learn to use them yourself.
Please use code tags. Find more information about them here:
http://www.daniweb.com/techtalkforum...cement8-3.html
Your sorting algorithm is fundamently flawed:
I don't quite understand what you're doing here. In the first iteration of the loop,
Um, ok, so then why bother putting this line in at all?
And if you're going to swap variables, you are going to need to have an "in-between" variable to hold the value of 1 while the other is being swapped. You're just loosing the value contained in
And even fixing that, it still will not work, as you aren't implementing the Bubble sort algorithm correctly (which is what it seems you're trying to do). This is what the Bubble sort does:
Hope this helps
http://www.daniweb.com/techtalkforum...cement8-3.html
Your sorting algorithm is fundamently flawed:
for ( int next = 0; next < ArraySize; next++ )
{
if ( strncmp(Array[next],Array[next-1],3) == -1)next will be equal to 0, so the second parameter you'll be passing to strncmp() will be... Array[-1]. That won't work. I think that you might want next to be initialized at 1. C++ Syntax (Toggle Plain Text)
{ Array[next] = Array[next];
C++ Syntax (Toggle Plain Text)
} if( strncmp(Array[next],Array[next-1],3) == 1) { Array[next] = Array[next - 1]; }
Array[next] with that statement right there.And even fixing that, it still will not work, as you aren't implementing the Bubble sort algorithm correctly (which is what it seems you're trying to do). This is what the Bubble sort does:
- Compares the first 2 values. If the second is smaller than the first, it swaps them.
- Keeps comparing and swapping through the whole list.
- Starts over, doing the whole process again.
- Depending on how large your list is, you may have to do this many times to get the entire list sorted.
Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
As a noob myself, I play with these examples as a learning experience, and if i can, help others .
I changed the the inital value of next to 1 a reccomended, AND switched the order of the arrays in the strncomp(). The first "if " is a "do nothing and probably can be deleted?
Anyway, the biggest revelation was when I ran it through the debugger to find that NIETHER condition for swap was ever returned by strncomp()!
Now THAT seems strange!
I added some cout's to see what variable was doing what, but never got any output!
Could we have some Guru input on this, please?
I changed the the inital value of next to 1 a reccomended, AND switched the order of the arrays in the strncomp(). The first "if " is a "do nothing and probably can be deleted?
Anyway, the biggest revelation was when I ran it through the debugger to find that NIETHER condition for swap was ever returned by strncomp()!
Now THAT seems strange!
I added some cout's to see what variable was doing what, but never got any output!
Could we have some Guru input on this, please?
C++ Syntax (Toggle Plain Text)
cout << "The sorted arangement of the town names is: \n"<<endl; for ( int next = 1; next < ArraySize; next++ ) { if ( strncmp(Array[(next-1)],Array[next],3) == -1) { Array[next] = Array[next]; } if( strncmp(Array[next-1],Array[next],3) == 1) { cout << "initially next-1 is: " << Array[next-1] <<endl; const char* temp[ArraySize]; temp[next] = Array[next]; Array[next-1] = temp [next]; cout << "now it's: " << Array[next-1]<< endl; }
Just had a EURIKA moment after looking up the the spec for strncmp(). It says that it will return an integer >,<, or =0, depending on the string relationship.
It DOES NOT say WHAT that integer is!
Changing the the boolean relationship to >0 rather than == 1 made the function come to life!
It's amazing what one can learn from just screwing around!
It DOES NOT say WHAT that integer is!
Changing the the boolean relationship to >0 rather than == 1 made the function come to life!
It's amazing what one can learn from just screwing around!
•
•
•
•
but still that program does not alphabetize the names!
all it does is compares two consequtive names.
1. Use a nested for loop. I.e a for loop within itself
2. Find out what strcmp returns.
3. How do you swap elements of the array. Do you need to create a temporary variable?
*Voted best profile in the world*
All VERY good hints! I had it alphabetizing soon after I got strncmp() working. You have to fix BOTH "if" statements and their attendant functions. Also think about what the "next" loop is actually doing.
There is a simple statement to make it "do more".
To my surprise, two "if" statements were needed. Each checking a different "direction". Once you get it to work, try commenting one set out and see what happens. I thought it was educational..
If I tell you the answer, then you will NEVER get a feel for this stuff! Learn by deductive reasoning-it's the only way.
There is a simple statement to make it "do more".
To my surprise, two "if" statements were needed. Each checking a different "direction". Once you get it to work, try commenting one set out and see what happens. I thought it was educational..
If I tell you the answer, then you will NEVER get a feel for this stuff! Learn by deductive reasoning-it's the only way.
![]() |
Other Threads in the C++ Forum
- Previous Thread: wanna get started with 3D world :)
- Next Thread: Visual C++.2003 ComboBox
Views: 3658 | Replies: 24
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework http iamthwee input int lazy linker list loop loops map math matrix member memory multidimensional network newbie number object objects opengl output parameter pointer pointers problem program programming project qt random read reading recursion recursive reference server sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio win32 window windows winsock






