Hi!

I wrote this program to generate a keyboard determined random numbers.

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

int amount, aleatory_numbers;
int array[70];
int counter=0;

int generator ()
{
printf ("Introduce the amount of numbers that you wish: ");
scanf ("%d", &amount);
srand ( time(NULL) );
do
{
aleatory_numbers = rand() % 49 + 1;
counter++;
array[counter]=aleatory_numbers;
printf ("%d", array[counter]);
}
while (counter!=amount);
system("pause");

return array[counter],counter;
}

int main ()
{
generator();
}

It works, but now I need to display on screen the frequency of each numbers of the array. How can I do that? I've tried lots of things, but I can't do that.

Thanks.

Recommended Answers

All 2 Replies

Well i guess now you could use another variable to count how many times a particular number is repeated in the data set and then could get the percentage of the occurence of that particular number .

You should however post your tries on doing this and we can help you out from there.

Just in passing:

First return array[counter],counter; is an awful way to say return counter .
The code does not return array[counter] in anyway.

Second

If I were to enter 100 as the number of numbers. then your code would break since the array is only 70 big.

Third

I think that you would prefer to enter a huge number of random and then obtain the number of 1s, 2s etc. You do not care about the order (yet) . So try collecting the statisics first. For example if your random number part only generated numbers between 1 and 10.
if you did this

int array[11];       // arrays are 0->10

// set array elements to zero here. 

while(counter<amount)
{
  N = rand() % 10 + 1;
  array[N]++;
  counter++;
}

// now print the array.

you have a nice set of distributions because array[1] is increased each time N becomes 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.