Anyone have any ideas on this one.....

J

joebenjamin

I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, I want to generate a list that will
tell the number of random numbers that fell between 1-5, 6-10, 11-15,
16-20, ... , and 46-50. Finally,I would like to print out the results as a
histogram. I have written it out and would think it would look like this.

1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********

I am trying to learn how to do this and I am stuck. Any suggestions would
help. Even some possible tutorials to chek out would help.
 
M

Michal Nazarewicz

joebenjamin said:
I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, I want to generate a list that will
tell the number of random numbers that fell between 1-5, 6-10, 11-15,
16-20, ... , and 46-50. Finally,I would like to print out the results as a
histogram. I have written it out and would think it would look like this.

1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********

I am trying to learn how to do this and I am stuck. Any suggestions would
help. Even some possible tutorials to chek out would help.

To generate random number integer from set [1, 50] you can use: "number
= rand() % 50 + 1;" (remember to initialise pseudo-random number
generator with for instance srand(time(0))).

To get bucket number you can use "bucket = (number - 1) / 5;" (so in
fact it is better to generate random integer from set [0, 49] using
"number = rand() % 50;" and then calculate bucket number using "bucket =
number / 5;").

Then you increment given bucket, ie.: "buckets[bucket]++". Buckets have
to be zeroed first of course. And when results are ready you print the
results using two nested for loops.
 
R

Richard Heathfield

Michal Nazarewicz said:
joebenjamin said:
I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, I want to generate a list that
will tell the number of random numbers that fell between 1-5, 6-10,
11-15, 16-20, ... , and 46-50. Finally,I would like to print out the
results as a histogram. I have written it out and would think it
would look like this.

1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********

I am trying to learn how to do this and I am stuck. Any suggestions
would help. Even some possible tutorials to chek out would help.

To generate random number integer from set [1, 50] you can use:
"number = rand() % 50 + 1;"

Better: int number = 50 * (rand() / (RAND_MAX + 1.0));
(remember to initialise pseudo-random
number generator with for instance srand(time(0))).

If he wants repeatability, he'd be better off using a constant. If he
wants unpredictability (as far as is practical), the following function
(based on code written by Lawrence Kirby) does a better job of
utilising all the available entropy.

#include <stddef.h>
#include <time.h>
#include <limits.h>

/* Usage: srand (time_seed ()); */

/* Choose and return an initial random seed based on the current time.
Based on code by Lawrence Kirby <[email protected]>. */
unsigned
time_seed (void)
{
time_t timeval;
unsigned char *ptr;
unsigned seed;
size_t i;

timeval = time (NULL);
ptr = (unsigned char *) &timeval;

seed = 0;
for (i = 0; i < sizeof timeval; i++)
seed = seed * (UCHAR_MAX + 2U) + ptr;

return seed;
}
To get bucket number you can use "bucket = (number - 1) / 5;" (so in
fact it is better to generate random integer from set [0, 49] using
"number = rand() % 50;" and then calculate bucket number using "bucket
= number / 5;").

Then you increment given bucket, ie.: "buckets[bucket]++". Buckets
have
to be zeroed first of course. And when results are ready you print
the results using two nested for loops.
 
T

tolkien

Try this:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
int matrix[10];
int i;

int x;

srand(time(NULL));
for(i=0;i<10;i++)
matrix=0;

for(i=0;i<100;i++){
x=rand()%50 + 1;

if(x<6)
matrix[0]++;
else if(x<11)
matrix[1]++;
else if(x<16)
matrix[2]++;
else if(x<21)
matrix[3]++;
else if(x<26)
matrix[4]++;
else if(x<31)
matrix[5]++;
else if(x<36)
matrix[6]++;
else if(x<41)
matrix[7]++;
else if(x<46)
matrix[8]++;
else
matrix[9]++;
}


printf("1-5 \t\t ( %3d ) ",matrix[0]);
for(i=0;i<matrix[0];i++)
printf("*");
printf("\n");

printf("6-10 \t\t ( %3d ) ",matrix[1]);
for(i=0;i<matrix[1];i++)
printf("*");
printf("\n");

return 0;
}
 
J

joebenjamin

Thanks Tolkien, that did help, and workd great. I have been working on
that
problem for about 2 weeks now... Much appreciated !!!!
 
B

Bart van Ingen Schenau

tolkien said:
Try this:
<snip - program>

Hey man, thanks for doing my homework.
Will you do my next assignment as well, so I can get an A+?
 
R

Richard Heathfield

Bart van Ingen Schenau said:
<snip - program>

Hey man, thanks for doing my homework.
Will you do my next assignment as well, so I can get an A+?

You'd give an A+ for that? I'd give a C, tops.
 
B

Bart van Ingen Schenau

Richard said:
Bart van Ingen Schenau said:


You'd give an A+ for that? I'd give a C, tops.
I am not that familiar with the letter-grade system.
On a scale of 1 to 10 (10 being the highest score), I would give the
program, if it was turned in as posted, at most a 5.
For starters, the program does not meet the stated requirements.

Bart v Ingen Schenau
 
K

Keith Thompson

Bart van Ingen Schenau said:
Richard Heathfield wrote: [...]
You'd give an A+ for that? I'd give a C, tops.
I am not that familiar with the letter-grade system.
On a scale of 1 to 10 (10 being the highest score), I would give the
program, if it was turned in as posted, at most a 5.
For starters, the program does not meet the stated requirements.

<OT>

The usual letter-grade system is A, B, C, D, F (E is skipped).
A is the best, F is failure. A '+' or '-' may be appended to the
letter grade; B+ is better than B, but not quite as good as A-.
F+ and F- are rarely given; failure is failure.

So your '5' is probably equivalent to Richard's 'C'.
 
W

Walter Roberson

Keith Thompson said:
The usual letter-grade system is A, B, C, D, F (E is skipped).
A is the best, F is failure.
So your '5' is probably equivalent to Richard's 'C'.

Usually, A = 80%, B = 75%, C = 65%, D = 55%, '+' means
higher towards the next best range, '-' means lower towards
the lesser range. So 5 in 1-10 scale would be D-
which is as low as you can go and still pass.
 
D

Default User

Walter said:
Usually, A = 80%, B = 75%, C = 65%, D = 55%, '+' means
higher towards the next best range, '-' means lower towards
the lesser range. So 5 in 1-10 scale would be D-
which is as low as you can go and still pass.


That's not typical in the US. Normally:

A: 90%-100%
B: 80%-89%
C: 70%-79%
D: 60%-69%
F: 0%-59%



Brian
 
R

Richard Bos

Usually, A = 80%, B = 75%, C = 65%, D = 55%, '+' means
higher towards the next best range, '-' means lower towards
the lesser range. So 5 in 1-10 scale would be D-
which is as low as you can go and still pass.

In the 1-10 scale I'm used to - and which, I can only presume since he
uses a news server which belongs to my own ISP and therefore is well
acquainted with my country, Bart grew up with as well - 5 is as high as
you can go and still fail. A 6-, or possibly a 5-and-a-half, is the
lowest pass grade.

Richard
 
C

Charlie Gordon

Keith Thompson said:
Bart van Ingen Schenau said:
Richard Heathfield wrote: [...]
You'd give an A+ for that? I'd give a C, tops.
I am not that familiar with the letter-grade system.
On a scale of 1 to 10 (10 being the highest score), I would give the
program, if it was turned in as posted, at most a 5.
For starters, the program does not meet the stated requirements.

Any decent scale for grading C programming assignment should be 0 based,
don't you think ?
The usual letter-grade system is A, B, C, D, F (E is skipped).
A is the best, F is failure. A '+' or '-' may be appended to the
letter grade; B+ is better than B, but not quite as good as A-.
F+ and F- are rarely given; failure is failure.

So your '5' is probably equivalent to Richard's 'C'.

Yet another example of the culturally engraved refusal to cope with the
decimal system that plagues the USA.
Just for the fun of it, you should expand on how these grades convert to GPA
points...
 
A

Al Balmer

Keith Thompson said:
Bart van Ingen Schenau said:
Richard Heathfield wrote: [...]
You'd give an A+ for that? I'd give a C, tops.

I am not that familiar with the letter-grade system.
On a scale of 1 to 10 (10 being the highest score), I would give the
program, if it was turned in as posted, at most a 5.
For starters, the program does not meet the stated requirements.

Any decent scale for grading C programming assignment should be 0 based,
don't you think ?
The usual letter-grade system is A, B, C, D, F (E is skipped).
A is the best, F is failure. A '+' or '-' may be appended to the
letter grade; B+ is better than B, but not quite as good as A-.
F+ and F- are rarely given; failure is failure.

So your '5' is probably equivalent to Richard's 'C'.

Yet another example of the culturally engraved refusal to cope with the
decimal system that plagues the USA.
 
B

Bart van Ingen Schenau

Charlie said:
Keith Thompson said:
Bart van Ingen Schenau said:
Richard Heathfield wrote: [...]
You'd give an A+ for that? I'd give a C, tops.

I am not that familiar with the letter-grade system.
On a scale of 1 to 10 (10 being the highest score), I would give the
program, if it was turned in as posted, at most a 5.
For starters, the program does not meet the stated requirements.

Any decent scale for grading C programming assignment should be 0
based, don't you think ?

Well, you could think of the scale as running from 0 to 10. The first
point is awarded for turning in a paper with your name on top. :)

I should have made it more clear in my previous post.
In the grading system in the Netherlands, the grades 1 (or 0) to 5
(inclusive) are all fail grades, so they would all correspond to an F.
The grades 6 to 10 are all pass grades, where 10 means a perfect score
(100%).

Bart v Ingen Schenau
 
A

Al Balmer

Al Balmer said:

Does this body look dead to you? No? Then no.

I do wish that the US would follow the UK (and most of the rest of the
world) and go metric. What's the name for the "4.0" grading system
higher ed uses here?
 
R

Richard Heathfield

Al Balmer said:

I do wish that the US would follow the UK (and most of the rest of the
world) and go metric.

And I wish we would go back to Imperial weights and measures. Life in
Britain is much more confusing than it needs to be. The last time I
went to the grocer, I had to ask for 0.68038856 kilograms of carrots,
2.2679619 kilograms of spuds, and 1.8927059 pints of milk.

That's the trouble with these Europeans. Give them 25.4 millimetres and
they'll take 1.1429982 metres.
 

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
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top