implement the three code fragments from written problem 3. Have your code repeat
ID: 3665643 • Letter: I
Question
implement the three code fragments from written problem 3. Have your code repeatedly run each fragment on various values of n. Time each run and see if the progression of timings as n increases matches the predicted run times from your written assignment. Place the results of your timing and your explanation in a file called Problem3.txt. The easiest way to time your run is to execute the following code before each fragment:
then after each fragment place:
The elapsed time is the difference between these two variables.
This is what I have so far, I'm kind of lost and confused what to do from here.
public class Problem3 {
public static void caseA(int n){
int starTime = (int) System.currentTimeMillis();
int sum = 0;
for ( int i = 0; i < 23; i ++)
for ( int j = 0; j < n ; j ++)
sum = sum + 1;
int endTime = (int) System.currentTimeMillis();
System.out.println(endTime- starTime);
for (int i = 0; i < 1000000; i++){
int j = i * 10;
}
for(int i = 1000; i < 1000000; i =10 *i){
caseA(i);
}
}
public static int caseB(int n){
int starTime = (int) System.currentTimeMillis();
int sum =0;
for ( int i = 0; i < n ; i ++)
for ( int k = i ; k < n ; k ++)
sum = sum + 1;
int endTime = (int) System.currentTimeMillis();
System.out.println(endTime- starTime);
}
public static int caseC(int x, int k) {
int starTime = (int) System.currentTimeMillis();
if(x<=k)
return 1;
else
return caseC(x/k,k) + 1;
int endTime = (int) System.currentTimeMillis();
System.out.println(endTime- starTime);
}
public static void main(String[] args) {
}
}
Explanation / Answer
//Please note that, if u using ide or command line, execute this program and enter ur values.
import java.util.Scanner;
public class Problem3 {
public static void caseA(int n){
int sum = 0;
for ( int i = 0; i < 23; i ++)
for ( int j = 0; j < n ; j ++)
sum = sum + 1;
}
public static int caseB(int n){
int sum =0;
for ( int i = 0; i < n ; i ++)
for ( int k = i ; k < n ; k ++)
sum = sum + 1;
return sum;
}
public static int caseC(int x, int k) {
if(x<=k)
return 1;
else
return caseC(x/k,k) + 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter value for x : ");
int x = scanner.nextInt();
System.out.println("Enter value for k : ");
int k = scanner.nextInt();
long startTime;
long endTime;
long timeTakenForCaseA;
long timeTakenForCaseB;
long timeTakenForCaseC = 0L;
for (int i = 0; i < 100000; i++) {
startTime = System.currentTimeMillis();
caseA(i);
endTime = System.currentTimeMillis();
timeTakenForCaseA = endTime - startTime;
startTime = System.currentTimeMillis();
caseB(i);
endTime = System.currentTimeMillis();
timeTakenForCaseB = endTime - startTime;
System.out.println("Time taken for caseA with n = " + i + " is : "
+ (timeTakenForCaseA));
System.out.println("Time taken for caseB with n = " + i+ " is : "
+ (timeTakenForCaseB));
}
//if u want to call c with loop u can put another loop here ,and move this below code with in that //loop as above code.
startTime = System.currentTimeMillis();
caseC(x,k);
endTime = System.currentTimeMillis();
timeTakenForCaseC = endTime - startTime;
System.out.println("Time taken for caseC with x = " + x + " and k= "+k+" is : "
+ (timeTakenForCaseC));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.