Problem of sorting words of each string using pointers

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

Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Problem of sorting words of each string using pointers

 
0
  #1
Feb 13th, 2006
Hello everyone,
I was having this problem of sorting words of a string entered by user where the user enters many strings and words of each string are sorted separately. This had to be done using pointers . Heres my attempt.I dont know where I am going wrong as garbage is being stored in some of the char pointers.
Please help.
Thanks,
comwizz
  1. //Program to read a set of lines and sort each word in the line
  2. #include<iostream.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. void main()
  6. {
  7. int i=0,j=0;
  8. char *temp,*p;
  9. int w_len,l_no,q;
  10. cout<<"Enter the number of lines you want to enter ";
  11. cin>>l_no;
  12. char ***a;
  13. a=new char**[l_no];
  14. for(j=0;j<l_no;i++)
  15. {
  16. cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  17. cin>>w_len;
  18. *(a+j)=new char*[w_len];
  19. cout<<"Enter the line"<<endl;
  20. cin.getline(p,150,-1);
  21. char *y;
  22. p++;
  23. y++;
  24. for(i=0;i<w_len;i++)
  25. {
  26. int count=0;
  27. y=p;
  28. while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
  29. {
  30. count++;p++;
  31. }
  32. p++;
  33. *(*(a+j)+i)=new char[count];
  34. strncpy(*(*(a+j)+i),y,count);
  35. }
  36. for(i=0;i<w_len;i++)
  37. {
  38. for(int k=0;k<w_len-i-1;j++)
  39. {
  40. if((strcmp(*(*(a+j)+i),*(*(a+j)+i+1)))>0)
  41. {
  42. temp=*(*(a+j)+i);
  43. *(*(a+j)+i)=*(*(a+j)+i+1);
  44. *(*(a+j)+i+1)=temp;
  45. }
  46. }
  47. }
  48. }
  49. for(j=0;j<l_no;j++)
  50. {
  51. cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  52. for(i=0;i<w_len;i++)
  53. {
  54. cout<<*(*(a+j)+i)<<endl;
  55. }
  56. }
  57. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,334
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem of sorting words of each string using pointers

 
0
  #2
Feb 13th, 2006
your program is littered with uninitialized pointers! set all pointers to 0 when declared and it will make your debugging a lot easier. Replace all those pointer arithmetic with normal array indexing. Make sure the program allocates memory before using it.

// replace this
 *(a+j)=new char*[w_len]; // exception attemtping to dereference uninitialized pointer
// with this
  a[j]=new char*[w_len];

// using uninitialized pointer and unallocated p
  cin.getline(p,150,-1);

and replace obsolete <iostream.h> with <iostream>

Unless you are required to use char pointers, you should consider replacing them with std::vector and std::string and eliminate all that messey memory allocations.
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
  #3
Feb 14th, 2006
hi comwizz ,
n hi 2 every1 else readin d reply . m a new member in the club.i dont get exactly wat u hve typed.... but i see in the foll code u has used j as a variable n incremented i... so look out..
/*
for(j=0;j<l_no;i++)
{
*/
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Problem of sorting words of each string using pointers

 
1
  #4
Feb 14th, 2006
Originally Posted by HackWizz
hi comwizz ,
n hi 2 every1 else readin d reply . m a new member in the club.i dont get exactly wat u hve typed.... but i see in the foll code u has used j as a variable n incremented i... so look out..
/*
for(j=0;j<l_no;i++)
{
*/
A good way to avoid these sorts of bugs is to only declare the loop variable at the point you actually need to use it.
As far as I can see, the program does not need j anywhere outside the scope of the for loops in which it is used.

one remedy could be
  1. for (int j=0; j<l_no; j++) {}
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Problem of sorting words of each string using pointers

 
0
  #5
Feb 16th, 2006
Heres my corrected code. Still , I am having problems , that when two lines are entered by the user , after sorting the words of the first line , the word length of second line as well as the contents of the second line are not taken. Why is this happennin'?? Also I have to do the entire exercise using pointers and not use vectors or arrays. any inputs are welcome.
Thanks,
comwizz
Heres the code.
  1. //Program to read a set of lines and sort each word in the line
  2. #include<iostream.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. void main()
  6. {
  7. int i=0,j=0;
  8. char *temp=0,*p=0;
  9. int w_len[10],l_no,q;
  10. cout<<"Enter the number of lines you want to enter ";
  11. cin>>l_no;
  12. char ***a;
  13. a=new char**[l_no];
  14. for(j=0;j<l_no;j++)
  15. {
  16. cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  17. cin>>w_len[j];
  18. *(a+j)=new char*[w_len[j]];
  19. cout<<"Enter the line"<<endl;
  20. p=new char[40];
  21. cin.getline(p,150,-1);
  22. char *y;
  23. p++;
  24. y++;
  25. for(i=0;i<w_len[j];i++)
  26. {
  27. int count=0;
  28. y=p;
  29. while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
  30. {
  31. count++;p++;
  32. }
  33. p++;
  34. *(*(a+j)+i)=new char[count];
  35. strncpy(*(*(a+j)+i),y,count);
  36. a[j][i][count]='\0';
  37. }
  38. for(i=0;i<w_len[j];i++)
  39. {
  40. for(int k=0;k<w_len[j]-i-1;k++)
  41. {
  42. if((strcmp(*(*(a+k)+i),*(*(a+k)+i+1)))>0)
  43. {
  44. temp=*(*(a+j)+i);
  45. *(*(a+j)+i)=*(*(a+j)+i+1);
  46. *(*(a+j)+i+1)=temp;
  47. }
  48. }
  49. }
  50. }
  51. for(j=0;j<l_no;j++)
  52. {
  53. cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  54. for(i=0;i<w_len[j];i++)
  55. {
  56. cout<<*(*(a+j)+i)<<endl;
  57. }
  58. }
  59. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Problem of sorting words of each string using pointers

 
0
  #6
Feb 17th, 2006
I would need help. It would be great if you could help me.
Thanks,
comwizz
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
  #7
Feb 17th, 2006
hi comWizz,
look for red n thnk abt it...n if u r in the pc rite now do send back n mail or reply back here

/*
//Program to read a set of lines and sort each word in the line
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0;
char *temp=0,*p=0;
int w_len[10],l_no,q;
cout<<"Enter the number of lines you want to enter ";
cin>>l_no;
char ***a;
a=new char**[l_no];
for(j=0;j<l_no;j++)
{
cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
cin>>w_len[j];
*(a+j)=new char*[w_len[j]];
cout<<"Enter the line"<<endl;
p=new char[40];
cin.getline(p,150,-1);
char *y;
p++;
///y++;///
without ne intializiation or assignment increment thnk abt it

for(i=0;i<w_len[j];i++)
{
int count=0;
y=p;
while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
{
count++;p++;
}
p++;
*(*(a+j)+i)=new char[count];
strncpy(*(*(a+j)+i),y,count);
a[j][i][count]='\0';
}
for(i=0;i<w_len[j];i++)
{
for(int k=0;k<w_len[j]-i-1;k++)
{
if((strcmp(*(*(a+k)+i),*(*(a+k)+i+1)))>0)
{
/// temp=*(*(a+j)+i);
*(*(a+j)+i)=*(*(a+j)+i+1);
*(*(a+j)+i+1)=temp;
////



comparin *(*(a+k)+i),*(*(a+k)+i+1 n
swappin *(*(a+j)+i),*(*(a+j)+i+1);


}
}
}
}
for(j=0;j<l_no;j++)
{
cout<<"The correct order for the line"<<j+1<<"is"<<endl;
for(i=0;i<w_len[j];i++)
{
cout<<*(*(a+j)+i)<<endl;
}
}
}


[/code][/QUOTE]

i ll just run it n reply back.. i typed a pointer rong so need to rectify n then i ll reply...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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 iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem of sorting words of each string using pointers

 
0
  #8
Feb 17th, 2006
Originally Posted by comwizz
Heres my corrected code. Still , I am having problems , that when two lines are entered by the user , after sorting the words of the first line , the word length of second line as well as the contents of the second line are not taken. Why is this happennin'?? Also I have to do the entire exercise using pointers and not use vectors or arrays. any inputs are welcome.
Thanks,
comwizz
Heres the code.
  1. //Program to read a set of lines and sort each word in the line
  2. #include<iostream.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. void main()
  6. {
  7. int i=0,j=0;
  8. char *temp=0,*p=0;
  9. int w_len[10],l_no,q;
  10. cout<<"Enter the number of lines you want to enter ";
  11. cin>>l_no;
  12. char ***a;
  13. a=new char**[l_no];
  14. for(j=0;j<l_no;j++)
  15. {
  16. cout<<"Enter the number of words in the line"<<i+1<<":"<<endl;
  17. cin>>w_len[j];
  18. *(a+j)=new char*[w_len[j]];
  19. cout<<"Enter the line"<<endl;
  20. p=new char[40];
  21. cin.getline(p,150,-1);
  22. char *y;
  23. p++;
  24. y++;
  25. for(i=0;i<w_len[j];i++)
  26. {
  27. int count=0;
  28. y=p;
  29. while((*p!=' ')&&(*p!='.')&&(*p!='\0'))
  30. {
  31. count++;p++;
  32. }
  33. p++;
  34. *(*(a+j)+i)=new char[count];
  35. strncpy(*(*(a+j)+i),y,count);
  36. a[j][i][count]='\0';
  37. }
  38. for(i=0;i<w_len[j];i++)
  39. {
  40. for(int k=0;k<w_len[j]-i-1;k++)
  41. {
  42. if((strcmp(*(*(a+k)+i),*(*(a+k)+i+1)))>0)
  43. {
  44. temp=*(*(a+j)+i);
  45. *(*(a+j)+i)=*(*(a+j)+i+1);
  46. *(*(a+j)+i+1)=temp;
  47. }
  48. }
  49. }
  50. }
  51. for(j=0;j<l_no;j++)
  52. {
  53. cout<<"The correct order for the line"<<j+1<<"is"<<endl;
  54. for(i=0;i<w_len[j];i++)
  55. {
  56. cout<<*(*(a+j)+i)<<endl;
  57. }
  58. }
  59. }
Sometimes it baffles me as to what they teach people nowadays. Which scares me, because in a few years I hope to do a degree in computer science.

Your teacher is asking you to use the c++ language to do an assignment with just one caveat - you can't actually use a single function associated with c++ to help make things easier. :rolleyes: Does that not sound odd to you?

//Program to read a set of lines
//and sort each word in the line
#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
int lines;
cout<<"How many lines do you want to enter?"<<endl;
cin>>lines;
cin.ignore();

cout<<"So type them in then retard...\n\n"<<endl;

string burp;
string pedantic;
vector <string> iamthwee;
vector <string> array;
vector <string> pfft;

for(int i=0; i<lines; i++)
{
cout<<i+1<<":";
getline(cin, burp, '\n');

iamthwee.push_back(burp);


while (burp.find(" ", 0) != string::npos)
{
size_t pos = burp.find(" ", 0);
pedantic = burp.substr(0, pos);
burp.erase(0, pos + 1);
array.push_back(pedantic);
}

array.push_back(burp);
sort(array.begin(),array.end());


string crapola="";
for(int j=0; j<array.size(); j++)
{
crapola=crapola+array[j]+" ";
}
pfft.push_back(crapola);
cout<<"\n";

array.erase(array.begin(),array.end());
}
cout<<"Those lines in alphabetical order...."<<endl;
for(int jkl=0; jkl<pfft.size(); jkl++)
{
cout<<jkl+1<<":"<<pfft[jkl]<<endl;
}

cin.get();

}
*Voted best profile in the world*
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
  #9
Feb 18th, 2006
Your teacher is asking you to use the c++ language to do an assignment with just one caveat - you can't actually use a single function associated with c++ to help make things easier. :rolleyes: Does that not sound odd to you?
Actually comwizz has learnt c and is still learning c++... so in the assignment it(the program) requires the programmer to use basically c logics with cin and cout statments and include iostream header files.. without using all the string and vectors concepts...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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 iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem of sorting words of each string using pointers

 
0
  #10
Feb 18th, 2006
Originally Posted by HackWizz
Actually comwizz has learnt c and is still learning c++... so in the assignment it(the program) requires the programmer to use basically c logics with cin and cout statments and include iostream header files.. without using all the string and vectors concepts...
A pointless endeavour. Good luck, when you two actually graduate. :eek:
*Voted best profile in the world*
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