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

Question: Write a C program that finds the sum of all the el... Bookmark Write a

ID: 3803582 • Letter: Q

Question

Question: Write a C program that finds the sum of all the el... Bookmark Write a C 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..

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;

public class Test {

public static void main(String[] args) {
VectorInteger nums = new VectorInteger() ;
int NUMBERS_TO_SUM = getNumFromUser("Enter number of elements to sum") ;
int sum = 0 ;
Random generate = new Random();
ExecutorService ex;
int randNum = 0 ;

//Generating 100 random numbers
for ( int i= 0 ; i NUMBERS_TO_SUM ; i++)
{
randNum = generate.nextInt(100) + 1;
nums.add(randNum);
sum += randNum ;
System.out.printf("%d - is %d" , i , randNum);
}

System.out.println();
System.out.println("The sum is " + sum );
int numOfThread = getNumFromUser("Enter number of threads") ;
ex = Executors.newFixedThreadPool(numOfThread) ;
Controller c = new Controller(nums , numOfThread) ;
//Creating all Threads
for (int i = 0 ; i numOfThread ; i++)
{
ex.execute(new sumThread(c));
}
ex.shutdown();
System.out.println("OK Threads finished successfully sum is " );
}

//Getting number from the user
private static int getNumFromUser(String inputString){
boolean inputAgian=true;
int input=-1;
while(inputAgian){
try{
input = Integer.parseInt(JOptionPane.showInputDialog(inputString));
if ((input 0) )
inputAgian = false;
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Enter an Integer");
}

}
return input;
}

}** class sumThread -- for using each thread instance
package Question1;

public class sumThread extends Thread{
private Controller controller;

/**
* Constructor for sumThread
* @param c
*/
public sumThread (Controller c)
{
controller = c ;
}

/**
* Invoke Thread operation
*/
public void run()
{
while(!controller.isFinish())
{
controller.sumTwoNums();
}
System.out.println("Sum is " + controller.array.get(0));
}
}** class Controller -- for controlling the processes
package Question1;

import java.util.*;

public class Controller {
public static VectorInteger array ;

boolean isFinish = false ;

public Controller(Vector numbers , int maxThreads)
{
array = numbers ;
}
/**
* Will sum 2 numbers from the array
*/
public synchronized void sumTwoNums(){
int sum = 0;
notify();
try{
wait();
}
catch(InterruptedException e)
{}
if(!isFinish){
sum = array.remove(array.size()-1) + array.remove(array.size()-1);
array.add(sum);

}

if (array.size() == 1){
isFinish = true;
notify();
}

}
public boolean isFinish() {
return isFinish;
}

public void setFinish(boolean isFinish) {
this.isFinish = isFinish;
}

}

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