By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

a program using four arrays of pointers

P: 91
hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,verb and preposition.the program should create a sentence by selecting a word at random from each array in the following order:article,noun,verb preposition,,article and noun.as each word is picked,it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. the words should be separated by spaces.when the final sentence is output,it should start with a capital letter and end with a period.the program should generate 20 such sentences.

what i have deduced from this statement is that ill be using 4 arrays of pointers.so may you help me to have an idea of where to start because i don't have an idea.thanks
Sep 18 '07 #1
Share this Question
Share on Google+
16 Replies


kreagan
100+
P: 153
hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,verb and preposition.the program should create a sentence by selecting a word at random from each array in the following order:article,noun,verb preposition,,article and noun.as each word is picked,it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. the words should be separated by spaces.when the final sentence is output,it should start with a capital letter and end with a period.the program should generate 20 such sentences.

what i have deduced from this statement is that ill be using 4 arrays of pointers.so may you help me to have an idea of where to start because i don't have an idea.thanks
Are you an extremely new to the language? Why are you stucked?

At any rate, I would suggest simplifying the problem down into sections:
1.) Generate/Initialize array of pointers
2.) Get 1 value from each array
3.) Genrate string from pieces of array
4.) Capitalize first character and place period at the end
5.) Store into array of size 20
6.) Print
7.) Repeat 19 more times.

Good luck
Sep 18 '07 #2

P: 91
Are you an extremely new to the language? Why are you stucked?

At any rate, I would suggest simplifying the problem down into sections:
1.) Generate/Initialize array of pointers
2.) Get 1 value from each array
3.) Genrate string from pieces of array
4.) Capitalize first character and place period at the end
5.) Store into array of size 20
6.) Print
7.) Repeat 19 more times.

Good luck
this is my first year programming in c++ but i now know the basics of it but my problem now is we haven't done arrays and pointers that's why i'm stucked.
Sep 18 '07 #3

kreagan
100+
P: 153
this is my first year programming in c++ but i now know the basics of it but my problem now is we haven't done arrays and pointers that's why i'm stucked.
Well, then I would suggest reading about arrays and pointers. Here's some good links.

Arrays and Pointers

More Arrays and pointers
Read section 2.2. The diagram shows an array and a pointer to an array.

To make the problem simplier, try combining just 4 words together by using pointers then printing them out. After you have done that, worry about creating the array of pointers, choosing the 4 words, and storing them.

Good luck
Kat
Sep 18 '07 #4

P: 91
i'm so sorry to repeat this statement,i thought maybe you guys u don't understand this program that i'm doing.
i'll post the code i have done for this program because this program is driving me crazy right now




Write a program that uses random number generation to create sentences. The program should use four arrays of pointers to char called article, noun, verb, and preposition. The program should create a sentence by selecting a word at random from each array in the following order: article noun verb preposition article noun. As each word is picked, it should be concatenated to the previous words in an array which is large enough to hold the entire sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 such sentences.

The arrays should be filled as shown below.

article noun verb preposition
the boy drove to
a girl jumped from
one dog ran over
some town walked under
any car skipped on

Turn in your source code listing and a print out of the output screen showing
the result. Your program must be modular and fully commented.
Sep 19 '07 #5

gpraghuram
Expert 100+
P: 1,275
i'm so sorry to repeat this statement,i thought maybe you guys u don't understand this program that i'm doing.
i'll post the code i have done for this program because this program is driving me crazy right now




Write a program that uses random number generation to create sentences. The program should use four arrays of pointers to char called article, noun, verb, and preposition. The program should create a sentence by selecting a word at random from each array in the following order: article noun verb preposition article noun. As each word is picked, it should be concatenated to the previous words in an array which is large enough to hold the entire sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 such sentences.

The arrays should be filled as shown below.

article noun verb preposition
the boy drove to
a girl jumped from
one dog ran over
some town walked under
any car skipped on

Turn in your source code listing and a print out of the output screen showing
the result. Your program must be modular and fully commented.

Why cant you post your code u have written and explain which part of the code u are facing issues?

Raghuram
Sep 19 '07 #6

P: 91
Why cant you post your code u have written and explain which part of the code u are facing issues?

Raghuram
this is the code i managed to do.my problem now is where can i place the random function inorder for the program to print different sentences

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cstdlib>
  4. #include<ctime>
  5. using namespace std;
  6. #include<string>
  7. int main()
  8.  
  9. {
  10.  
  11.     char arrayNoun[50]="boy,girl,dog,town,car";
  12.  
  13.     char*arraynounPtr=arrayNoun;
  14.  
  15.  
  16.  
  17.     char arrayArticle[50]="the,a,some,any";
  18.     char* arrayarticlePtr=arrayArticle;
  19.  
  20.     char arrayVerb[50]="drove,jumped,ran,walked,skipped";
  21.     char*arrayverbPtr=arrayVerb;
  22.  
  23.  
  24.     char arrayPreposition[50]="to,from,over,under,on";
  25.     char*arrayprepositionPtr=arrayPreposition;
  26.  
  27.  
  28.     srand(time(0));
  29.  
  30.  
  31.         cout<<arrayArticle[a];
  32.         cout<<arrayNoun[a];
  33.          cout<<arrayVerb[a];
  34.          cout<<arrayPreposition[a];
  35.          cout<<arrayArticle[a];
  36.          cout<<arrayNoun[a];
  37.          cout<<endl;
  38.  
  39.  
  40.         return 0;
  41. }
