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

I need help with array problem.

P: 40
hi,
I'm trying to write a program in wich I would generate 20 sentences out of
4 arrays(articles,nouns,preposition,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(char *string);
void concatenate(char *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","car"};
char *verb[5]= {"drove","jumped","ran","walked","skipped"};
char *preposition[5]= {"to","from","over","under","on"};

do
{


if(counter == 20)
touppercase(article[position]);

concatenate(array,counter,article,row);

concatenate(array,counter,noun,row);

concatenate(array,counter,verb,row);


concatenate(array,counter,preposition,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(char *string)
{
int i =0;
while((*string != '\0') && (i != 1))
{
*string = toupper(*string);
++i;
}
}
void concatenate(char *array[][column],int element, char *string[],int size)
{
int position = 0;
position = (1 + rand() % 5);
strcpy(array[element][column],string[position]);
}
Nov 1 '06 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
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;
Nov 1 '06 #2

P: 40
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
Nov 2 '06 #3

P: 1
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
Nov 3 '06 #4

P: 40
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<cstring>
const int row= 20;

const int column= 80;
void touppercase(char *string);
void concatenate(char *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","car"};
char *verb[5]= {"drove","jumped","ran","walked","skipped"};
char *preposition[5]= {"to","from","over","under","on"};

do
{


if(counter == 20)
touppercase(article[position]);

concatenate(array,counter,article,row);

concatenate(array,counter,noun,row);

concatenate(array,counter,verb,row);


concatenate(array,counter,preposition,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(char *string)
{
int i =0;
while((*string != '\0') && (i != 1))
{
*string = toupper(*string);
++i;
}
}
void concatenate(char *array[][column],int element, char *string[],int size)
{
int position = 0;
position = rand() % 5;
array[element][column] = string[position];

}
Nov 5 '06 #5

Post your reply

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