I need help with array problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayman723
    New Member
    • Sep 2006
    • 40

    I need help with array problem.

    hi,
    I'm trying to write a program in wich I would generate 20 sentences out of
    4 arrays(articles ,nouns,preposit ion,verb).I have to select an item at random
    from each array and concatetane each one of those selections to form a sentence,
    I have to form 20 sentences. I have no compiling errors but when I execute
    the program is giving an execption. This is the code I have


    // program uses randon number generation to create sentences

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>

    #include<ctype. h>
    #include<string .h>

    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);


    int main()
    {

    srand(time(NULL ));

    char *array[row][column] = {" "};
    int position= 0,
    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 = (1 + rand() % 5);
    strcpy(array[element][column],string[position]);
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    for a start the calculation
    position = (1 + rand() % 5);
    gives position values in the range 1 to 5 which could cause memory access exceptions when accessing the noun array etc (array index is in the range 0 to length-1)

    should it be 1 to 4?
    position = (1 + rand() % 4);

    or 0 to 5?
    position = rand() % 5;

    Comment

    • ayman723
      New Member
      • Sep 2006
      • 40

      #3
      hi:

      thanks for answering my request, but I have corrected the position to be

      position = (1+ rand() % 4 );

      and it doesn't give me anything.

      any help will be greatly appreciated

      ayman

      Comment

      • Hanns
        New Member
        • Nov 2006
        • 1

        #4
        Hi

        I think one of the problems is the line

        strcpy(array[element][column],string[position]);

        In function concatenate. array is an array of pointers to char an the pointers are NULL, therefore you get access violations.

        Try something like array[element][column] = string[position] instead. I think there also problems with the array indices.

        Regards
        Hanns

        Comment

        • ayman723
          New Member
          • Sep 2006
          • 40

          #5
          hi;

          I have fixed the program as far as I can, and still can't make it work. anyone can see where the problem in this code. pleassse

          code:

          // 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];

          }

          Comment

          Working...