Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program and PLz also include the output Note: the output must have few

ID: 3862953 • Letter: W

Question

Write a C program and PLz also include the output

Note: the output must have few negative numbers.

A Child Needs its Space It is known that a child process runs in its own address space, while its parent process runs in its own address space. We will use the Threesum algorithm to illustrate this. Write a program the spawns a child process from the parent process. In the child process implement the Threes algorithm. As we know, the Threesum algorithm has N-cubed order and hence takes a long time to complete (especially for larger files of numbers). Use the clock() function to compute the time it takes to run the program. Start the clock at the beginning of the program and end it at the end of the program then compute the execution time. This may give a surprising result, especially for larger files of numbers as you wait for the program to execute. Then begin the clock inside (at the beginning) the child process and end it at the end of the child process. Compute the execution time of the child process and note it and the time of the parent process and note that. Try this with the C system wait() function (so the parent waits for the child to complete) and without the wait() function What do you think caused these results? Explain! You should make a table (and/or graph) of your results and then an objective analysis. Note, to obtain interesting results and use a larger file of numbers, you may want to use the unsigned long int data type with the format specifier %lu or unsigned long long with format specifier %llu, or %lld for just long long

Explanation / Answer

ThreeSum.java


Below is the syntax highlighted version of ThreeSum.java from §1.4 Analysis of Algorithms.


/******************************************************************************
* Compilation: javac ThreeSum.java
* Execution: java ThreeSum input.txt
* Dependencies: In.java StdOut.java Stopwatch.java
* Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt
* http://algs4.cs.princeton.edu/14analysis/2Kints.txt
* http://algs4.cs.princeton.edu/14analysis/4Kints.txt
* http://algs4.cs.princeton.edu/14analysis/8Kints.txt
* http://algs4.cs.princeton.edu/14analysis/16Kints.txt
* http://algs4.cs.princeton.edu/14analysis/32Kints.txt
* http://algs4.cs.princeton.edu/14analysis/1Mints.txt
*
* A program with cubic running time. Reads n integers
* and counts the number of triples that sum to exactly 0
* (ignoring integer overflow).
*
* % java ThreeSum 1Kints.txt
* 70
*
* % java ThreeSum 2Kints.txt
* 528
*
* % java ThreeSum 4Kints.txt
* 4039
*
******************************************************************************/

/**
* The {@code ThreeSum} class provides static methods for counting
* and printing the number of triples in an array of integers that sum to 0
* (ignoring integer overflow).
* <p>
* This implementation uses a triply nested loop and takes proportional to n^3,
* where n is the number of integers.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class ThreeSum {

// Do not instantiate.
private ThreeSum() { }

/**
* Prints to standard output the (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @param a the array of integers
*/
public static void printAll(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (a[i] + a[j] + a[k] == 0) {
StdOut.println(a[i] + " " + a[j] + " " + a[k]);
}
}
}
}
}

/**
* Returns the number of triples (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @param a the array of integers
* @return the number of triples (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}
*/
public static int count(int[] a) {
int n = a.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (a[i] + a[j] + a[k] == 0) {
count++;
}
}
}
}
return count;
}

/**
* Reads in a sequence of integers from a file, specified as a command-line argument;
* counts the number of triples sum to exactly zero; prints out the time to perform
* the computation.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();

Stopwatch timer = new Stopwatch();
int count = count(a);
StdOut.println("elapsed time = " + timer.elapsedTime());
StdOut.println(count);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote