473,322 Members | 1,405 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,322 software developers and data experts.

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,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
4 3214
horace1
1,510 Expert 1GB
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
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
Hanns
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
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

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
2
by: JackM | last post by:
Let me attempt to explain my problem. I have a crude php script that takes a text list of songs that was generated by an mp3 list program and translates each entry into the form where they can be...
4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
7
by: lkrubner | last post by:
The PHP scripting language has the array_unique() function that gets the unique, non-redundant values out of an array. Does Javascript have anything similar?
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
2
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
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
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.