Originally Posted by
kasuka
Hi guys, thanks for replies. Basically what ive been tryin to achieve is to generate 10,000 numbers
thats why im using
Code:
for(i = 0; i <= 10; i++)
{
ran_num = (10.0*rand()/(RAND_MAX+1.0));
arr[ran_num]++;
}
If you want to generate 10,000 random ints in an array you don't do it that way, you're going to run into all kinds of array bounds errors and parts of the array will be left unused...
Try it like this...
Code:
int array = malloc(10000 * sizeof(int)); // it's too big for the stack.
int i;
for (i = 0; i < 10000; i++)
array[i] = rand();
... see how easy that is?
and store it to the array and then divide them to even and odd
Code:
if (arr[ran_num] %2 == 0)
{
printf("%d\n", arr[ran_num]);
eve_num++;
}
else
{
printf("%d\n", arr[ran_num]);
odd_num++;
}
printf("Even No.s : %d\n", eve_num);
printf("Odd No.s : %d\n", odd_num);
i know the logic but im consfused to write it in codes
This part of your code is basically ok... except for the way you pick stuff out of the array...
Code:
int odd_nums = 0;
int even_nums = 0;
for (i = 0; i < 10000; i++)
if (array[i] & 1) // true for odd, false for even
odd_nums++;
else
even_nums++;
printf("/nThere were %d odd numbers and %d even numbrers", odd_nums, even_nums);
Again... very easy to do.
The trick is to think like the computer... little tiny steps, in strict order...