Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------
- //Filename:
- //Author:
- //Date:
- //Description:
- //
- //
- //
- //
- //
- //-------------------------------------------------------
- #include <stdio.h>
- #include <iostream>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <cmath>
- using namespace std;
- int SumofPrimes(int, int, int[]);
- void printPrimes(int, int[]);
- void populatePrimes(int, int[]);
- int main (int argc, char *argv[])
- {
- int pid;
- int sum = 0;
- int number = atoi(argv[1]); //saves the number they input
- int primes[number]; //declare an array "primes"
- populatePrimes(number, primes); //function call to populate the array
- pid = fork(); //function call to fork
- if (pid == 0) //child
- {
- cout << "Child Printing..." << "The sum of the first n prime numbers is: " << SumofPrimes(sum, number, primes) << endl;;
- cout << "Child Printing... Proc ID = " << getpid() << ". Child CPU time = " << endl;
- exit(0);
- }
- else //parent
- {
- cout << "Parent Printing... Child pid = " << pid << " Child CPU time = " << endl;
- cout << "Parent Printing... Proc ID = " << getpid() << ". Parent CPU time = " << endl;
- cout << "Parent Printing... The first " << number << " prime numbers are: ";
- printPrimes(number, primes); //this function will print the prime
- //numbers in the primes array
- exit(0);
- }
- }
- //************************************************
- //This function is used to populate the array primes.
- //
- //The program will:
- // 1)Determine if the number is a 0 or 1
- // (Neither are primes)
- // 2)If number is >= 2, the program will store
- // 2 as the first prime, and will from there
- // calculate the rest of the primes, and store
- // them in the array primes.
- //************************************************
- void populatePrimes(int number, int primes[])
- {
- int k = 0;
- if(number == 1 || number == 0) // one isn't prime and also one isn't compound
- {
- cout << "there aren't any prime number" << endl;
- return;
- }
- primes[k] = 2; //the first prime is 2, k then becomes 1
- k++;
- for(int n = 3; k < number; n++)
- {
- bool prime = true;
- for(int i = 0; k/primes[i]; i++)
- {
- if(n % primes[i] == 0)
- {
- prime = false;
- break;
- }
- }
- if(prime)
- {
- primes[k++] = n;
- }
- }
- return;
- }
- //************************************************
- //This function is used to calculate the sum of the
- //prime numbers. It uses a for loop and loops through
- //all of the values, adding them to a single variable.
- //************************************************
- int SumofPrimes(int sum, int number, int primes[])
- {
- for(int k = 0; k < number; k++)
- {
- sum = sum + primes[k];
- }
- return sum;
- }
- //************************************************
- //This function is used to print the prime numbers
- //in the primes array. It uses a for loop and loops
- //through all the values, printing each one.
- //************************************************
- void printPrimes(int number, int primes[])
- {
- for(int k = 0; k < number; k++)
- {
- cout << primes[k] << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment