niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
· nixdoc.net · man pages · Linux HOWTOs · FreeBSD Tips · Forums
navigation Forum index » Programming » C
Newbie question - displaying trivia questions at random
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
philbo30
*nix forums beginner


Joined: 16 Jul 2006
Posts: 1

PostPosted: Sun Jul 16, 2006 8:39 pm    Post subject: Newbie question - displaying trivia questions at random Reply with 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):


/*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

PostPosted: Sun Jul 16, 2006 9:01 pm    Post subject: Re: Newbie question - displaying trivia questions at random Reply with quote

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

PostPosted: Mon Jul 17, 2006 10:06 pm    Post subject: Re: Newbie question - displaying trivia questions at random Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Sep 09, 2010 11:48 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts random timeout / delivery temporarily suspended unarcher Postfix 0 Fri Jun 13, 2008 12:33 pm
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts configuration question for httpd Karl Wang Apache 1 Fri Jul 21, 2006 2:10 pm
No new posts nim problem/question Ron AIX 0 Fri Jul 21, 2006 1:57 pm
No new posts random shuffles Boris Borcic python 4 Fri Jul 21, 2006 11:11 am

Copyright © 2004-2005 DeniX Solutions SRL
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum |  email marketing service
 
Sponsors: Free Animated Greetings | Free Ecards | Breast Enlargement | Debt Help | Debt Help
Privacy Policy
[ Time: 0.1275s ][ Queries: 17 (0.0970s) ][ GZIP on - Debug on ]