1. ## Urgent help needed!!!

Write a program that produces 20 random numbers between 1 and 20. The program should store all nonduplicate values in an array.
Use the smallest possible array to accomplish this task.

I can get the numbers generated no problem however, i can't get my program to check against the array to se if the value has been stored or not...

Here is my code thus far:
Code:
#include <stdlib.h>
#include <stdio.h>
#define val 20	/*variable to be stored in the array*/
int main()
{

int i,x;
int array[val];
for ( i = 1; i <= 20; i++ )
{
x = 1 + (rand() % 20);
array[val]=x;
printf("%d", array[val]);
printf("\n");
}

printf("\n");
return 0;
}

Also, check the code tags if you want to post code.

>array[val]=x;
This is outside of the array bounds. Remember you access the array by going 0 to val-1, not 1 to val. Also, I'm guessing here, but maybe you meant:
>array[i] = x;
.. and the same thing on the printf() statement.

3. >i can't get my program to check against the array to se if the value has been stored or not...

You could use a loop within the loop for this to check if the value is already present in the array. Don't forget that if you have read in only N values, you must check only the first N values, since a duplicate value might be present after N. Such looks like this.

Code:
for (n = 0; n < MAX; n++)
{
value = get_value ();

/* Check if value already in array */
duplicate_found= false;
for (m = 0; m < n; m++)
{
if (array [m] == value)
{
duplicate_found = true;
}
}

....
}