Hello, I have a program here that must generate 256 random numbers between 20 and -20 and write them to a file, numbers.txt. Then I must print the first 10 numbers generated. However with the code I have so far, all I get is the number 7, printed indefinitely on a loop. I'm thinking the j++ counter should be moved elsewhere, but I can't figure out where. Any help is appreciated.
Code:#include <stdio.h> #include <stdlib.h> int rand( void ) ; int main( int argc, char *argv[] ) { int x, i = 0, j = 0 ; FILE *fin ; fin = fopen("numbers.txt", "a+") ; while ( i < 255 ) { x = rand() ; if (( x <= 20 ) && ( x >= -20 )){ fprintf(fin, "%d ", x ) ; i++ ; j++ ; while( j <= 10 ){ printf("\n%d", x ) ; } } } fclose( fin ) ; return 0 ; }



1Likes
LinkBack URL
About LinkBacks



