Originally Posted by
Sam19911
Also I wanted to add that n is not unknown it is given in the main function and when the program runs it asks it from the user.
But then you declare a new copy of N inside your random number function. This internal variable masks (hides) the one in main and is used in an uninitialized state in your function. You should be passing N into the function, not counting upon it's existence elsewhere...
A good function performs 1 task... not half of your program's chores.
Code:
#include <stdlib.h>
#include <stdko.h>
#include <time.h>
void MakeLuckyNumbers(int *array, int min, int max, int num)
{ int i;
for (i=0; i < num; i++)
array[i]= min + (rand() % (max - min));
}
int main()
{
int min = -1;
int max = -1;
int num = -1;
int numbers[100];
printf("Lucky number generator... \n\n");
srand(time(NULL));
do
{
printf("How many numbers? : ");
}
while (scanf("%d", &num) < 1 || num < 0 || num > 100);
do
{
printf("The highest number? : ");
}
while (scanf("%d", &max) < 1 || max < 0);
do
{
printf("The lowest number? : ");
}
while (scanf("%d", &min) < 1 || min < 0 || min > max);
MakeLuckyNumbers(numbers,min,max,num);
printf("Your lucky numbers are...\n");
for (int i = 0; i < num; i++)
printf("%d ",numbers[num]);
printf("\n\n");
return 0;
}