a program using four arrays of pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nkhosinathie
    New Member
    • May 2007
    • 91

    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,ve rb and preposition.the program should create a sentence by selecting a word at random from each array in the following order:article,n oun,verb preposition,,ar ticle 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
  • kreagan
    New Member
    • Aug 2007
    • 153

    #2
    Originally posted by Nkhosinathie
    hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,ve rb and preposition.the program should create a sentence by selecting a word at random from each array in the following order:article,n oun,verb preposition,,ar ticle 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

    Comment

    • Nkhosinathie
      New Member
      • May 2007
      • 91

      #3
      Originally posted by kreagan
      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.

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by Nkhosinathie
        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

        Comment

        • Nkhosinathie
          New Member
          • May 2007
          • 91

          #5
          the modular arrays of pointers program

          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.

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by Nkhosinathie
            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

            Comment

            • Nkhosinathie
              New Member
              • May 2007
              • 91

              #7
              Originally posted by gpraghuram
              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

              [code=cpp]
              #include<iostre am>
              #include<iomani p>
              #include<cstdli b>
              #include<ctime>
              using namespace std;
              #include<string >
              int main()

              {

              char arrayNoun[50]="boy,girl,dog, town,car";

              char*arraynounP tr=arrayNoun;



              char arrayArticle[50]="the,a,some,an y";
              char* arrayarticlePtr =arrayArticle;

              char arrayVerb[50]="drove,jumped, ran,walked,skip ped";
              char*arrayverbP tr=arrayVerb;


              char arrayPrepositio n[50]="to,from,over, under,on";
              char*arrayprepo sitionPtr=array Preposition;


              srand(time(0));


              cout<<arrayArti cle[a];
              cout<<arrayNoun[a];
              cout<<arrayVerb[a];
              cout<<arrayPrep osition[a];
              cout<<arrayArti cle[a];
              cout<<arrayNoun[a];
              cout<<endl;


              return 0;
              }[/code]
              Last edited by JosAH; Sep 19 '07, 10:21 AM. Reason: fixed the tags

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #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:

                [code=cpp]
                char arrayNoun[50]="boy,girl,dog, town,car";
                [/code]

                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

                Comment

                • Nkhosinathie
                  New Member
                  • May 2007
                  • 91

                  #9
                  Originally posted by JosAH
                  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:

                  [code=cpp]
                  char arrayNoun[50]="boy,girl,dog, town,car";
                  [/code]

                  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?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Nkhosinathie
                    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,w 3 ...,wn". Read it again.

                    kind regards,

                    Jos

                    Comment

                    • Nkhosinathie
                      New Member
                      • May 2007
                      • 91

                      #11
                      Originally posted by JosAH
                      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,w 3 ...,wn". Read it again.

                      kind regards,

                      Jos
                      please check the following code.[code=cpp]
                      // program uses randon number generation to create sentences
                      #include <iostream>
                      #include <stdlib.h>
                      #include <time.h>
                      #include<ctype. h>
                      #include<cstrin g>
                      const int row= 20;

                      const int column= 80;
                      void touppercase(cha r *string);
                      void concatenate(cha r *array[][column],int element,char *string[],int size);

                      using std::cout;
                      using std::endl;

                      int main()
                      {

                      srand(time(NULL ));

                      char *array[row][column] = {" "};
                      int position= 0;
                      int counter= 1;
                      char *article[5]= {"the","a","one ","some","any"} ;
                      char *noun[5]= {"boy","girl"," dog","town","ca r"};
                      char *verb[5]= {"drove","jumpe d","ran","walke d","skipped" };
                      char *preposition[5]= {"to","from","o ver","under","o n"};

                      do
                      {


                      if(counter == 20)
                      touppercase(art icle[position]);

                      concatenate(arr ay,counter,arti cle,row);

                      concatenate(arr ay,counter,noun ,row);

                      concatenate(arr ay,counter,verb ,row);


                      concatenate(arr ay,counter,prep osition,row);


                      }while(counter < 21);
                      for(int i =0;i<20;i++)
                      for(int j =0;j<20;j++)
                      cout<<array[i][j]<<endl;

                      return 0;
                      }

                      void touppercase(cha r *string)
                      {
                      int i =0;
                      while((*string != '\0') && (i != 1))
                      {
                      *string = toupper(*string );
                      ++i;
                      }
                      }
                      void concatenate(cha r *array[][column],int element, char *string[],int size)
                      {
                      int position = 0;
                      position = rand() % 5;
                      array[element][column] = string[position];

                      }[/code]
                      Last edited by sicarie; Sep 19 '07, 01:02 PM. Reason: Code tags

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        So now you've changed the implementation from this:

                        [code=c]
                        char arrayNoun[50]="boy,girl,dog, town,car";
                        [/code]

                        to this:

                        [code=c]
                        char *noun[5]= {"boy","girl"," dog","town","ca r"};
                        [/code]

                        That renders my remarks, based on your previous implementation, completely useless.

                        kind regards,

                        Jos

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          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.

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            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 &#91;CODE=cp p] and [/CODE].

                            Thanks

                            Comment

                            • Nkhosinathie
                              New Member
                              • May 2007
                              • 91

                              #15
                              Originally posted by pbmods
                              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.

                              Comment

                              Working...