Link to home
Start Free TrialLog in
Avatar of Erik_Myers
Erik_Myers

asked on

Random Sentence Generator

I need the solution, code, source, whatever on this question. "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 and noun. As each word is picked,it should be concatenated to the previous words in an array large enough to hold the entire sentence. The words should be seperated by spaces. When the final sentence is output, it should start with a capital letter and end with a peroid. The program should generate 20 such sentences.
    The arrays should be filled as follows: The article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should contain the verbs "drove", "jumped", "walked" and "skipped"; the preposition array should contain the prepositions "to", "from", "over", "under" and "on".
    After the preceding program is written and working, modify the program to produce a short story consisting of several of these sentences.
   BTW: Keep it as simple as possible please. Solution required before the 24 of April 2004.
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

This seems pretty straight forward.  The instructions are explicit and detailed,
including some implementation details.   Experts are not permitted to do your
homework for you.  In fact, your member agreement forbids you asking us to do so.
We can, however, assist you in overcoming obstacles in your design and implementation.
You have got a week to do this project.  I could code this up in 20 minutes (while
watching TV at the same time), but I can do so because I have plenty of experience
gained by writing code rather than asking someone else to do is for me.


Some maniac must create this set of words:)

Some car skipped from any car.
The dog skipped from one town.
Any dog walked under any girl.
Some dog jumped from some car.
A town walked to any town.
The town skipped on one girl.
Some car skipped from any car.
The dog skipped from one town.
Any dog walked under any girl.
Some dog jumped from some car.
A town walked to any town.
The town skipped on one girl.
The car drove to any boy.
One girl skipped under a girl.
A dog drove from any car.
Some girl walked under a girl.
Some boy skipped on any girl.
Some town walked on any dog.
A town jumped from a boy.
Any dog walked under a car.
One boy jumped over a girl.
A town drove from the town.
The town skipped under one girl.
One car jumped from some boy.
A dog skipped to the car.
One car drove over a town.
Hi Erik,
since it is homework noone will (should) give you the code.
However you may need some help so: (I don't choose the most optimized but the one you are likely to understand the better)

1. You need article, noun, verb, preposition, article and noun
where do they come from (article, noun, verb, preposition)?
you have to create 4 arrays of strings, the 1st containing a list of articles, the 2nd of nouns...
(tip: char *array[] = {xxx, yyy, ... })

2. You need a random function
C has random functions like rand() but it will return a value bigger than the size of your arrays...
How do you ensure the random value will be from 0 to (size-1) of your array?
(tip: the operator %)

3. You need a storage string to gather the results
You want the resulting sentence to be read like this current one ... with spaces.
So you have to ensure your array of chars "sentence" is at least as big as maximum length(article) + maxlength (noun )...
And you need spaces in it ... 6 words ... how many spaces do you need?
Taken this value you can create your sentence array of chars (add one for the trailing zero '\0' for strings in C)
If you are not sure put 100 for the time being ;-)
(tip: char sentence [the number you found])

4. For each word you construct the sentence
You have to concatenate each word you find in this order : article, noun, verb, preposition, article and noun in your 'sentence' array of chars.
(tip: strcpy (a,b)  then  strcat (a, b) )

5. Display the result
(tip: printf () )

By the way, given the list of words you have, you will get some pretty funny sentences :)
very well explained,mercantilum. :~)
Avatar of DEEP_FX
DEEP_FX

Hmm...it also doesn't capitlaize anything either..oh well..thats easy.  Hope my code helps.

Steven.
Sounds like you did Erik's homework ;-)    [ok ,needs to change one line and it becomes a valid program...]

Well, you know, all experts on this thread are able to do this obvious program.
Almost none of them had to prove it though.

Enjoy your points :)
>since it is homework none WILL (SHOULD) give you the code.
There is also a 1 / RAND_MAX chance that this program will crash...take the following line:

rands[j] = rand() * maxs[j] / RAND_MAX;

if rand() returns RAND_MAX, then rands[j] will evaluate to maxs[j] which is actualy one above the maximum index of the array. Also, if your compiling on a 16-bit compiler, this statement will cause an overflow, as rand() * maxs[j] may be bigger than an int.

I think thats it

Steven

Sounds like you couldn't even prove it  (:o
hehe..prove what?  It will crash if rand() evaluates to RAND_MAX
Avatar of Erik_Myers

ASKER

And how would one, if one so desired, capitalize the first letter in the first word of each sentence in this program? Just out of pure curiosity.
If your sentence is in the array of chars named 'sentence', you have to get a line like
first char of 'sentence' = make uppercase ( first char of 'sentence' )

To get the first char of an array of chars you have to use [ index ] with the right index...

tips :
- the first index of arrays in C is 0
- http://www.sensi.org/~alec/man/man3/toupper.html  (don't worry about the 'int' type - it will work anyway :)
I am quite appaled that anyone would ASSUME that this is a homework probelm. Do the people in the "know" have a crystal ball which can see truth from fiction? If so please tell me tomorrows Florida winning lottery numbers, I will split half the prooceeds with you. Thank you DEEP FX for seeing the book from the cover, I appreciate the help.
When you need "the solution, code, source, whatever" on a question that is phrased exactly like a homework question, includes basic assets of the programming language and especially, has a deadline, one (quite accurately) assumes that the question at hand is a homework assignment.

I have not seen DEEP FX's code, but Mercantilum's first post is an excellent guideline.
My parents taught me not to assume untill I had all the facts. Does anyone but me have all the facts?
And sunny, do you serriously think that I believe that anyone from experts exchange reads the complaints, I have a feeling they are filed suspectedly close to the cross cut shredder.
Enough.

Erik_Myers,

Lots of people above have proved that this could possibly be a homework question(it surely looks like one as aib_42 pointed out).

Can you give us any proof as to this NOT being a homework question?
Ahhhh whatever. I give up. I am sorry if I upset anyone . *<[;-{. I am wrong.
Great big huggs for you ankuratvb
>Great big huggs for you ankuratvb
???
I would like to carry on this discussion on another platform, since I don't want to be in violation of the membership agreement, and flame or argue. But who said we cant do it through email?
The nuances of language will always prevail.
I did pay for this with a credit card...
Homework or more Homework,

You got your work done,just take it and get on with life.

Maybe I should read my credit card agreement, I believe that there is something in there that says: If the member is not satisified with the product, the charge could be reversed. That is not the exact verbage but I know it has something to that effect.
You're still not satisfied?Even after all that code?

BTW,there's also a clause in your credit card agreement "Terms and Conditions Apply"
And the "Terms and Condition" would not be applicable in this situtation since judgement was passed with a severe lack of evidence. In the end I pay the credit card company, they will cater to my request. A) Loose large ammounts of intrest payments from cardholder due to immediate card cancelation and the balance being paid in full the next day or B) Write off $9.95. It does not take a MBA to figure out the best business deal.
Thanks for this insightful conversation.
Please do as you please.



Wasn't Mercantilum supposed to get points for this?
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial