Hi guys,
Im trying to implement a function/program that generates numbers from 1 to the input number given by the user. I've come close to generating all the numbers however there are some duplicates of the numbers still remain.
I just want to generate the number with no duplicates but my algorithm must be wrong, can anyone see anything wrong with what ive done?
Cheers.
Code:#include <stdlib.h> #include <stdio.h> #include <time.h> #define MAXSIZE 1000 int main() { int k; int i; int j; int num; int n=0; int tmp[MAXSIZE]; printf( "Enter integer (max 1000): " ); scanf( "%d", &k ); srand(1234567); for( i=1; ; i++ ) { num = 1+rand()%k; for( j=0; j<=MAXSIZE; j++ ) { if ( num == tmp[j] ) break; else if ( (num != tmp[j]) ) { tmp[n++] = num; break; } } if ( n==k ) break; } for( i=0; i<=n-1; i++ ) printf( "%d ", tmp[i] ); printf( "\n" ); return 0; }