943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3944
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 1st, 2006
0

help please

Expand Post »
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:

#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;
 
 }
any ideas on what is wrong? and what solution is there??

thank you
Last edited by ~s.o.s~; Dec 2nd, 2006 at 2:43 pm. Reason: Added code tags learn to use them yourself.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1e9v85 is offline Offline
11 posts
since Nov 2006
Dec 1st, 2006
0

Re: help please

Please use code tags. Find more information about them here:
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)
I don't quite understand what you're doing here. In the first iteration of the loop, 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)
  1. {
  2. Array[next] = Array[next];
Um, ok, so then why bother putting this line in at all?
C++ Syntax (Toggle Plain Text)
  1. }
  2. if( strncmp(Array[next],Array[next-1],3) == 1)
  3. {
  4. Array[next] = Array[next - 1];
  5. }
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 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.
If this seems redundant, it is. The bubble sort is one of the slowest sorting algorithms, although you probably don't need to worry about speed right now, as the list is relatively small.

Hope this helps
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 3rd, 2006
0

Re: help please

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?
C++ Syntax (Toggle Plain Text)
  1. cout << "The sorted arangement of the town names is: \n"<<endl;
  2. for ( int next = 1; next < ArraySize; next++ )
  3. {
  4. if ( strncmp(Array[(next-1)],Array[next],3) == -1)
  5. {
  6. Array[next] = Array[next];
  7. }
  8. if( strncmp(Array[next-1],Array[next],3) == 1)
  9. {
  10. cout << "initially next-1 is: " << Array[next-1] <<endl;
  11. const char* temp[ArraySize];
  12. temp[next] = Array[next];
  13.  
  14. Array[next-1] = temp [next];
  15. cout << "now it's: " << Array[next-1]<< endl;
  16. }
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Dec 3rd, 2006
0

Re: help please

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!
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Dec 3rd, 2006
0

Re: help please

please don't post your questions in threads created by others, create a thread of your own.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 3rd, 2006
0

Re: help please

but still that program does not alphabetize the names!

all it does is compares two consequtive names.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1e9v85 is offline Offline
11 posts
since Nov 2006
Dec 3rd, 2006
0

Re: help please

Click to Expand / Collapse  Quote originally posted by d1e9v85 ...
but still that program does not alphabetize the names!

all it does is compares two consequtive names.
Hints:

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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 3rd, 2006
1

Re: help please

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.
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Dec 3rd, 2006
0

Re: help please

Click to Expand / Collapse  Quote originally posted by JRM ...
All VERY good hints!
Thank you. And no you only need one if statement, if you are using a nested for loop like I suggested.
Last edited by iamthwee; Dec 3rd, 2006 at 12:13 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 3rd, 2006
0

Re: help please

thankx alot guys

got it to work

used two for loops and one if statement

works fine

thx alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d1e9v85 is offline Offline
11 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: wanna get started with 3D world :)
Next Thread in C++ Forum Timeline: Visual C++.2003 ComboBox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC