r3dbum

Untitled

Feb 8th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. //-------------------------------------------------------
  2. //Filename:
  3. //Author:      
  4. //Date:        
  5. //Description:
  6. //
  7. //
  8. //
  9. //
  10. //
  11. //-------------------------------------------------------
  12. #include <stdio.h>
  13. #include <iostream>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <cmath>
  17. using namespace std;
  18.  
  19. int SumofPrimes(int, int, int[]);
  20. void printPrimes(int, int[]);
  21. void populatePrimes(int, int[]);
  22.  
  23. int main (int argc, char *argv[])
  24. {
  25.         int pid;
  26.         int sum = 0;
  27.         int number = atoi(argv[1]); //saves the number they input
  28.         int primes[number];         //declare an array "primes"
  29.  
  30.         populatePrimes(number, primes); //function call to populate the array
  31.         pid = fork(); //function call to fork
  32.  
  33.         if (pid == 0)   //child
  34.         {
  35.                 cout << "Child Printing..." << "The sum of the first n prime numbers is: " << SumofPrimes(sum, number, primes) << endl;;
  36.                 cout << "Child Printing... Proc ID = " << getpid() << ". Child CPU time = " << endl;
  37.  
  38.                 exit(0);
  39.         }
  40.         else            //parent
  41.         {
  42.                 cout << "Parent Printing... Child pid = " << pid << " Child CPU time = " << endl;
  43.                 cout << "Parent Printing... Proc ID = " << getpid() << ". Parent CPU time = " << endl;
  44.                 cout << "Parent Printing... The first " << number << " prime numbers are: ";
  45.  
  46.                 printPrimes(number, primes);   //this function will print the prime
  47.                                                //numbers in the primes array
  48.                 exit(0);
  49.         }
  50.  
  51.  
  52. }
  53. //************************************************
  54. //This function is used to populate the array primes.
  55. //
  56. //The program will:
  57. //      1)Determine if the number is a 0 or 1
  58. //              (Neither are primes)
  59. //      2)If number is >= 2, the program will store
  60. //        2 as the first prime, and will from there
  61. //        calculate the rest of the primes, and store
  62. //        them in the array primes.
  63. //************************************************
  64. void populatePrimes(int number, int primes[])
  65. {
  66.         int k = 0;
  67.         if(number == 1 || number == 0) // one isn't prime and also one isn't compound
  68.         {
  69.                 cout << "there aren't any prime number" << endl;
  70.                 return;
  71.         }
  72.  
  73.         primes[k] = 2; //the first prime is 2, k then becomes 1
  74.         k++;
  75.  
  76.         for(int n = 3; k < number; n++)
  77.         {
  78.                 bool prime = true;
  79.  
  80.                 for(int i = 0; k/primes[i]; i++)
  81.                 {
  82.                         if(n % primes[i] == 0)
  83.                         {
  84.                                 prime = false;
  85.                                 break;
  86.                         }
  87.                 }
  88.                 if(prime)
  89.                 {
  90.                         primes[k++] = n;
  91.                 }
  92.         }
  93.         return;
  94.  
  95. }
  96.  
  97. //************************************************
  98. //This function is used to calculate the sum of the
  99. //prime numbers. It uses a for loop and loops through
  100. //all of the values, adding them to a single variable.
  101. //************************************************
  102. int SumofPrimes(int sum, int number, int primes[])
  103. {
  104.  
  105.         for(int k = 0; k < number; k++)
  106.         {
  107.                 sum = sum + primes[k];
  108.         }
  109.  
  110.         return sum;
  111. }
  112.  
  113. //************************************************
  114. //This function is used to print the prime numbers
  115. //in the primes array. It uses a for loop and loops
  116. //through all the values, printing each one.
  117. //************************************************
  118. void printPrimes(int number, int primes[])
  119. {
  120.         for(int k = 0; k < number; k++)
  121.         {
  122.                 cout << primes[k] << " ";
  123.         }
  124.  
  125.         cout << endl;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment