Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Random Sentence Generator

Posted on 2004-04-17
35
Medium Priority
?
2,153 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
ID: 10850896
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
ID: 10851163
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
ID: 10851850
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10852143
very well explained,mercantilum. :~)
0
 

Expert Comment

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

Steven.
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10852315
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
ID: 10852321
>since it is homework none WILL (SHOULD) give you the code.
0
 

Expert Comment

by:DEEP_FX
ID: 10852326
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
ID: 10852334
Sounds like you couldn't even prove it  (:o
0
 

Expert Comment

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

Author Comment

by:Erik_Myers
ID: 10854539
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
ID: 10856550
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
ID: 10859322
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
ID: 10859537
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
ID: 10860131
My parents taught me not to assume untill I had all the facts. Does anyone but me have all the facts?
0
 

Author Comment

by:Erik_Myers
ID: 10860167
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
ID: 10860241
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
ID: 10860242
Ahhhh whatever. I give up. I am sorry if I upset anyone . *<[;-{. I am wrong.
0
 

Author Comment

by:Erik_Myers
ID: 10860267
Great big huggs for you ankuratvb
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10860277
>Great big huggs for you ankuratvb
???
0
 

Author Comment

by:Erik_Myers
ID: 10860283
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
ID: 10860291
The nuances of language will always prevail.
0
 

Author Comment

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

Expert Comment

by:ankuratvb
ID: 10860319
Homework or more Homework,

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

0
 

Author Comment

by:Erik_Myers
ID: 10860352
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
ID: 10860407
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
ID: 10860518
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
ID: 10860722
Thanks for this insightful conversation.
Please do as you please.



0
 
LVL 9

Expert Comment

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

Accepted Solution

by:
Netminder earned 0 total points
ID: 10945757
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

971 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