Solved

Random Sentence Generator

Posted on 2004-04-17
35
1,922 Views
Last Modified: 2009-03-31
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.
0
Comment
Question by:Erik_Myers
  • 11
  • 8
  • 4
  • +5
35 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
Comment Utility
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.


0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
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.
0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
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 :)
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
very well explained,mercantilum. :~)
0
 

Expert Comment

by:DEEP_FX
Comment Utility
Hmm...it also doesn't capitlaize anything either..oh well..thats easy.  Hope my code helps.

Steven.
0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
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 :)
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
>since it is homework none WILL (SHOULD) give you the code.
0
 

Expert Comment

by:DEEP_FX
Comment Utility
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

0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
Sounds like you couldn't even prove it  (:o
0
 

Expert Comment

by:DEEP_FX
Comment Utility
hehe..prove what?  It will crash if rand() evaluates to RAND_MAX
0
 

Author Comment

by:Erik_Myers
Comment Utility
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.
0
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
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 :)
0
 

Author Comment

by:Erik_Myers
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:aib_42
Comment Utility
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.
0
 

Author Comment

by:Erik_Myers
Comment Utility
My parents taught me not to assume untill I had all the facts. Does anyone but me have all the facts?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Erik_Myers
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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?
0
 

Author Comment

by:Erik_Myers
Comment Utility
Ahhhh whatever. I give up. I am sorry if I upset anyone . *<[;-{. I am wrong.
0
 

Author Comment

by:Erik_Myers
Comment Utility
Great big huggs for you ankuratvb
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
>Great big huggs for you ankuratvb
???
0
 

Author Comment

by:Erik_Myers
Comment Utility
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?
0
 

Author Comment

by:Erik_Myers
Comment Utility
The nuances of language will always prevail.
0
 

Author Comment

by:Erik_Myers
Comment Utility
I did pay for this with a credit card...
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
Homework or more Homework,

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

0
 

Author Comment

by:Erik_Myers
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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"
0
 

Author Comment

by:Erik_Myers
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
Thanks for this insightful conversation.
Please do as you please.



0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
Wasn't Mercantilum supposed to get points for this?
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
Comment Utility
Mercantilum has said that he doesn't want points from this question. As such, because this is a homework questions, no points will be awarded.

This question is closed.

Netminder
Site Admin
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now