help please

Reply

Join Date: Nov 2006
Posts: 11
Reputation: d1e9v85 is an unknown quantity at this point 
Solved Threads: 0
d1e9v85 d1e9v85 is offline Offline
Newbie Poster

help please

 
0
  #1
Dec 1st, 2006
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 1:43 pm. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,052
Reputation: John A has much to be proud of John A has much to be proud of John A has much to be proud of John A has much to be proud of John A has much to be proud of John A has much to be proud of John A has much to be proud of John A has much to be proud of 
Solved Threads: 334
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help please

 
0
  #2
Dec 1st, 2006
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.
  1. {
  2. Array[next] = Array[next];
Um, ok, so then why bother putting this line in at all?
  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
"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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 433
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 48
JRM's Avatar
JRM JRM is offline Offline
Posting Pro in Training

Re: help please

 
0
  #3
Dec 3rd, 2006
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?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 433
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 48
JRM's Avatar
JRM JRM is offline Offline
Posting Pro in Training

Re: help please

 
0
  #4
Dec 3rd, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,464
Reputation: jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all jwenting is a name known to all 
Solved Threads: 233
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: help please

 
0
  #5
Dec 3rd, 2006
please don't post your questions in threads created by others, create a thread of your own.
42
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: d1e9v85 is an unknown quantity at this point 
Solved Threads: 0
d1e9v85 d1e9v85 is offline Offline
Newbie Poster

Re: help please

 
0
  #6
Dec 3rd, 2006
but still that program does not alphabetize the names!

all it does is compares two consequtive names.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help please

 
0
  #7
Dec 3rd, 2006
Originally Posted by d1e9v85 View Post
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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 433
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 48
JRM's Avatar
JRM JRM is offline Offline
Posting Pro in Training

Re: help please

 
1
  #8
Dec 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help please

 
0
  #9
Dec 3rd, 2006
Originally Posted by JRM View Post
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 11:13 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: d1e9v85 is an unknown quantity at this point 
Solved Threads: 0
d1e9v85 d1e9v85 is offline Offline
Newbie Poster

Re: help please

 
0
  #10
Dec 3rd, 2006
thankx alot guys

got it to work

used two for loops and one if statement

works fine

thx alot
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 3658 | Replies: 24
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC