0

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);
ReadFile(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);
}
}
/**********************************************************************
* ReadFile
**********************************************************************/
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;
}
}
```