The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C++ Code
Posted by: sjdurfey | February 9, 2010 @ 3:15pm
C++ Code
[
Download
]
#include <iostream> #include <stdio.h> #include <cmath> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <iomanip> #include <sys/resource.h> using namespace std; void findPrimes(int primes[], int numPrimesToFind); void sumPrimes(int primes[], int numPrimesToFind); int main(int argc, char **argv) { int numPrimesToFind = atoi(argv[1]); int primes[numPrimesToFind]; int stat_val; struct rusage parent_usage; struct rusage child_usage; double usage = 0.0; pid_t childPID; pid_t child_pid; findPrimes(primes, numPrimesToFind); childPID = fork(); if(childPID == -1) { cout << "No children were forked in the making of this program :(" << endl; exit(1); } if(childPID == 0) { cout << "=======================Child========================\n"; sumPrimes(primes, numPrimesToFind); getrusage(RUSAGE_SELF, &child_usage); usage = child_usage.ru_utime.tv_sec + child_usage.ru_stime.tv_sec; //) / pow(10.0, 9); cout << "Process ID: " << getpid() << " ..... " << " CPU time " << fixed << showpoint << setprecision(6) << usage << " s\n\n"; cout << "Ending child process\n\n"; cout << "====================================================\n\n"; } else { child_pid = wait(0); cout << "==========================Parent=========================\n\n"; getrusage(RUSAGE_CHILDREN, &child_usage); usage = child_usage.ru_utime.tv_sec + child_usage.ru_stime.tv_sec; //) / pow(10.0, 9); cout << "Child PID = " << childPID << " Child CPU time = " << fixed << showpoint << setprecision(6) << usage << " s\n"; getrusage(RUSAGE_SELF, &parent_usage); usage = parent_usage.ru_utime.tv_sec + parent_usage.ru_stime.tv_sec; //) / pow(10.0, 9); cout << "Parent PID = " << getpid() << " Parent CPU time = " << fixed << showpoint << setprecision(6) << usage << " s\n"; cout << endl << endl << endl << "List of first " << numPrimesToFind << " prime numbers:\n"; /* for(int index = 0; index < numPrimesToFind; index++) { cout << primes[index] << endl; } */ cout << "=============================================================\n\n"; } return 0; } void findPrimes(int primes[], int numPrimesToFind) { int divider = 2; int number = 2; int index = 0; bool prime = true; /*loop through to find the first numPrimesToFind prime numbers*/ while(index < numPrimesToFind) { /*A composite integer will have a prime number as a factor that does not exceed the square root of the composite integer. If this prime is not found, then the number itself must be prime*/ while (divider <= sqrt(number) && prime) { /*if number divides evenly into divider, then it is not prime*/ if(number % divider == 0) prime = false; divider++; } if(prime) { primes[index] = number; index++; } prime = true; divider = 2; number++; } } void sumPrimes(int primes[], int numPrimesToFind) { int sum = 0; for(int index = 0; index < numPrimesToFind; index++) sum += primes[index]; cout << endl << "The sum of the first " << numPrimesToFind << " prime numbers is: " << sum << endl; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.38 / 8
55 Votes
http://codebin.yi.org/492
page generated in 0.01 seconds