Starting from:

$30

CSC402 - Homework 5  - Solved

1.    1.3.19   (You may assume the list has at least one node).

 

2.    1.4.5     (show your work)

3.    1.4.6    (it might help to try some small values of N – see if you see a pattern.

 

4.    Review the Java program on the next page.   

 

Carefully compare the two functions: addTwoIntsNtimes, addThreeIntsNtimes and review how they are called from main.

 

Answer the three questions Q1, Q2, Q3 in the comments in the main function.  

      Q1.  

 

      Q2.

 

      Q3.

 

 

5.    Create a java program with this class and run it using the values in the table below. (You will need to change the value of the variable reps in the program).  Record the value printed in the table.  

Reps 
Printed value of diff 
10000
 
100000
 
1000000
 
10000000
 
100000000
 
1000000000
 
 

 

       

       

Vers 1.0 

                                                                                -     Over -  

package algs11;

 

import stdlib.In; import stdlib.StdOut; import stdlib.StdRandom; import stdlib.Stopwatch;

 public class ArithTimer {

 

      public static int addTwoIntsNtimes(int reps) {

 

            int a,b,c, sum =0;            c = StdRandom.uniform(0,1000);          for (int i = 1; i <= reps; i++) {

                  a=StdRandom.uniform(1000);       // get two random ints from 0 to 9999               b=StdRandom.uniform(1000);

                  c = a+b;                         // add two ints

                  sum = sum + c;

                  sum = sum % 12345;               // don't let sum get too big

            }

            return sum;

      }

 

      public static int addThreeIntsNtimes(int reps) {

 

            int a,b,c, sum =0;            c = StdRandom.uniform(0,1000);            for (int i = 1; i <= reps; i++) {                   a=StdRandom.uniform(1000);      // get two random ints from 0 to 9999                  b=StdRandom.uniform(1000);

                  c = b + a + c;                  // add  three ints

                  sum = sum + c;

                    sum = sum % 12345;           // don't let sum get too big

            }

            return sum;

      }

      public static void main(String[] args)  {

 

            int result1, result2, reps;

            double time1,time2, diff;

 

            reps = 10000;                          // number of repetitions  

            Stopwatch timer1 = new Stopwatch();                         result1 = addTwoIntsNtimes(reps);                         time1 = timer1.elapsedTime()/reps;     // Q1.  average time to ________ ?

 

            Stopwatch timer2 = new Stopwatch();             result2 = addThreeIntsNtimes(reps);           time2 = timer2.elapsedTime()/reps;    // Q2. average time to________ ?

            diff = (time2-time1);                 // Q3. average time to _________?

 

            StdOut.format(" time value:  %e \n", diff);

      }

}

 

 

 

 

 

Vers 1.0 

                                                                                -     Over -  

More products