Hello,

For the program described below, I obtain the correct prime numbers in the file, but when I try to output this data sorted to standard input, I get some garbage when I try it on my school network. However, when I run the same code using vmware's lamp with an ubuntu framework, everything works fine. Are there any suggestions as to why this occurs?

Thanks,
Jill

``````/*******************************************************
* * First, it forks four children.
* * Each of these four children will find prime numbers in
*   different ranges of values.
* * The first child will find all primes between 1 and 100.
* * The second child will find all primes between 101 and 300.
* * The third child will find all primes between 301 and 600.
* * The fourth child will find all primes between 601 and 1000.
* * When a child finds a prime, she should print the value to
*   a single shared file called "results_lastname.dat".
* * When the children have completed their tasks, have
*   the parent read all the values that are in the file and
*   output the primes
*   in a sorted order to standard output.
* Output:
* Prime 1 = 1
* Prime 2 = 2
* Prime 3 = 5
* Prime 4 = 7
* Prime 5 = 11
* .....
* Prime ? = 997
********************************************************************/
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <fstream>
using namespace std;

const int ARRAY_SIZE = 169; // Number of prime numbers from 1-1000

void IsPrime(int rangeFrom, int rangeTo, ofstream & outFile);
void OpenOutFile(ofstream & outFile);
void OpenInputFile(ifstream& inFile);
void ReadFile(ifstream& inFile, int PrimeData[]);
void InsertionSort(ofstream& outFile, int PrimeData[]);

int main()
{
int i;
ofstream outFile;
ifstream inFile;
int pid;
int primeArray[ARRAY_SIZE];

OpenOutFile(outFile);
system("clear");

// Parent creates the first child.
pid = fork();

// Child 1 finds the prime numbers from 1 to 100.
if(pid == 0)
IsPrime(1, 100, outFile);

// Parent creates child 2.
else if(pid > 0)
{
pid = fork();

// Child 2 finds prime numbers from 101 to 300
if(pid == 0)
IsPrime(101, 300, outFile);

// Parent creates child 3.
else if(pid > 0)
{
pid = fork();

// Child 3 finds prime numbers
// from 301 to 600
if(pid == 0)
IsPrime(301, 600, outFile);

// Parent creates child 4.
else if(pid > 0)
{
pid = fork();

// Child 4 finds prime numbers
// from 601 to 1000.
if(pid == 0)
IsPrime(601, 1000,
outFile);

else if(pid > 0)
{
OpenInputFile(inFile);

primeArray);

InsertionSort(outFile,
primeArray);
}
}
}
}

outFile.close();
inFile.close();

return 0;
}

/*********************************************************
* IsPrime
**********************************************************/
void IsPrime(int from, int to, ofstream & outFile)
{
int i, rem = 0, rem0, arraySize = 0;

for(i = from; i <= to; i++)
{
rem0 = 0;
for (int numer = 1; numer <= i; numer++)
{
rem = i % numer;

// If the remainder of the division is
// zero and that denominator was
// not 1 or the number itself, then
// the number isn't prime. We
// increment to show that when
// rem0 is greater than zero we
// don't have a prime on our
// hands.
if (rem == 0 && numer !=1
&& numer != i)
{
rem0++;
break;
}
}
if(rem0 == 0)
{
outFile << i << endl;
}
}
}

/********************************************************************
* OpenOutFile
********************************************************************/
void OpenOutFile(ofstream & outFile)
{
outFile.open("results.txt");
if (!outFile)                               //test
{
cout << "results.txt will not open. The program will exit\a\n";
exit(1);
}
}

/**********************************************************************
* OpenInputFile
**********************************************************************/
void OpenInputFile(ifstream& inFile)
{
inFile.open("results.txt");
if (!inFile)                                          //test
{
cout << "results.txt will not open. The program will exit.\a\n";
exit(1);
}
}

/**********************************************************************
**********************************************************************/
void  ReadFile(ifstream& inFile, int primeData[])
{
string oneLine;
string temp;
int ii = 0;

getline(inFile, oneLine);

while(inFile)
{
temp = oneLine;
primeData[ii] = atoi(temp.data());

getline(inFile, oneLine);

ii++;
}
}

/***********************************************************************
* InsertionSort()
* for ii in range(1, NUM_OF_INTS+1):
*    value = randomInts[ii]
*    jj = ii - 1
*    while jj >= 0 and randomInts[jj] > value:
*        randomInts[jj + 1] = randomInts[jj]
*        jj = jj - 1
*    randomInts[jj+1] = value
***********************************************************************/
void InsertionSort(ofstream& outFile, int PrimeData[])
{
int ii = 0,
jj = 0,
kk = 0,
value = 0;

for(ii = 0; ii < ARRAY_SIZE; ii++)
{
value = PrimeData[ii];
jj = ii - 1;
while(jj >= 0 and PrimeData[jj] > value)
{
PrimeData[jj + 1] = PrimeData[jj];
jj = jj - 1;
}
PrimeData[jj + 1] = value;
}

for(kk = 0; kk < ARRAY_SIZE; kk++)
{
cout << "Prime " << kk << ": "
<< PrimeData[kk] << endl;
}
}``````

Hi,

I'd like to retract my request for help due to figuring it out! I'd like to thank you all for having a place to vent my frustrations anyway. =)
I put the sorting functionality in it's own if statement. I guess this other version of linux got confused. :P

Jill

``````// Parent Child stuff
outFile.close();

if(pid > 0)
{
OpenInputFile(inFile);
InsertionSort(primeArray);
}
inFile.close();
return 0;
}//.... rest of code....``````

So the prime number file is correct at school as well as on ubuntu? Only outputing to standard output (reading from the perfectly correct file) is not working???

I would've imagined that the numbers from the different children would get mixed up in the file if you don't use file-locking. If one wrote 135 and another wrote 246 you might get 132465. At least that's how it seems to me, but I must be missing something if it works at school.

I guess I just got lucky. Thanks for the input.

No problem. I must have posted at the same time as you.

You might want to put explicit exits for each child to make sure none of them is executing more code than you want.

BTW, 1 is not technically prime!

I love math, so I'm a little disappointed that I forgot about one's demotion since I've certainly never had a one on my factor trees. Also, from now on, I will exit() my children.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.