| Author |
Message |
philbo30 *nix forums beginner
Joined: 16 Jul 2006
Posts: 1
|
Posted: Sun Jul 16, 2006 8:39 pm Post subject:
Newbie question - displaying trivia questions at random
|
|
|
Caveat: I know very little about C; please exuse my current ignorance.
Here's what I need to do:
I have a list of 10 quotations that I want to display, at random each
time my DOS application is run. Currently, I have them commented out,
since I don't know how to get them working. Here's an example of how
they look right now (just 2 for reference):
/*Trivia Question 1
printf("Today's Trivia\n");
printf("Did you know that the\n");
printf("Sears Tower is over 500\n");
printf("meters tall and contains 108\n");
printf("floors?\n");
*/
/*Trivia Question 2
printf("Today's Trivia\n");
printf("Did you know that the\n");
printf("Fordham Spire is over 600\n");
printf("meters tall and contains 124\n");
printf("floors?\n");
*/
Because I have limited output space to work with, I need them to
display in the manner shown above. (i.e. no more than 4 to 5 words per
line).
I assume the basic process is something to the effect of:
1. Associate each of the 10 trivia questions with an integer
2. Upon running the .exe, generate a random integer from the list and
then
3. Display the trivia question associated with the random integer
4. Do again
It really boils down to # 3, displaying each trivia question,
consisting of 5 to 7 lines, as a unit, at random, each time application
runs.
In advance, thank you for any information you may be able to provide or
any direction you may be able to point me in. Please note that this
will be running on an embedded system, so the most simple approach, as
usual, is the best, I think.
Philbo |
|
| Back to top |
|
 |
neutron*star *nix forums Guru
Joined: 21 Feb 2005
Posts: 2039
|
Posted: Sun Jul 16, 2006 9:01 pm Post subject:
Re: Newbie question - displaying trivia questions at random
|
|
|
philbo30 said:
| Quote: | Caveat: I know very little about C; please exuse my current ignorance.
Here's what I need to do:
I have a list of 10 quotations that I want to display, at random each
time my DOS application is run.
|
If you mean MS-DOS, you have lots of memory - tens of kilobytes - to play
with, so this should be trivial.
| Quote: | Because I have limited output space to work with, I need them to
display in the manner shown above. (i.e. no more than 4 to 5 words per
line).
|
Store them as an array of const char *, e.g.
const char *trivia[] =
{
"I knew a man with a wooden leg called Smith",
"Peter Piper picked a peck of pickled pepper",
"foo bar baz quux whatever blah blah"
};
Let the maximum display width be N.
Write the intro:
puts("Today's Trivia");
puts("Did you know that");
Now pick a trivium at random (see FAQ for how to get a random number in the
right range).
Point to the beginning of the trivium.
Find the length of the trivium.
For as long as that length exceeds N:
Find the last possible displayable character, N bytes on from the
beginning of the text that hasn't yet been displayed.
Search backward from that point until you find a space or hit the
beginning of the string.
Display everything up to that point.
Point to the beginning of the text that hasn't yet been displayed.
Subtract from your length counter the number of characters you
just displayed.
Rof
Display the remainder of the trivium.
<snip>
| Quote: | In advance, thank you for any information you may be able to provide or
any direction you may be able to point me in. Please note that this
will be running on an embedded system, so the most simple approach, as
usual, is the best, I think.
|
The simplest approach would be to hire a C programmer to do this for you.
This really is very basic stuff.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) |
|
| Back to top |
|
 |
Simon Biber *nix forums Guru Wannabe
Joined: 19 May 2005
Posts: 216
|
Posted: Mon Jul 17, 2006 10:06 pm Post subject:
Re: Newbie question - displaying trivia questions at random
|
|
|
philbo30 wrote:
| Quote: | Caveat: I know very little about C; please exuse my current ignorance.
Here's what I need to do:
I have a list of 10 quotations that I want to display, at random each
time my DOS application is run. Currently, I have them commented out,
since I don't know how to get them working. Here's an example of how
they look right now (just 2 for reference):
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
/* set the seed of the pseudo-random
number generator from the current time */
srand(time(NULL));
/* select a block of code based
on a random number from 0 to 9 */
switch(rand() % 10)
{
case 0:
| Quote: | printf("Today's Trivia\n");
printf("Did you know that the\n");
printf("Sears Tower is over 500\n");
printf("meters tall and contains 108\n");
printf("floors?\n");
|
break;
case 1:
| Quote: | printf("Today's Trivia\n");
printf("Did you know that the\n");
printf("Fordham Spire is over 600\n");
printf("meters tall and contains 124\n");
printf("floors?\n");
|
break;
case 2:
...
}
return 0;
}
--
Simon. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|