I got my program working but again i'm having trouble filling the array up with random numbers. the program works if the number of elements of the array does not exceed (10^5). I realize that the program is "hanging". I understand this to mean that it is taking a long time to fill the array. Is there a faster and more efficient way to fill the array with random integers? once the array is filled the rest of the program runs fast.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// program ask for max value and always returns random numbers < max
int main(void)
{
int max;
long int n[1000];
long int num = 0;
int num2;
int i;
int j;
int counter = 0;
int target;
int numberOfMatches;
srand(time(NULL));
puts("please enter a max number in array");
scanf("%d", &max);
system("cls");
for(num = 0;num < 1000; num++)
{
for ( i = 0; i < 1000; i++ )
{
num2 = rand();
while( num2 > max)
{
num2 = rand()%(max+1); // I suspect the problem is in this block
n[ i ] = num2;
}
}
}
printf("Enter a number you think is in the array from (0 to %d)\n",max);
scanf("%d",&target);
printf("Enter the amount of times you think (%d) is in the array\n",target);
scanf("%d",&numberOfMatches);
for ( i = 0; i < 1000; i++ )
{
if(target == n[i])
{
printf("Element[%d] = %d\n", i, n[i] );
counter++;
}
}
printf("number of matches found = %d\n", counter);
printf("your guess = %d\n",numberOfMatches);
printf("you were off by %d\n",counter-numberOfMatches);
system("pause");
return 0;
}