Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text file... then average the numbers in the text file and print the average.
I'm not entirely sure I'm doing this correctly (This is my first Perl program!) so if there are additional mistakes that anyone sees that will trip me up after I get this problem fixed, I'd more than welcome the feedback.
The error I'm getting with the command 'perl -w myperl':
The error I get with the command 'perl -d myperl':
When I try to just type 'perl myperl', I get a 0 printed to the screen and nothing is in datafile.txt.
My C Program (called 'myproc.c', when compiled is 'myprog'):
#include <stdlib.h>
#define RAND_MAX 10
// function prototypes
void initrand(void);
int randint(int max);
// main function
int main(void)
{
int randomNumber;
int max = 10;
initrand(); // generate the seed
randomNumber = randint(max); // get the random number
return randomNumber;
}
// this function seeds the random number generator
void initrand()
{
srand((unsigned)(time(0)));
}
// this function generates a psuedo-random integer between 0 and max
int randint(int max)
{
return (max*rand()/(RAND_MAX+1));
}
My Perl program (called 'myperl'):
# assign file handler for myprog
$outfile = 'datafile.txt'; # name file we're printing output to
open(INFO, $outfile); # open file for output
for ($i = 0; $i < 20; $i++) # run myprog 20 times
{
$randNum = system(`myprog`); # Get data from the C program
print INFO $randNum; # print collected data to $outfile
print INFO '/n'; # print newline to $outfile
}
# assign input file handler
$infile = 'datafile.txt'; # name file to open for input
open(INFO, $infile); # open file for input
@rands = <INFO>; # put all the data from file into an array
print @rands; # print the array to get the contents of file
$j = 1; # counter for averaging
$sum = 0; # keeps up with final sum
foreach $number (@rands) # loop through data to get the average
{
$sum = $sum + $number;
$j++;
}
$average = $sum / $j; # calculate average
print "$average\n"; # print new average to the screen
School info: Arkansas State Univerity, Jonesboro, AR, Dr. Hai Jiang, CS 4-5223
---------- Post updated 02-20-11 at 12:35 PM ---------- Previous update was 02-19-11 at 06:03 PM ----------
I actually got this figured out with the help of a friend, so here's that answer if it helps anyone:
FOR THE PERL PART
When opening files for input and output in Perl, do it like this:
output-
open(INFO, '>' . $outfile);
input-
open(INFO, '<' . $infile);
To run the C program within your perl program:
$randNum = system(`./myproc`); # Get data from the C program
FOR THE C PART
I had a problem within my initrand function which was causing all the output numbers to be the same. This is caused by the fact that the srand() function for seeding is by the second, which doesn't work when trying to run a program multiple times per second. This is a problem I'm actually still trying to work on a little, but that's a good first step towards solving your own problem.
I was trying to REdefine RAND_MAX, so that was renamed to RANDINT_MAX.
When casting an int, use parentheses:
return (int) (/*stuff*/);