Sep 19 '07 #7

Expert 10K+
P: 11,448
So you have all your nouns in a single char array separated by commas. The
same applies to the verbs, articles etc. Let's take the noun array for example:

Expand|Select|Wrap|Line Numbers
  1. char arrayNoun[50]="boy,girl,dog,town,car";
  2.  
If there are 'n' commas in that single string there are 'n+1' nouns in that string.
So you need to generate a pseudo random number 'r' in the range [0, n] (inclusive).

Next you have to skip 'r' commas and copy the next noun upto the 'r+1'st
comma.

Check the available functions in the <cstring> library or the <string> library.
I'm sure you can make good use of some of them.

kind regards,

Jos
Sep 19 '07 #8

P: 91
So you have all your nouns in a single char array separated by commas. The
same applies to the verbs, articles etc. Let's take the noun array for example:

Expand|Select|Wrap|Line Numbers
  1. char arrayNoun[50]="boy,girl,dog,town,car";
  2.  
If there are 'n' commas in that single string there are 'n+1' nouns in that string.
So you need to generate a pseudo random number 'r' in the range [0, n] (inclusive).

Next you have to skip 'r' commas and copy the next noun upto the 'r+1'st
comma.

Check the available functions in the <cstring> library or the <string> library.
I'm sure you can make good use of some of them.

kind regards,

Jos
i'm really sorry i don't understand what u mean?
Sep 19 '07 #9

Expert 10K+
P: 11,448
i'm really sorry i don't understand what u mean?
That was a short outline of the steps you have to implement in order to produce
a (pseudo) random element from a list "w0,w1,w2,w3 ...,wn". Read it again.

kind regards,

Jos
Sep 19 '07 #10

P: 91
That was a short outline of the steps you have to implement in order to produce
a (pseudo) random element from a list "w0,w1,w2,w3 ...,wn". Read it again.

kind regards,

Jos
please check the following code.
Expand|Select|Wrap|Line Numbers
  1. // program uses randon number generation to create sentences
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include<ctype.h>
  6. #include<cstring>
  7. const int row= 20;
  8.  
  9. const int column= 80;
  10. void touppercase(char *string);
  11. void concatenate(char *array[][column],int element,char *string[],int size);
  12.  
  13. using std::cout;
  14. using std::endl;
  15.  
  16. int main()
  17. {
  18.  
  19. srand(time(NULL));
  20.  
  21. char *array[row][column] = {" "};
  22. int position= 0;
  23. int counter= 1;
  24. char *article[5]= {"the","a","one","some","any"};
  25. char *noun[5]= {"boy","girl","dog","town","car"};
  26. char *verb[5]= {"drove","jumped","ran","walked","skipped"};
  27. char *preposition[5]= {"to","from","over","under","on"};
  28.  
  29. do
  30. {
  31.  
  32.  
  33. if(counter == 20)
  34. touppercase(article[position]);
  35.  
  36. concatenate(array,counter,article,row);
  37.  
  38. concatenate(array,counter,noun,row);
  39.  
  40. concatenate(array,counter,verb,row);
  41.  
  42.  
  43. concatenate(array,counter,preposition,row);
  44.  
  45.  
  46. }while(counter < 21);
  47. for(int i =0;i<20;i++)
  48. for(int j =0;j<20;j++)
  49. cout<<array[i][j]<<endl;
  50.  
  51. return 0;
  52. }
  53.  
  54. void touppercase(char *string)
  55. {
  56. int i =0;
  57. while((*string != '\0') && (i != 1))
  58. {
  59. *string = toupper(*string);
  60. ++i;
  61. }
  62. }
  63. void concatenate(char *array[][column],int element, char *string[],int size)
  64. {
  65. int position = 0;
  66. position = rand() % 5;
  67. array[element][column] = string[position]; 
  68.  
  69. }
Sep 19 '07 #11

Expert 10K+
P: 11,448
So now you've changed the implementation from this:

Expand|Select|Wrap|Line Numbers
  1. char arrayNoun[50]="boy,girl,dog,town,car";
  2.  
to this:

Expand|Select|Wrap|Line Numbers
  1. char *noun[5]= {"boy","girl","dog","town","car"};
  2.  
That renders my remarks, based on your previous implementation, completely useless.

kind regards,

Jos
Sep 19 '07 #12

pbmods
Expert 5K+
P: 5,821
Heya, Nkhosinathie.

What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Sep 19 '07 #13

sicarie
Expert Mod 2.5K+
P: 4,677
Nkhosinathie-

Please do not double-post - try to keep all your questions on the same topic in one thread.

Also, you are a member, so you should be using code tags around your code. They are [CODE=cpp] and [/code].

Thanks
Sep 19 '07 #14

P: 91
Heya, Nkhosinathie.

What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
the program doesn't want to print anything. when i compile it,it shows just a space
thanks.
Sep 20 '07 #15

P: 91
Nkhosinathie-

Please do not double-post - try to keep all your questions on the same topic in one thread.

Also, you are a member, so you should be using code tags around your code. They are
Expand|Select|Wrap|Line Numbers
  1.  and 
.

Thanks
i apologise for that,and i'm still not sure how to wrap my code to code tags.
Sep 20 '07 #16

sicarie
Expert Mod 2.5K+
P: 4,677
i apologise for that,and i'm still not sure how to wrap my code to code tags.
If you look at my post above, you put the code in the space of the 'and' in between the two tags. Then if you look at your reply, you will see how the code is translated.
Sep 20 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.