View Full Version : Generating random number in an array of a text file.
alvinleephd
12-10-2006, 09:13 PM
Hi all, i'm trying to write a function that will generate 25 random numbers into the array of a text file. i have to use rand() to generate the #. here's what i have so far:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUMS 25 /* maximum number of numbers */
void quicksort( int [], int, int );
void fprint_arr( int [], int, FILE *);
int main()
{
int dataarray[MAXNUMS] = rand() % (99) + 1; /* program storage for numbers sorted */
int num_nums=0,eof; /* number of numbers, end_of_file */
FILE *inptr, *fileoutput;
void print_arr(int dataarray[], int num_nums);
}
inptr = fopen("randQSort.txt","r"); /* file opened properly ? */
if (inptr == NULL)
{
printf("Open failed, check data filename\n");
exit(1);
}
i get tons of errors and i'm not sure why.. how can i implement rand() to an array? i need to generate these random numbers to be quicksorted.
rpgfan3233
12-10-2006, 10:00 PM
You need to use a loop for each element of the array:
int dataarray[MAXNUMS];
int i;
for (i = 0; i < MAXNUMS; i++)
dataarray[i] = (rand() % 99) + 1;
That should fix some of the errors.
Edit: Also, your function prototypes should be fixed. In a header file (commonly .h), no variable is needed. In the actual source file for the program, you must have variables. Just as you declare "int num_nums", you must also declare "void quicksort( int x[], int y, int z );", for example, rather than simply "void quicksort( int [], int, int );".
Ok, is this a cut and paste job from a few examples you have? I ask because there are some errors in this code that I just can't imagine you'd make if it wasn't.
Advise: write code and compile incrementally. To make sure that new code works correctly. It will also allow you to find a smaller number of errors at a time, so you don't get overwhelmed with a mass of errors. Also, take note of the line in the code that the compiler is complaining about as it will give you a hint as to what is wrong.
That said, I will suggest a few fixes:
As rpgfan pointed out...
int dataarray[MAXNUMS] = rand() % (99) + 1;
I'm assuming you're trying to assign 25 random numbers to the array here, it will never work, and the syntax is illegal. You'll need to follow rpgfan's advice and make a for-loop to populate the array.
It's also best that you seed the random number generator using time(NULL) so that the random numbers aren't always the same. Here's how.
#include <time.h> //Add this to your include statements
...
//Then in main do this
srand(time(NULL)); //Seed random number generator so that random number are always different.
//Fill array with random numbers
for(i = 0; i < MAXNUMS; i++)
{
dataarray[i] = rand() %99 + 1;
}
Ok, what else...
void print_arr(int dataarray[], int num_nums);
}
A) I assume you know that methods/functions must be declared at the top of the file and not in main, as you correctly defined two functions already. Not sure how this definition ended up in the middle of your main.
B) That curly brace has no business being there. Remove it, and add it at the end of the file to "close" the main.
duanedtp2
04-25-2011, 09:11 AM
Can someone please help me generate a series of 13 digit numbers and output to a text file. I'm desperate here
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.