I really need help on this c program!!! pleaaasseeee

C

Clever Monkey

Diana said:
How would I write a program that generates 8 random letters? Thanks!
1. Open up a text editor
2. Edit code
3. Compile code
4. Repeat from (2) until you get the results you like

That's how I'd do it.

This is called the iterative development process. Perhaps post some of
your C code if you run into problems.
 
N

Nelu

Diana said:
How would I write a program that generates 8 random letters? Thanks!

No one here will write the program for you. You have to try and do it
yourself and come back for help when you find yourself in trouble with
the code *you* wrote.

Some pointers:
- use isalpha from <ctype.h>
- use srand and rand from <stdlib.h>
- you may want to use time from <time.h> with srand.
 
D

Default User

Diana said:
How would I write a program that generates 8 random letters? Thanks!


With the C language, presumably. Otherwise you'd be in the wrong
newsgroup. What do you have so far?




Brian
 
D

Dann Corbit

Diana said:
How would I write a program that generates 8 random letters? Thanks!

#include <stdio.h>
int main(void)
{
/* The dispersion of the distribution is a bit lacking. */
puts("aaaaaaaa");
return 0;
}
 
B

Bill Pursell

Diana said:
How would I write a program that generates 8 random letters? Thanks!

Here's a start. You'll want to do something with the
generated values, and you'll want to seed the pseudo-random
number generator so you don't get the same sequence each
time. (You did mean pseudo-random, right? If you meant
truly random, well, that's a lot harder.)

#include <stdlib.h>

int
main(void)
{
int i;
for (i=0; i<8; i++)
'a' + rand() % 26;
}
 
G

Giorgio Silvestri

Bill Pursell said:
Here's a start. You'll want to do something with the
generated values, and you'll want to seed the pseudo-random
number generator so you don't get the same sequence each
time. (You did mean pseudo-random, right? If you meant
truly random, well, that's a lot harder.)

#include <stdlib.h>

int
main(void)
{
int i;
for (i=0; i<8; i++)
'a' + rand() % 26;
}

No, only digits are guaranteed to be consecutive.
You can use a look-up table with rand() % 26 like an index.


Giorgio Silvestri
 
D

David Wade

Bill Pursell said:
Here's a start. You'll want to do something with the
generated values, and you'll want to seed the pseudo-random
number generator so you don't get the same sequence each
time. (You did mean pseudo-random, right? If you meant
truly random, well, that's a lot harder.)

#include <stdlib.h>

int
main(void)
{
int i;
for (i=0; i<8; i++)
'a' + rand() % 26;
}
This won;t work on EBCDIC platforms...
 
B

Bill Pursell

Tosha said:
That won't work on turing machine also.

To the OP, who wanted a simple example:
all of the complaints that my code will not work
are accurate. To those making complaints:
the code does an absolutely perfect job of
giving the OP somewhere to start. Back to the
OP: instead of using 'a' + rand()%26, you can
do:

char * alphabet = "abcde...";
and get the value with:
alphabet[ rand() % length];

That gives you control over your definition of "letter"
and doesn't rely on the system's ordering.
 
R

Richard Heathfield

Bill Pursell said:

To those making complaints:
the code does an absolutely perfect job of
giving the OP somewhere to start.

No, it doesn't. It makes a non-portable assumption and gives a poor example
of using rand().

The OP is better served by those who help him or her to start in the right
place.
 
B

Bill Pursell

Richard said:
Bill Pursell said:



No, it doesn't. It makes a non-portable assumption and gives a poor example
of using rand().

The OP is better served by those who help him or her to start in the right
place.

As opposed to the 5 or 6 posters who ridiculed to OP and provided no
assistance whatsoever.

How is the usage of rand() in the example poor? The usage
there provides a pseudo-random number between 0 and 25
as best as rand() can, and I avoided seeding it
but did comment that seeding would be necessary in an effort
to avoid completely doing the homework problem for the OP.
I fail to see how my usage is poor.
 
R

Richard Heathfield

Bill Pursell said:
As opposed to the 5 or 6 posters who ridiculed to OP and provided no
assistance whatsoever.

Not so.

Clever Monkey suggested, somewhat sarcastically, that the OP have a go
herself, and that she should post code if she got stuck. The sarcasm was to
a certain extent merited - this is not a "do my homework" newsgroup - and
he had a positive suggestion with which to balance it.

Nelu pointed out that the OP had to make the effort, and gave some hints
about how to approach the problem.

Brian asked "What do you have so far?" - an important question.

Dann Corbit's reply was heavily ironic, I agree.

You gave some broken code, that would certainly fail on EBCDIC systems
(typically IBM mainframes). We often see people in this newsgroup saying
that mainframes are a dying breed of no consequence to C programmers - but
of course mainframes are vastly superior to PCs at number-crunching and
will be around for a long long time, and yes, people do write C code for
mainframes. A large part of my own career has been spent writing C programs
that were destined to be run on mainframe systems.

How is the usage of rand() in the example poor? The usage
there provides a pseudo-random number between 0 and 25
as best as rand() can,

No, it doesn't. See http://c-faq.com/lib/randrange.html

<snip>
 
M

mensanator

Bill said:
As opposed to the 5 or 6 posters who ridiculed to OP and provided no
assistance whatsoever.

How is the usage of rand() in the example poor? The usage
there provides a pseudo-random number between 0 and 25
as best as rand() can, and I avoided seeding it
but did comment that seeding would be necessary in an effort
to avoid completely doing the homework problem for the OP.
I fail to see how my usage is poor.

Forget it, you can't win an argument here, they'll quote the FAQ.
Were you yourself to quote the FAQ, you'll find people claiming
that current C compilers no longer have this problem and that
the FAQ doesn't apply.

Give up.
 
B

Bill Pursell

Forget it, you can't win an argument here, they'll quote the FAQ.
Were you yourself to quote the FAQ, you'll find people claiming
that current C compilers no longer have this problem and that
the FAQ doesn't apply.

Give up.

You were right, Richard quoted the FAQ, but he was absolutely
correct to do so. The link he gave was certainly helpful, and
somehow I've never seen it before. I probably skim over it
because I never use rand() and instead use drand48() and
the method described in the last paragraph on the rare occasion
that I need need randomness.

So although the OP apparently abandoned this thread long ago,
and I've become slightly irked at accurate criticism, I still
learned something from it. Thanks for the link, Richard. (to
question 13.16 of the FAQ: http://c-faq.com/lib/randrange.html )
 
M

mensanator

Bill said:
You were right, Richard quoted the FAQ, but he was absolutely
correct to do so. The link he gave was certainly helpful, and
somehow I've never seen it before. I probably skim over it
because I never use rand() and instead use drand48() and
the method described in the last paragraph on the rare occasion
that I need need randomness.

So although the OP apparently abandoned this thread long ago,
and I've become slightly irked at accurate criticism, I still
learned something from it.

Just don't try to repeat what you've learned, otherwise the
other half of the crowd will tell you:

<quote>
But yes, this is exactly what I was telling mensanator: the
fact that his claim about the lower bits having less randomness
is "outdated" given that recent C/C++ libraries do not suffer
from that problem.
 
R

Richard Heathfield

(e-mail address removed) said:
Bill Pursell wrote:

Just don't try to repeat what you've learned, otherwise the
other half of the crowd will tell you:

<quote>
But yes, this is exactly what I was telling mensanator: the
fact that his claim about the lower bits having less randomness
is "outdated" given that recent C/C++ libraries do not suffer
from that problem.
</quote>

Whoever said it has half a point, but the fact remains that some people,
through no fault of their own, are faced with "outdated" C libraries, and
it makes sense to recommend a method that works well across the board,
rather than a method that only works if you don't have an "outdated"
library.

And that's the problem: some of the people who don't have to work with
"outdated" libraries are apparently incapable of recognising their good
fortune, and assume it applies to everyone. It doesn't. This is yet another
manifestation of All The World Uses The Same Kit That I Do Syndrome; an
easy and tempting trap to fall into, but wiser heads avoid so doing.
 
J

John Smith

Forget it, you can't win an argument here ...

This is the price to be paid for getting free help from experts:
they occasionally take themselves a little too seriously.

JS
 
K

Keith Thompson

John Smith said:
This is the price to be paid for getting free help from experts: they
occasionally take themselves a little too seriously.

The price to be paid for getting free help from experts is that
they're usually right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top