Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random number output file

Status
Not open for further replies.

BrianCPP

Technical User
Sep 20, 2002
3
0
0
US
How do I output 50 randomly generated numbers to a text file.
 

Code:
#include <stdio.h>
#include <stdlib.h>

void main()
{
    FILE *fp;

    fp = fopen(&quot;file.txt&quot;,&quot;w&quot;);

    if(!fp)
    {
        fprintf( stderr, &quot;Error while opening \&quot;file\&quot;&quot;);
        exit(1);
    }

    for( i = 0; i < 50; i++ )
    {
        fprintf( fp, &quot;%d\n&quot;, rand() % 50 );
    }
 
    fclose(fp);
}
 
Well, here is a sample that generates a 5 by 6 array...aka lotto drawing....I'm not sure if this is what you are asking for......


#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>


void GenerateNumbers( int lotto[][6] );



main()
{

int lotto[5][6], r, c;

srand(( unsigned ) time(NULL ));

GenerateNumbers( lotto );

for ( r=0; r < 5; r++ )
{
for ( c=0; c<6; c++ )
{
cout << lotto[r][c] << &quot; &quot;;

}
cout << &quot;\n\n&quot;;

}

cout << &quot;\n\n&quot;;


}


void GenerateNumbers(int lotto[][6] )
{
int num, r, c;
for ( r=0; r < 5; r++ )
{
for ( c=0; c<6; c++ )
{
num = rand() % 47;
lotto[r][c] = num;

}
}




}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top