OK....
This could do with some optimising, but I think it works....
Code:
#include <stdio.h>
int main()
{
int i, array[ 20 ], smallestSize = 0, num, j, flag = 0, total = 0;
srand( time( NULL ) );
for ( i = 0; i < 20; i++ ) {
flag = 0; // Reset flag
num = 1 + rand() % 20;
for ( j = 0; j <= i; j++ ) {
if ( array[ j ] == num ){
--i; // If number already selected count down by 1
flag = 1; // Do not select number
}
}
if(!flag){ // If number is selectable
array[ i ] = num; // Assign it a place in the array
} // Else start again with i 1 value less that it should
}
for ( i = 0; i < 20; i++ ) {
printf( "%d\t", array[ i ] );
total += array[i]; // Check: sum of all values between 1 & 20 = 210
}
printf( "\nTotal of all numbers = %i", total ); // Is it 210??
return 0;
}
The "total" thing is a check. I wanted to make sure that the total of all elements of the array totalled 210 (1+2+3.......+19+20 = 210)
It seems to work, but its just me messing with your code...there should be better algorithms on this board if you look.....