C: Create randomly-generated integers, store them in array elements, and print number of integers stored in...











up vote
0
down vote

favorite
1












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]);
}
}


enter image description here



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










share|improve this question




















  • 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












  • 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

















up vote
0
down vote

favorite
1












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]);
}
}


enter image description here



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










share|improve this question




















  • 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












  • 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















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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]);
}
}


enter image description here



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










share|improve this question















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]);
}
}


enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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) 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
















  • 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












  • 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










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














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;
}





share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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;
}





share|improve this answer























  • 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















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;
}





share|improve this answer























  • 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













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;
}





share|improve this answer














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;
}






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues