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

Write a program that finds the sum of all the elements in an array. You have to

ID: 3865173 • Letter: W

Question

Write a program that finds the sum of all the elements in an array. You have to implement 3 versions of the program. The program will have an integer array of 20,000 elements. Populate the array using random values between -100 and 100.

a) The first version of the program is single threaded. Show the sum, and also the time required to execute the program.

b) The second version of the program has 5 threads. Appropriately divide the summation work among the threads, and then use the main thread to find the sum from the partial sums created by the threads. Show the sum and the time required to execute the program.

c) The third version of the program is similar to the second version, except it uses 10 threads.

Write the program in C, and use pthreads to perform the required actions. Also include a print screen of the execution of the program in addition to the source code.

Explanation / Answer

import java.math.BigInteger;
import java.util.Scanner;

class SumThread extends Thread {
public SumThread (BigInteger from, BigInteger to) {
this.from = from;
this.to = to;
sum = new BigInteger("0");
}

public void run( ) {
for(BigInteger i = from; i.compareTo(to) <= 0; i = i.add(new BigInteger("1"))) {
sum = sum.add(i);
}
}

public BigInteger getSum( ) {
return sum;
}

private BigInteger from, to, sum;
}


public class Sum4 {
public static void main(String args[]) {
/*if(args.length != 3) {
System.out.println("Pass threads, start and end.");
return;
}*/
   Scanner s=new Scanner(System.in);
   System.out.print("Enter The Number Of Threads: ");
/* get arguments */
int num_threads = s.nextInt();
System.out.print("Enter The Starting Number : ");
BigInteger from = new BigInteger(String.valueOf(s.nextInt()));
System.out.print("Enter The Ending Number : ");
BigInteger to = new BigInteger(String.valueOf(s.nextInt()));

/* an array of threads */
SumThread [] threads = new SumThread [num_threads];

/* fill in the start/end ranges for each */
BigInteger step = to.subtract(from).divide(BigInteger.valueOf(num_threads));
long startTime = System.currentTimeMillis();
for(int i = 0; i < num_threads; i++) {
BigInteger start = from.add(step.multiply(BigInteger.valueOf(i)));
BigInteger stop = start.add(step).subtract(BigInteger.valueOf(1));

/* make sure we go all the way to the end on last thread */
if(i == (num_threads - 1)) {
stop = to;   
}
System.out.printf("Thread %s sums from %s to %s. ", i, start.toString( ), stop.toString( ));
threads[i] = new SumThread (start, stop);
}

/* start them all */
for(int i = 0; i < num_threads; i++) {
threads[i].start( );
}

/* wait for all the threads to finish! */
try {
for(int i = 0; i < num_threads; i++) {
threads[i].join( );
}
} catch(InterruptedException e) {
System.out.println("Interrupted");
}

/* calculate total sum */
BigInteger total_sum = new BigInteger("0");
for(int i = 0; i < num_threads; i++) {
total_sum = total_sum.add(threads[i].getSum( ));
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.printf("The sum of %s-%s is %s in total time %d msec. ", from.toString( ), to.toString( ), total_sum.toString( ),elapsedTime);
}
}

Its in JAVA

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