Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help I am confused on what to do Situation #1. For this first situation, we are doing the work with you. You simply have

Please help I am confused on what to do

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Situation #1. For this first situation, we are doing the work with you. You simply have to follow and copy our steps, so that you learn and are ready to take on Situation #2 more on your own. You are given an array A of non-negative integers and your job is to identify the maximum value of the product of two distinct numbers in A So for instance, if A = (4.2.7.1.9), the maximum value of the product of two of A's distinct values is 63. We share with you two approaches to solving this problem. We provide you with the code of each method below: /* input A: * is non empty and contains only non-negative values */ public static int MaxPairwiseProductl(int[] A) { int maxProduct = 0; for (int i=0; i maxProduct) maxProduct = A[i]*A[j]; } } return max Product; /* input A: is non empty and contains only non-negative values */ public static int MaxPairwise Product2(int[] A) { int indexMax = 0; // index of max value in A int indexMax2 = 0:// index of second max value in A for (int i=1; i A[indexMax]) indexMax = i; } for (int i=1;i A[indexMax2]) indexMax2 = 1; } return A[indexMax)*A[indexMax2): Now, how do you take these codes and figure out how many steps each take? You may think about timing their execution. Not a bad guess. However, the time reported per execution may be influenced by other processes running on your computer. So we are aiming for a more universal quantity, one that is not influenced by when or where or on which machine you run your code. Here is how we do it See below, we are going to modify our code so that we track the number of steps that are executed and we return them (rather than the result, which we now will print out). Here is the modified code 1: /* input A: * is non empty and contains only non-negative values */ public static int MaxPairwise ProductiwithNumSteps(int[] A) { int numSteps = 0:// we declare and initialize our number of steps. int maxProduct = 0; numSteps++://for the declaration of maxProduct as a step numSteps+=2; // for the declaration of i and checking the condition i maxProduct if (A[i]*A[j] > maxProduct) { maxProduct = A[i]*Aljl: numSteps++; // for the update of maxProduct } numSteps += 2: // for the update of j checking the condition j A[indexMax] If [A[l]> A[indexMax]] { IndexMax- numSteps++; // for the update of indexMax } numSteps += 2: // for the update of I and checking the condition Alength ) condition i Alength numSteps += 2: // for the declaration of and checking the for (int -1; A.length; i++){ numSteps++; // for the condition (1 !- IndexMax && A[i]> AlindexMax]) (1 !- IndexMax && All > AfindexMax2] IndexMax2 - numSteps++://for the update of IndexMax 1 numSteps += 2: // for the update of land checking the condition iA.length ) System.out.println(A[indexMax]*AlindexMax2]): return numSteps: Note: the increment of numSteps for checking condition (i != indexMax && A[i]> A[indexMax]) is not exactly 1 but that's the approximation we are using here, for simplicity. Ok, we have these codes, what do we do next? We need to run these codes on a number of inputs of varying sizes. Here is what we ask you to do. For each of the above code, do the following: Run your code on 3 randomly generated arrays of size 3: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 10 randomly generated arrays of size 10: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 50 randomly generated arrays of size 50: . For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 100 randomly generated arrays of size 100: For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 200 randomly generated arrays of size 200: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 500 randomly generated arrays of size 500: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 1000 randomly generated arrays of size 1000: For each run, collect the number of steps. o Report the average in a table like the one shown below. Note: the number of executions is not scientifically informed: it is just for now for you to practice generating input and running inputs. Table that you have to fill out: Size of input array MaxPairwise ProductlwithNumSt MaxPairwise Product2with Nums eps teps 3 10 50 100 200 500 1000 Ok, next, how do we run these tests? Let's look at some code: you can do it otherwise, but the code we provide is one way to do it. public static int[] costMaxPairwise Producti() { // one int per cost, one average cost per size (3, 10, 50, 100, 200, 500, 1000) int[] sizes = {3, 10, 50, 100, 200, 500, 1000): int[] averages = new int[7]: for (int i=0; i maxProduct) maproduct AlleAlik return maxProduct Pinput A is non empty and contains only non-negative values * public static int MaxParwideroduct2|in| ALL int indexMax = 0:// index of max value in A int index2 - Q // index of second max value in forint it length: 1+ All > AlindexMaxi) indexMax for line it iAjength++ indexMax 58 Al>Ainde 2 index return AlindesMax "AlndexM2 Now, how do you take these codes and figure out how many steps each take? You may think about timing their execution Not a bad guess. However, the time reported per execution may be influenced by other processes running on your computer So we wsiming for a more universal quantity, one that is not influenced by when or where or on which machine you run your codie Here is how we do it See below, we are going to modify our code so that we track the number of steps that are executed and we return them rather than the result which we now will print out) Here is the modified code t * input A: is non empty and contains only non-negative values */ public static int MaxPairwise ProductiwithNumSteps(int[] A) { int numSteps = 0; // we declare and initialize our number of steps. int maxProduct = 0; numSteps++; // for the declaration of maxProduct as a step numSteps+=2; // for the declaration of i and checking the condition i maxProduct if (A[i]*Aljl> maxProduct) { maxProduct = A[i]*A[j]: numSteps++; // for the update of maxProduct ) numSteps += 2: // for the update of j checking the condition i System.out.println(maxProduct); return numSteps: Similarly, here is the modified code 2 public static int MaxPairwise Product2with NumSteps fint All! Int numSteps 0:// we declare and initialize our number of steps int indexMax -0. // Index of max value in A int indexMax2 - 0, // index of second max value in A numSteps += 2. // for the declaration of indexMax and indexMax2 condition Ajength numSteps +2. // for the declaration of and checking the for fint bet: Ajength: 14+) numStepser. Il for the condition Ali>AindexMax! All > Alindex tall Index Max numSteps. Il for the update of indexMax > numSteps 2 // for the update of i and checking the condition Alength ) numSteps +2.// for the declaration of and checking the condition Alength for (int int length; i+){ numSeps. Il for the condition - IndexMax & All AfindexMax]] indexlax 88 AD]> AfindexMax2] IndexMax2 - numStepsell for the update of indexMax numSteps +=2 // for the update of i and checking the condition kA length System.out.printin AfindexMax]"AindexMax2] return rumSteps Note the increment of numSteps for checking condition indexMax 83 All > AfindexMax]] is not exactly 1 but that's the approximation we are using here for simplicity Ok, we have these codes what do we do next? We need to run these codes on a number of inputs of varying size Here is what we ask you to do For each of the above code, do the following Run your code on randomly generated any of size . For each collect the number of steps Report the average in a table the one shown below .Run your code on omandomly generated ways of size . for each collect the number of steps Report the average in a tablete the one shown below Run your code on randomly generated array of 50 for each collect the number of steps Report the average in a bike the one shown below . Run your code on 100 randomly generated array of 100 . for each nan collect the number of steps Report the average in a table de the one shown below Run your code on 200 domly generated any of 200 . For each on collect the number of steps a Report the average in a tablete the one shown below Run your code on 500 randomly generated ways of size 500 for each un collect the number of steps . Report the average na tablete the one shown below . Run your code on 1000 randomly generated arthy of 1000 For each collect the number of steps Report the average in a ble like the one shown below More the number of execution nocentically infomed in forrow for you to practice generating input and grous Table that you have to out size of trout any Mustehoductiwit Nume Mwoductions cps 100 200 500 1000 Ok, next, how do we run these ters? Let's look at some code you can do otherwise, but the code we provide is one way to do public static coaroduct Il one is percond one werage cost per les 50,400,200.500, 1000) Insies 310.50.200.200.500, 1000) for inti kelengthi++) average-coloanwoductives System.out peintel" Average for size* *sized+**** svetaget return averages public static int cost MaxPairwise Productlint size) ( // we will run MaxPairwiseProduction size different arrays of size size called input int input-new int size): int average = 0; for (int =0; i-size; i++* input = randombtArray(size): average += MaxPairwise Productiwith NumSteps input): return average /size: public static int[] costMaxPairwise Product2016 // one int per cost, one average cost per size 13. 10.50, 100, 200, 500, 1000) int[] sizes = {3, 10, 50, 100, 200, 500, 1000) int[] averages = new int[71 for (int i=0; iesizes.length; i++) { averagestil = costMaxPairwise Product&sizestill System.outprintln("Average for size + sizes[i]+"is + averages till: 1 return averages public static int costMaxPairwise Product2fint size){ // we will run MaxPairwise Product2 on size different arrays of size size called input int/) input = new int[size) Int average = 0; for (int i=0; i-size; i++){ input = randomintArraytsize) average += MaxPairwise Product2with NumStepstinput): return average /size: public static int[] randomlntArray(int size) { Random rnd = new Random(); rnd.setSeed[System.current TimeMills)): int[] result = new int[size): for (int i=0; i return result: Now, you simply have to run costMaxPairwise Productk): costMaxPairwiseProduct2) And capture the results in the table given to you. From there, you are to guess the behavior of your function of the size of the input: is it linear? (somehow f(n) = an + b) Is it quadratic? (somehow f(n) = an? + bn+C). Explain. Situation 2 For this second situation, we are going to get you started but you will have to complete the work yourself this time. You are given a String strand you need to modify it as described in Problem 5 of Gradescope HW that was due on 02/04. We provide you with 2 pieces of code both terative approaches as shown below public static String removertering intenay-new intengt String news int sum- int increment for jint i=0; i stringiti sum-sum increment) boolean adidinde tot true for linti festen - for lint - 0.1 kg video 1 WeddindexON cha cuentasche adidintre retum new and public statie String removelystery219eing set Sting result int todieRemover intcount= // this for todo looks #every charge and decides whether to copy it in reset or not for liniste // decide whether to copychain result Stokemoved But start che toomoved telemoved court return result Your job is the following: . For each of the above methods, you have to add a variable numSteps and insert it into the code so as to count the number of steps each execution of the given method performs. You will create two new methods: removeMysterylWith NumSteps and removeMystery WithNumSteps. You have to run experiments so as to compute the number of steps for several strings as shown below and fill out the table below: Table that you have to fill out: Input string removeMysterylwithN removeMystery2withN umSteps umSteps "abcder "abcdefghijklmnopqrstuvwxyz "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq rstuvwxyz" What are you asked to do? You are asked to do the following: For Situation #1: Turn in your java file: Situationl.java. o Follow instructions and fill in the given table with results of your experiments Conclude on the type of cost function of n for each method: MaxPairwise Product and MaxPairwise Product2 For Situation #2 o Turn in your java file: Situation2.java. o Write code as instructed. o Follow instructions and fill in the given table with results of your experiments Conclude on the type of cost function of n for each method: removeMystery WithNumSteps and removeMystery2WithNumSteps Situation #1. For this first situation, we are doing the work with you. You simply have to follow and copy our steps, so that you learn and are ready to take on Situation #2 more on your own. You are given an array A of non-negative integers and your job is to identify the maximum value of the product of two distinct numbers in A So for instance, if A = (4.2.7.1.9), the maximum value of the product of two of A's distinct values is 63. We share with you two approaches to solving this problem. We provide you with the code of each method below: /* input A: * is non empty and contains only non-negative values */ public static int MaxPairwiseProductl(int[] A) { int maxProduct = 0; for (int i=0; i maxProduct) maxProduct = A[i]*A[j]; } } return max Product; /* input A: is non empty and contains only non-negative values */ public static int MaxPairwise Product2(int[] A) { int indexMax = 0; // index of max value in A int indexMax2 = 0:// index of second max value in A for (int i=1; i A[indexMax]) indexMax = i; } for (int i=1;i A[indexMax2]) indexMax2 = 1; } return A[indexMax)*A[indexMax2): Now, how do you take these codes and figure out how many steps each take? You may think about timing their execution. Not a bad guess. However, the time reported per execution may be influenced by other processes running on your computer. So we are aiming for a more universal quantity, one that is not influenced by when or where or on which machine you run your code. Here is how we do it See below, we are going to modify our code so that we track the number of steps that are executed and we return them (rather than the result, which we now will print out). Here is the modified code 1: /* input A: * is non empty and contains only non-negative values */ public static int MaxPairwise ProductiwithNumSteps(int[] A) { int numSteps = 0:// we declare and initialize our number of steps. int maxProduct = 0; numSteps++://for the declaration of maxProduct as a step numSteps+=2; // for the declaration of i and checking the condition i maxProduct if (A[i]*A[j] > maxProduct) { maxProduct = A[i]*Aljl: numSteps++; // for the update of maxProduct } numSteps += 2: // for the update of j checking the condition j A[indexMax] If [A[l]> A[indexMax]] { IndexMax- numSteps++; // for the update of indexMax } numSteps += 2: // for the update of I and checking the condition Alength ) condition i Alength numSteps += 2: // for the declaration of and checking the for (int -1; A.length; i++){ numSteps++; // for the condition (1 !- IndexMax && A[i]> AlindexMax]) (1 !- IndexMax && All > AfindexMax2] IndexMax2 - numSteps++://for the update of IndexMax 1 numSteps += 2: // for the update of land checking the condition iA.length ) System.out.println(A[indexMax]*AlindexMax2]): return numSteps: Note: the increment of numSteps for checking condition (i != indexMax && A[i]> A[indexMax]) is not exactly 1 but that's the approximation we are using here, for simplicity. Ok, we have these codes, what do we do next? We need to run these codes on a number of inputs of varying sizes. Here is what we ask you to do. For each of the above code, do the following: Run your code on 3 randomly generated arrays of size 3: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 10 randomly generated arrays of size 10: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 50 randomly generated arrays of size 50: . For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 100 randomly generated arrays of size 100: For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 200 randomly generated arrays of size 200: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 500 randomly generated arrays of size 500: For each run, collect the number of steps. Report the average in a table like the one shown below. Run your code on 1000 randomly generated arrays of size 1000: For each run, collect the number of steps. o Report the average in a table like the one shown below. Note: the number of executions is not scientifically informed: it is just for now for you to practice generating input and running inputs. Table that you have to fill out: Size of input array MaxPairwise ProductlwithNumSt MaxPairwise Product2with Nums eps teps 3 10 50 100 200 500 1000 Ok, next, how do we run these tests? Let's look at some code: you can do it otherwise, but the code we provide is one way to do it. public static int[] costMaxPairwise Producti() { // one int per cost, one average cost per size (3, 10, 50, 100, 200, 500, 1000) int[] sizes = {3, 10, 50, 100, 200, 500, 1000): int[] averages = new int[7]: for (int i=0; i maxProduct) maproduct AlleAlik return maxProduct Pinput A is non empty and contains only non-negative values * public static int MaxParwideroduct2|in| ALL int indexMax = 0:// index of max value in A int index2 - Q // index of second max value in forint it length: 1+ All > AlindexMaxi) indexMax for line it iAjength++ indexMax 58 Al>Ainde 2 index return AlindesMax "AlndexM2 Now, how do you take these codes and figure out how many steps each take? You may think about timing their execution Not a bad guess. However, the time reported per execution may be influenced by other processes running on your computer So we wsiming for a more universal quantity, one that is not influenced by when or where or on which machine you run your codie Here is how we do it See below, we are going to modify our code so that we track the number of steps that are executed and we return them rather than the result which we now will print out) Here is the modified code t * input A: is non empty and contains only non-negative values */ public static int MaxPairwise ProductiwithNumSteps(int[] A) { int numSteps = 0; // we declare and initialize our number of steps. int maxProduct = 0; numSteps++; // for the declaration of maxProduct as a step numSteps+=2; // for the declaration of i and checking the condition i maxProduct if (A[i]*Aljl> maxProduct) { maxProduct = A[i]*A[j]: numSteps++; // for the update of maxProduct ) numSteps += 2: // for the update of j checking the condition i System.out.println(maxProduct); return numSteps: Similarly, here is the modified code 2 public static int MaxPairwise Product2with NumSteps fint All! Int numSteps 0:// we declare and initialize our number of steps int indexMax -0. // Index of max value in A int indexMax2 - 0, // index of second max value in A numSteps += 2. // for the declaration of indexMax and indexMax2 condition Ajength numSteps +2. // for the declaration of and checking the for fint bet: Ajength: 14+) numStepser. Il for the condition Ali>AindexMax! All > Alindex tall Index Max numSteps. Il for the update of indexMax > numSteps 2 // for the update of i and checking the condition Alength ) numSteps +2.// for the declaration of and checking the condition Alength for (int int length; i+){ numSeps. Il for the condition - IndexMax & All AfindexMax]] indexlax 88 AD]> AfindexMax2] IndexMax2 - numStepsell for the update of indexMax numSteps +=2 // for the update of i and checking the condition kA length System.out.printin AfindexMax]"AindexMax2] return rumSteps Note the increment of numSteps for checking condition indexMax 83 All > AfindexMax]] is not exactly 1 but that's the approximation we are using here for simplicity Ok, we have these codes what do we do next? We need to run these codes on a number of inputs of varying size Here is what we ask you to do For each of the above code, do the following Run your code on randomly generated any of size . For each collect the number of steps Report the average in a table the one shown below .Run your code on omandomly generated ways of size . for each collect the number of steps Report the average in a tablete the one shown below Run your code on randomly generated array of 50 for each collect the number of steps Report the average in a bike the one shown below . Run your code on 100 randomly generated array of 100 . for each nan collect the number of steps Report the average in a table de the one shown below Run your code on 200 domly generated any of 200 . For each on collect the number of steps a Report the average in a tablete the one shown below Run your code on 500 randomly generated ways of size 500 for each un collect the number of steps . Report the average na tablete the one shown below . Run your code on 1000 randomly generated arthy of 1000 For each collect the number of steps Report the average in a ble like the one shown below More the number of execution nocentically infomed in forrow for you to practice generating input and grous Table that you have to out size of trout any Mustehoductiwit Nume Mwoductions cps 100 200 500 1000 Ok, next, how do we run these ters? Let's look at some code you can do otherwise, but the code we provide is one way to do public static coaroduct Il one is percond one werage cost per les 50,400,200.500, 1000) Insies 310.50.200.200.500, 1000) for inti kelengthi++) average-coloanwoductives System.out peintel" Average for size* *sized+**** svetaget return averages public static int cost MaxPairwise Productlint size) ( // we will run MaxPairwiseProduction size different arrays of size size called input int input-new int size): int average = 0; for (int =0; i-size; i++* input = randombtArray(size): average += MaxPairwise Productiwith NumSteps input): return average /size: public static int[] costMaxPairwise Product2016 // one int per cost, one average cost per size 13. 10.50, 100, 200, 500, 1000) int[] sizes = {3, 10, 50, 100, 200, 500, 1000) int[] averages = new int[71 for (int i=0; iesizes.length; i++) { averagestil = costMaxPairwise Product&sizestill System.outprintln("Average for size + sizes[i]+"is + averages till: 1 return averages public static int costMaxPairwise Product2fint size){ // we will run MaxPairwise Product2 on size different arrays of size size called input int/) input = new int[size) Int average = 0; for (int i=0; i-size; i++){ input = randomintArraytsize) average += MaxPairwise Product2with NumStepstinput): return average /size: public static int[] randomlntArray(int size) { Random rnd = new Random(); rnd.setSeed[System.current TimeMills)): int[] result = new int[size): for (int i=0; i return result: Now, you simply have to run costMaxPairwise Productk): costMaxPairwiseProduct2) And capture the results in the table given to you. From there, you are to guess the behavior of your function of the size of the input: is it linear? (somehow f(n) = an + b) Is it quadratic? (somehow f(n) = an? + bn+C). Explain. Situation 2 For this second situation, we are going to get you started but you will have to complete the work yourself this time. You are given a String strand you need to modify it as described in Problem 5 of Gradescope HW that was due on 02/04. We provide you with 2 pieces of code both terative approaches as shown below public static String removertering intenay-new intengt String news int sum- int increment for jint i=0; i stringiti sum-sum increment) boolean adidinde tot true for linti festen - for lint - 0.1 kg video 1 WeddindexON cha cuentasche adidintre retum new and public statie String removelystery219eing set Sting result int todieRemover intcount= // this for todo looks #every charge and decides whether to copy it in reset or not for liniste // decide whether to copychain result Stokemoved But start che toomoved telemoved court return result Your job is the following: . For each of the above methods, you have to add a variable numSteps and insert it into the code so as to count the number of steps each execution of the given method performs. You will create two new methods: removeMysterylWith NumSteps and removeMystery WithNumSteps. You have to run experiments so as to compute the number of steps for several strings as shown below and fill out the table below: Table that you have to fill out: Input string removeMysterylwithN removeMystery2withN umSteps umSteps "abcder "abcdefghijklmnopqrstuvwxyz "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq rstuvwxyz" What are you asked to do? You are asked to do the following: For Situation #1: Turn in your java file: Situationl.java. o Follow instructions and fill in the given table with results of your experiments Conclude on the type of cost function of n for each method: MaxPairwise Product and MaxPairwise Product2 For Situation #2 o Turn in your java file: Situation2.java. o Write code as instructed. o Follow instructions and fill in the given table with results of your experiments Conclude on the type of cost function of n for each method: removeMystery WithNumSteps and removeMystery2WithNumSteps

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algorithmic Trading Navigating The Digital Frontier

Authors: Alex Thompson

1st Edition

B0CHXR6CXX, 979-8223284987

More Books

Students also viewed these Databases questions