The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 4 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Questions about Intro to programming
all the questions based on Java
Homework 5
1. (10 points; 5 points each) Trace the following nested loops using the table on the right. Show every time a variable is changed—including the last change.
a. (Hint: Go SLOWLY, this gets confusing. The lowest value on each column at any time will be the current value of that variable, so don’t get ahead of yourself.)
int[] data = { 1, 3, 8, 2 };
intsum = 0;
intproduct = 0;
for (inti = 0; i<data.length; i++) {
product = data[i] * 2;
for (intj = i; j<i + 1&&j<data.length; j++) {
sum = data[j] + product;
}
|
i |
j |
sum |
product |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
b. (Hint: Remember to include the ending value of counter variables like i and j)
|
i |
j |
product |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int[] data = { 1, 3, 8, 2};
intsum = 0;
intproduct = 0;
for (inti = 0; i<data.length; i++) {
product = data[i] * 2;
for (intj = 0; j< 3; j++) {
sum = data[j] + product;
}
}
2. (10 points; 2 points each)
You have an array of earthquake data for Oklahoma for the last year.
double[] richterValues; // constructed and initialized elsewhere as a Perfect Size Array
Use the methods in the Arrays API to do the following actions. Note: Each of these items is a single line of code that will call one method in the Arrays class. No credit will be given for writing the logic to do this work instead of using the Arrays class method.
a) Sort the values in the richterValues array
b) Store a String representation of the richterValues values into a String called summary.
c) Make a new copy of the first 5 items in the array. (Note: If there are less than 5 items in the richterValues array, leave the new values added as 0)
d) Check to see if the richterValues array is equal to the array “{1, 2, 3}” and store that value into a boolean variable called isSame.
e) Make a new copy richterValues array that contains only the items in index 3 – index 7. (Note: In this case, you may assume that the richterValues array has at least 4 items so there is definitely a value in index 3.)
3. (10 points) Trace the execution of insertion sort, using the algorithm presented in class (no other version will be accepted). Show each data movement on a separate line in the table. The column labeled “Auxiliary” is there to hold the data item that has to be set to the side.
The tables are given a default size, which may be either too big or too small. If it’s too big, delete extra rows. If it is too small, add extra rows.
|
6 |
34 |
86 |
22 |
4 |
6 |
0 |
Auxiliary |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4. (10 points) Trace the execution of selection sort, using the algorithm presented in class (no other version will be accepted). Show each data movement on a separate line in the table. The tables are given a default size, which may be either too big or too small. If it’s too big, delete extra rows. If it is too small, add extra rows.
|
200 |
73 |
505 |
100 |
37 |
12 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Homework 5 (Last Page)
Please place your name in the space provided below. Please do not put any personally identifying information anywhere else on this homework.
(5 point) Student Name: __________________________________________________________
Submit this file to Canvas as a PDF. The title of the file does not matter.
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly