Problem of sorting words of each string using pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem of sorting words of each string using pointers

 
0
  #11
Feb 19th, 2006
>I dont know where I am going wrong as garbage is being stored in some of the char pointers.
You're overcomplicating the problem. Compare and contrast your solution with this one:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. void jsw_insertion ( char **a, int n )
  8. {
  9. for ( int i = 1; i < n; i++ ) {
  10. char *save = *(a + i);
  11. int j;
  12.  
  13. for ( j = i; j >= 1 && strcmp ( *(a + (j - 1)), save ) > 0; j-- )
  14. *(a + j) = *(a + (j - 1));
  15.  
  16. *(a + j) = save;
  17. }
  18. }
  19.  
  20. int main()
  21. {
  22. int lines;
  23.  
  24. cout<<"Number of lines: ";
  25. cin>> lines;
  26.  
  27. while ( --lines >= 0 ) {
  28. int words;
  29.  
  30. cout<<"Number of words: ";
  31. cin>> words;
  32.  
  33. char **line = new char*[words];
  34. char buffer[1024];
  35.  
  36. // Gather words in a line
  37. for ( int i = 0; i < words; i++ ) {
  38. cin>> setw ( sizeof buffer ) >> buffer;
  39. *(line + i) = new char[strlen ( buffer ) + 1];
  40. strcpy ( *(line + i), buffer );
  41. }
  42.  
  43. jsw_insertion ( line, words );
  44.  
  45. cout<<"The sorted line is: ";
  46. for ( int i = 0; i < words; i++ )
  47. cout<< *(line + i) <<' ';
  48. cout<<'\n';
  49.  
  50. // Tidy up
  51. while ( --words >= 0 )
  52. delete [] line[words];
  53. delete [] line;
  54. }
  55. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 54
Reputation: HackWizz is an unknown quantity at this point 
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: Problem of sorting words of each string using pointers

 
0
  #12
Feb 19th, 2006
Hi narue,
I shall try and understand the code given by you and run it.. I need to understand the "buffer" and hopefully I will..

What is "iomanip" included for ...? I suppose "cstring" is combined version of "string" and "ctype"...???

Thanks you were the first to reply with accordance to our need.. You seem to be helpful .... and so try and help jobe in " sol to ex. 4-6 in Acc. C++" as i made him write a long post with his/her code and it appeared out of my scope at this moment.. I will see the code and reply back..

thanxs again!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem of sorting words of each string using pointers

 
0
  #13
Feb 19th, 2006
> What is "iomanip" included for ...?
iomanip is included so that I could use the setw manipulator with cin's >> operator. Reading strings with cin's >> operator is dangerous because most people don't take precautions against buffer overflow. setw does that.

>I suppose "cstring" is combined version of "string" and "ctype"...???
cstring is the C++ equivalent of string.h.

>so try and help jobe in " sol to ex. 4-6 in Acc. C++"
I decided that not replying would be more instructive for JoBe. Be aware that I read every thread, so please don't direct me to questions on the assumption that I haven't seen them.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 54
Reputation: HackWizz is an unknown quantity at this point 
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: Problem of sorting words of each string using pointers

 
0
  #14
Feb 20th, 2006
I decided that not replying would be more instructive for JoBe. Be aware that I read every thread, so please don't direct me to questions on the assumption that I haven't seen them.
I wasn't directing you I was just requesting....!!!!!! I thought you were so helpful to me so I had expected you to be helpful to me in helping others because I couldn't!! Anyways Thats your wish how u look at things and understand them!!!

GOOD BYE!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 54
Reputation: HackWizz is an unknown quantity at this point 
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: Problem of sorting words of each string using pointers

 
0
  #15
Feb 21st, 2006
Hello narue,
I used the code and it ran correctly.But few questions..
1. Null Pointer Assignment warning gets printed on the screen when the pointers are not allocated memory but u have everywhere except in save pointer in the function jsw_insertion.. I allocated it the memory but still the warning appeared???

2.Also while u used the buffer in the input ...Well I don't exactly know iomanip so I will try seeing that ... But one thing in that while compiling the code step by step it (cin) was executed many times(specified) ... i.e. in the first time it took the line but the subsequent times it didn't... Also in the code of comwizz if u c and run the program once after the input through cin next time the it is executed without input from the user..why so???

3. Also the question was to read the set of lines together and then print the sorted version together..

The code given by you was great.. answering our question almost and very small indeed. it was very simple for a a new programmer to understand!! GREAT!!
Thanxs a lot!!!
HackWizz
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem of sorting words of each string using pointers

 
0
  #16
Feb 21st, 2006
>Also the question was to read the set of lines together and
>then print the sorted version together..
You neglected to mention that requirement.

>2 <snip>
I didn't understand a word of that. Give me an example of what the program is doing and what you think it should be doing.

>but u have everywhere except in save pointer in the function jsw_insertion
Correct. jsw_insertion works with pointers to existing memory, so there's no need to allocate anything new. All it does is move around addresses.

>I allocated it the memory but still the warning appeared???
Can you be more specific?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 54
Reputation: HackWizz is an unknown quantity at this point 
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: Problem of sorting words of each string using pointers

 
0
  #17
Feb 21st, 2006
Yes,I am sorry I forgot to mention that..!

For the warning ... After the program runs completely on the output screen I have "Null Pointer Assignment" written...
That was what I was asking.. Is that due to unallocated pointer??

While I used F8 to understand the program ... the line cin while taking the input to gather the words in the first F8 it takes input from the user and then pressing F8 repeatedly while highlighting the line it does not take the input and moves on...

Even in comwizz's code while inputing the no of words using the array after the first line is inputed next time instead of taking the input from the user it takes some garbage like
-23568... so is it some bug in cin or an error in the code??
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC