2019-02-19

Feb 20 In-Class Exercise Thread.

Post your solutions to the Feb 20 In-Class Exercise to this thread.
Best,
Chris
Post your solutions to the Feb 20 In-Class Exercise to this thread. Best, Chris
2019-02-20

-- Feb 20 In-Class Exercise Thread
change c = firstHalf.c + secondHalf.c; to c = Math.min(firstHalf.c, secondHalf.c);
change c = firstHalf.c + secondHalf.c; to c = Math.min(firstHalf.c, secondHalf.c);

-- Feb 20 In-Class Exercise Thread
c = java.lang.Math.min(firstHalf.c, secondHalf.c);
prints out -1
(Edited: 2019-02-20)
c = java.lang.Math.min(firstHalf.c, secondHalf.c); prints out -1

-- Feb 20 In-Class Exercise Thread
 Change the line below in the run() function
 c = firstHalf.c + secondHalf.c;
 to this line and import Java.lang.Math
 c = Math.min(firstHalf.c, secondHalf.c);
 
Change the line below in the run() function c = firstHalf.c + secondHalf.c; to this line and import Java.lang.Math c = Math.min(firstHalf.c, secondHalf.c);

-- Feb 20 In-Class Exercise Thread
Modify the code of the last slide so instead of computing a sum, it computes the minimum of the values in a[].
``
Solution: Modify the "merge" operation in `\mathtt{run()}` to calculate the minimum of the subproblems as opposed to the sum. Resource Description for IMG_BC00D8728291-1.jpeg
(Edited: 2019-02-20)
''Modify the code of the last slide so instead of computing a sum, it computes the minimum of the values in a[].'' @BT@@BT@ '''Solution:''' Modify the "merge" operation in @BT@\mathtt{run()}@BT@ to calculate the minimum of the subproblems as opposed to the sum. ((resource:IMG_BC00D8728291-1.jpeg|Resource Description for IMG_BC00D8728291-1.jpeg))

-- Feb 20 In-Class Exercise Thread
In the run() method, changing line 33 from
c = firstHalf.c + secondHalf.c;
to
c = Math.min(firstHalf.c, secondHalf.c);
finds the minimum of the a[] instead of calculating its sum.
In the run() method, changing line 33 from c = firstHalf.c + secondHalf.c; to c = Math.min(firstHalf.c, secondHalf.c); finds the minimum of the a[] instead of calculating its sum.

-- Feb 20 In-Class Exercise Thread
Make below change in run() : if(low != high ){
            firstHalf.sync();
            secondHalf.sync();
            c = Math.min(firstHalf.c, secondHalf.c); //changed
        }
(Edited: 2019-02-20)
Make below change in run() : if(low != high ){ firstHalf.sync(); secondHalf.sync(); c = Math.min(firstHalf.c, secondHalf.c); //changed }

-- Feb 20 In-Class Exercise Thread
import java.lang.Thread;
public class SumDemo extends Thread {
    public SumDemo(SumDemo spawner, int low, int high)
    {
        this.high = high;
        this.low = low;
        this.spawner = spawner;
        done = false;
        c = 0;
    }
    public void run()
    {
        SumDemo firstHalf = null;
        SumDemo secondHalf = null;
        done = false;
        if(low == high)
        {
            c = a[low];
        }
        else
        {
            int mid = (low + high) / 2;
            firstHalf = new SumDemo(this, low, mid);
            firstHalf.start();
            secondHalf = new SumDemo(this, mid + 1, high);
            secondHalf.start();
        }
        if(low != high ){
            firstHalf.sync();
            secondHalf.sync();
            c = firstHalf.c<secondHalf.c?firstHalf.c:secondHalf.c;	
        }
        done = true;
        if(spawner == null)
        {
            System.out.println(c);
        }
    }
    public synchronized void sync()
    {
        if(!done)
        {
            try
            {
                wait(); 
            }
            catch(InterruptedException ie)
            {
                ie.printStackTrace();
            }
        }
    }
    public static void main(String args[])
    {
        SumDemo parent = new SumDemo(null, 0, a.length - 1);
        parent.start();
    }
    int low;
    int high;
    int c = 0;
    SumDemo spawner;
    boolean done;
    static int a[] = {4, 3, 9, -1, 11};
}
(Edited: 2019-02-20)
import java.lang.Thread; public class SumDemo extends Thread { public SumDemo(SumDemo spawner, int low, int high) { this.high = high; this.low = low; this.spawner = spawner; done = false; c = 0; } public void run() { SumDemo firstHalf = null; SumDemo secondHalf = null; done = false; if(low == high) { c = a[low]; } else { int mid = (low + high) / 2; firstHalf = new SumDemo(this, low, mid); firstHalf.start(); secondHalf = new SumDemo(this, mid + 1, high); secondHalf.start(); } if(low != high ){ firstHalf.sync(); secondHalf.sync(); '''c = firstHalf.c<secondHalf.c?firstHalf.c:secondHalf.c;''' } done = true; if(spawner == null) { System.out.println(c); } } public synchronized void sync() { if(!done) { try { wait(); } catch(InterruptedException ie) { ie.printStackTrace(); } } } public static void main(String args[]) { SumDemo parent = new SumDemo(null, 0, a.length - 1); parent.start(); } int low; int high; int c = 0; SumDemo spawner; boolean done; static int a[] = {4, 3, 9, -1, 11}; }

-- Feb 20 In-Class Exercise Thread
Change c = firstHalf.c + secondHalf.c to c = Math.min(firstHalf.c, secondHalf.c)
Change c = firstHalf.c + secondHalf.c to c = Math.min(firstHalf.c, secondHalf.c)

-- Feb 20 In-Class Exercise Thread
Change c = firstHalf.c + secondHalf.c;
to
if (firstHalf.c < secondHalf.c) {c = firstHalf.c;} else {c = secondHalf.c;}
Change c = firstHalf.c + secondHalf.c; to if (firstHalf.c < secondHalf.c) {c = firstHalf.c;} else {c = secondHalf.c;}
[ Next ]
X