Description
Write the answers to these problems on paper. Scan the paper and upload to the submissions folder.
We will grade a random subset of these for credit.
1. 1.3.19
2. 1.4.5 3. 1.4.6
(You may assume the list has at least one node).
(show your work)
(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, Q1.
Q2. Q3.
5. Create a java program with this below. (You will need to change Record the value printed in the
Q3 in the comments in the main function.
class and run it using the values in the table the value of the variable reps in the program). 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) {
} }
Vers 1.0
int a,b,c, sum =0;
c = StdRandom.uniform(0,1000); for (int i = 1; i <= reps; i++) {
a=StdRandom.uniform(1000); b=StdRandom.uniform(1000); c = a+b;
sum = sum + c;
public static int addThreeIntsNtimes(int reps) {
} }
} }
sum = sum % 12345; return sum;
// get two random ints from 0 to 9999 // add two ints
// don’t let sum get too big
int a,b,c, sum =0;
c = StdRandom.uniform(0,1000); for (int i = 1; i <= reps; i++) {
a=StdRandom.uniform(1000); b=StdRandom.uniform(1000); c = b + a + c;
sum = sum + c;
// get two random ints from 0 to 9999 // add three ints
// don’t let sum get too big
// number of repetitions
// Q1. average time to ________ ?
// Q2. average time to________ ?
diff = (time2-time1);
StdOut.format(” time value: %e \n”, diff);
sum = sum % 12345; return sum;
public static void main(String[] args) {
int result1, result2, reps; double time1,time2, diff;
reps = 10000;
Stopwatch timer1 = new Stopwatch(); result1 = addTwoIntsNtimes(reps); time1 = timer1.elapsedTime()/reps;
Stopwatch timer2 = new Stopwatch(); result2 = addThreeIntsNtimes(reps); time2 = timer2.elapsedTime()/reps;
– Over-
// Q3. average time to _________?