C: Create randomly-generated integers, store them in array elements, and print number of integers stored in...
up vote
0
down vote
favorite
I'm incredibly new to C
(and programming in general) and finding how to manipulate arrays is almost impossible to understand (I know what an array is).
I'm attempting to write a program that generates 100 random integers in a range (1-50), stores them in array elements (1-10, 11-20, 21-30, 31-40, and 41-50), and print the number of randomly generated integers stored in each element, i.e.
- 1-10 = 20
- 11-20 = 30
- 21-30 = 21
- 31-40 = 19
- 41-50 = 20
The best I can come up with so far is:
void randomNumbers
{
int count[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = 0;
}
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = rand() % 50 + 1;
}
for (int i = 0; i <= ARRAY_LENGTH - 1; i++)
{
printf("Index %d -> %dn", i, count[i]);
}
}
That just says "element 1 = random number, element 2 = random number" etc.
I don't understand how to:
- Store the randomly-generated integers in the array's elements
- Partition the randomly-generated integers into the corresponding
element - Tell the program to print the number of integers generated in each
element range
c arrays element random-seed
add a comment |
up vote
0
down vote
favorite
I'm incredibly new to C
(and programming in general) and finding how to manipulate arrays is almost impossible to understand (I know what an array is).
I'm attempting to write a program that generates 100 random integers in a range (1-50), stores them in array elements (1-10, 11-20, 21-30, 31-40, and 41-50), and print the number of randomly generated integers stored in each element, i.e.
- 1-10 = 20
- 11-20 = 30
- 21-30 = 21
- 31-40 = 19
- 41-50 = 20
The best I can come up with so far is:
void randomNumbers
{
int count[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = 0;
}
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = rand() % 50 + 1;
}
for (int i = 0; i <= ARRAY_LENGTH - 1; i++)
{
printf("Index %d -> %dn", i, count[i]);
}
}
That just says "element 1 = random number, element 2 = random number" etc.
I don't understand how to:
- Store the randomly-generated integers in the array's elements
- Partition the randomly-generated integers into the corresponding
element - Tell the program to print the number of integers generated in each
element range
c arrays element random-seed
1
Are you saying you have 5 bins, each of which is used to store numbers in its range? I can't see any other point in the grouping. Aside:i <= ARRAY_LENGTH - 1;
is more idiomatic (and readable) asi < ARRAY_LENGTH;
– Weather Vane
Nov 10 at 20:57
If the random number,r
, is in the range [1,50] and you then want to reduce the value with an expression that yields0
whenr
is in the range 1..10, and yields 2 whenr
is in the range 11..20, etc, then(r - 1) / 10
yields the correct result. (For example,r == 1; (r - 1) / 10 == 0;
—r == 10; (r - 1) / 10 == 0;
—r == 11; (r - 1) / 10 == 1;
—r == 50; (r - 1) / 10 == 4;
— etc.)
– Jonathan Leffler
Nov 10 at 21:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm incredibly new to C
(and programming in general) and finding how to manipulate arrays is almost impossible to understand (I know what an array is).
I'm attempting to write a program that generates 100 random integers in a range (1-50), stores them in array elements (1-10, 11-20, 21-30, 31-40, and 41-50), and print the number of randomly generated integers stored in each element, i.e.
- 1-10 = 20
- 11-20 = 30
- 21-30 = 21
- 31-40 = 19
- 41-50 = 20
The best I can come up with so far is:
void randomNumbers
{
int count[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = 0;
}
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = rand() % 50 + 1;
}
for (int i = 0; i <= ARRAY_LENGTH - 1; i++)
{
printf("Index %d -> %dn", i, count[i]);
}
}
That just says "element 1 = random number, element 2 = random number" etc.
I don't understand how to:
- Store the randomly-generated integers in the array's elements
- Partition the randomly-generated integers into the corresponding
element - Tell the program to print the number of integers generated in each
element range
c arrays element random-seed
I'm incredibly new to C
(and programming in general) and finding how to manipulate arrays is almost impossible to understand (I know what an array is).
I'm attempting to write a program that generates 100 random integers in a range (1-50), stores them in array elements (1-10, 11-20, 21-30, 31-40, and 41-50), and print the number of randomly generated integers stored in each element, i.e.
- 1-10 = 20
- 11-20 = 30
- 21-30 = 21
- 31-40 = 19
- 41-50 = 20
The best I can come up with so far is:
void randomNumbers
{
int count[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = 0;
}
for (int i = 0; i < ARRAY_LENGTH; i++)
{
count[i] = rand() % 50 + 1;
}
for (int i = 0; i <= ARRAY_LENGTH - 1; i++)
{
printf("Index %d -> %dn", i, count[i]);
}
}
That just says "element 1 = random number, element 2 = random number" etc.
I don't understand how to:
- Store the randomly-generated integers in the array's elements
- Partition the randomly-generated integers into the corresponding
element - Tell the program to print the number of integers generated in each
element range
c arrays element random-seed
c arrays element random-seed
edited Nov 10 at 22:48
Nadim Baraky
429516
429516
asked Nov 10 at 20:48
hailnolly
155
155
1
Are you saying you have 5 bins, each of which is used to store numbers in its range? I can't see any other point in the grouping. Aside:i <= ARRAY_LENGTH - 1;
is more idiomatic (and readable) asi < ARRAY_LENGTH;
– Weather Vane
Nov 10 at 20:57
If the random number,r
, is in the range [1,50] and you then want to reduce the value with an expression that yields0
whenr
is in the range 1..10, and yields 2 whenr
is in the range 11..20, etc, then(r - 1) / 10
yields the correct result. (For example,r == 1; (r - 1) / 10 == 0;
—r == 10; (r - 1) / 10 == 0;
—r == 11; (r - 1) / 10 == 1;
—r == 50; (r - 1) / 10 == 4;
— etc.)
– Jonathan Leffler
Nov 10 at 21:04
add a comment |
1
Are you saying you have 5 bins, each of which is used to store numbers in its range? I can't see any other point in the grouping. Aside:i <= ARRAY_LENGTH - 1;
is more idiomatic (and readable) asi < ARRAY_LENGTH;
– Weather Vane
Nov 10 at 20:57
If the random number,r
, is in the range [1,50] and you then want to reduce the value with an expression that yields0
whenr
is in the range 1..10, and yields 2 whenr
is in the range 11..20, etc, then(r - 1) / 10
yields the correct result. (For example,r == 1; (r - 1) / 10 == 0;
—r == 10; (r - 1) / 10 == 0;
—r == 11; (r - 1) / 10 == 1;
—r == 50; (r - 1) / 10 == 4;
— etc.)
– Jonathan Leffler
Nov 10 at 21:04
1
1
Are you saying you have 5 bins, each of which is used to store numbers in its range? I can't see any other point in the grouping. Aside:
i <= ARRAY_LENGTH - 1;
is more idiomatic (and readable) as i < ARRAY_LENGTH;
– Weather Vane
Nov 10 at 20:57
Are you saying you have 5 bins, each of which is used to store numbers in its range? I can't see any other point in the grouping. Aside:
i <= ARRAY_LENGTH - 1;
is more idiomatic (and readable) as i < ARRAY_LENGTH;
– Weather Vane
Nov 10 at 20:57
If the random number,
r
, is in the range [1,50] and you then want to reduce the value with an expression that yields 0
when r
is in the range 1..10, and yields 2 when r
is in the range 11..20, etc, then (r - 1) / 10
yields the correct result. (For example, r == 1; (r - 1) / 10 == 0;
— r == 10; (r - 1) / 10 == 0;
— r == 11; (r - 1) / 10 == 1;
— r == 50; (r - 1) / 10 == 4;
— etc.)– Jonathan Leffler
Nov 10 at 21:04
If the random number,
r
, is in the range [1,50] and you then want to reduce the value with an expression that yields 0
when r
is in the range 1..10, and yields 2 when r
is in the range 11..20, etc, then (r - 1) / 10
yields the correct result. (For example, r == 1; (r - 1) / 10 == 0;
— r == 10; (r - 1) / 10 == 0;
— r == 11; (r - 1) / 10 == 1;
— r == 50; (r - 1) / 10 == 4;
— etc.)– Jonathan Leffler
Nov 10 at 21:04
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The following is the code that generates 100 random integers and groups them into categories based on their value :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, temp;
int a[5]; // array to store the frequency
for(i=0;i<5;i++)
a[i]=0;
srand(time(0)); // for generating new random integers on every run
for(i=0;i<100;i++)
{
temp = (rand()%50) + 1; // generates random integers b/w 1 to 50
a[(temp-1)/10]++;
}
for(i=0;i<5;i++)
printf("%d->%d = %dn",i*10+1,(i+1)*10,a[i]); //printing in the desired format
return 0;
}
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The following is the code that generates 100 random integers and groups them into categories based on their value :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, temp;
int a[5]; // array to store the frequency
for(i=0;i<5;i++)
a[i]=0;
srand(time(0)); // for generating new random integers on every run
for(i=0;i<100;i++)
{
temp = (rand()%50) + 1; // generates random integers b/w 1 to 50
a[(temp-1)/10]++;
}
for(i=0;i<5;i++)
printf("%d->%d = %dn",i*10+1,(i+1)*10,a[i]); //printing in the desired format
return 0;
}
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
add a comment |
up vote
1
down vote
accepted
The following is the code that generates 100 random integers and groups them into categories based on their value :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, temp;
int a[5]; // array to store the frequency
for(i=0;i<5;i++)
a[i]=0;
srand(time(0)); // for generating new random integers on every run
for(i=0;i<100;i++)
{
temp = (rand()%50) + 1; // generates random integers b/w 1 to 50
a[(temp-1)/10]++;
}
for(i=0;i<5;i++)
printf("%d->%d = %dn",i*10+1,(i+1)*10,a[i]); //printing in the desired format
return 0;
}
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The following is the code that generates 100 random integers and groups them into categories based on their value :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, temp;
int a[5]; // array to store the frequency
for(i=0;i<5;i++)
a[i]=0;
srand(time(0)); // for generating new random integers on every run
for(i=0;i<100;i++)
{
temp = (rand()%50) + 1; // generates random integers b/w 1 to 50
a[(temp-1)/10]++;
}
for(i=0;i<5;i++)
printf("%d->%d = %dn",i*10+1,(i+1)*10,a[i]); //printing in the desired format
return 0;
}
The following is the code that generates 100 random integers and groups them into categories based on their value :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, temp;
int a[5]; // array to store the frequency
for(i=0;i<5;i++)
a[i]=0;
srand(time(0)); // for generating new random integers on every run
for(i=0;i<100;i++)
{
temp = (rand()%50) + 1; // generates random integers b/w 1 to 50
a[(temp-1)/10]++;
}
for(i=0;i<5;i++)
printf("%d->%d = %dn",i*10+1,(i+1)*10,a[i]); //printing in the desired format
return 0;
}
edited Nov 10 at 21:46
Jonathan Leffler
554k886591012
554k886591012
answered Nov 10 at 21:06
ask
727
727
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
add a comment |
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
Thanks for this! Is there any way I can adapt the code so I can change the ranges (e.g. 1-12, 13-20 etc.)?
– hailnolly
Nov 10 at 22:46
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243259%2fc-create-randomly-generated-integers-store-them-in-array-elements-and-print-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you saying you have 5 bins, each of which is used to store numbers in its range? I can't see any other point in the grouping. Aside:
i <= ARRAY_LENGTH - 1;
is more idiomatic (and readable) asi < ARRAY_LENGTH;
– Weather Vane
Nov 10 at 20:57
If the random number,
r
, is in the range [1,50] and you then want to reduce the value with an expression that yields0
whenr
is in the range 1..10, and yields 2 whenr
is in the range 11..20, etc, then(r - 1) / 10
yields the correct result. (For example,r == 1; (r - 1) / 10 == 0;
—r == 10; (r - 1) / 10 == 0;
—r == 11; (r - 1) / 10 == 1;
—r == 50; (r - 1) / 10 == 4;
— etc.)– Jonathan Leffler
Nov 10 at 21:04