473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

a program using four arrays of pointers

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
16 5763
kreagan
153 100+
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
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
153 100+
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
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
1,275 Expert 1GB
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
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
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
5,821 Expert 4TB
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
4,677 Expert Mod 4TB
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
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
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
4,677 Expert Mod 4TB
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

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

Similar topics

1
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
26
by: mwt | last post by:
Hello. Today I wrote my first program in C. It adds up the elements in an array. I am just beginning to learn this language. Any tips or pointers about better ways to write/structure/format/etc....
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
34
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
23
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
334
by: Antoninus Twink | last post by:
The function below is from Richard HeathField's fgetline program. For some reason, it makes three passes through the string (a strlen(), a strcpy() then another pass to change dots) when two would...
6
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me...
4
by: wutang | last post by:
Create a program that displays the sum of the sales amounts made in each of four regions (North, South, East, West) during a three month period. The program should display the total sales made during...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.