| | |
Problem of sorting words of each string using pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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:
You're overcomplicating the problem. Compare and contrast your solution with this one:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <cstring> using namespace std; void jsw_insertion ( char **a, int n ) { for ( int i = 1; i < n; i++ ) { char *save = *(a + i); int j; for ( j = i; j >= 1 && strcmp ( *(a + (j - 1)), save ) > 0; j-- ) *(a + j) = *(a + (j - 1)); *(a + j) = save; } } int main() { int lines; cout<<"Number of lines: "; cin>> lines; while ( --lines >= 0 ) { int words; cout<<"Number of words: "; cin>> words; char **line = new char*[words]; char buffer[1024]; // Gather words in a line for ( int i = 0; i < words; i++ ) { cin>> setw ( sizeof buffer ) >> buffer; *(line + i) = new char[strlen ( buffer ) + 1]; strcpy ( *(line + i), buffer ); } jsw_insertion ( line, words ); cout<<"The sorted line is: "; for ( int i = 0; i < words; i++ ) cout<< *(line + i) <<' '; cout<<'\n'; // Tidy up while ( --words >= 0 ) delete [] line[words]; delete [] line; } }
I'm here to prove you wrong.
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
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!!!
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!!!
> 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.
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.
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
•
•
•
•
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.
GOOD BYE!!!
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
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
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
>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?
>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.
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
Solved Threads: 2
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??
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??
![]() |
Other Threads in the C++ Forum
- Previous Thread: file size
- Next Thread: KEy board event trapping and manipulation
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






