Practice 1: Fill in the blanks marked (1) and (2) /* Copy the top half of A into
ID: 3642497 • Letter: P
Question
Practice 1: Fill in the blanks marked (1) and (2)
/* Copy the top half of A into the lower half of B,
* and lower half of A into the upper half of B */
public class SwitchHalves {
public static void main(String[] args){
double[] arrayA = {20, 15, 13.2, 26, 19, 21, 0.897};
double[] arrayB = new double[arrayA.length];
for (int indexA=(int) (arrayA.length/2)+1, // from halfway up A
indexB=0; // from start in B
__________________; // (1) up to end of A
indexA++, indexB++) // indexA and indexB goes up
{
arrayB[indexB] = arrayA[indexA]; // copy over
}
for (int indexA=0, // from bottom of A
______________________________; // (2) into top-half of B
indexA < (arrayA.length/2)+1; // up to end of A
indexA++, indexB++) // indexA and indexB goes up
{
arrayB[indexB] = arrayA[indexA]; // copy over
}
System.out.println("arrayA:");
for (double content: arrayA) {System.out.print(content+",");}
System.out.println(); // To put arrayB on its own line
System.out.println("arrayB:");
for (double content: arrayB) {System.out.print(content+",");}
}
}
Run:
Practice 2: Fill in the blank marked (3)
public class CopyAndScale3While {
public static void main(String[] args){
double[] arrayA = {20, 15, 13.2, 26, 19, 21, 0.897};
double[] arrayB = new double[arrayA.length];
int indexA=0, indexB=0; // Start from beginning
while (indexA < arrayA.length)
{
arrayB[indexB] = arrayA[indexA]; // copy over
_______________; indexB++; // (3) indexA goes up by 3, //indexB goes up
}
System.out.println("Final values: indexA=" + indexA + " indexB=" + indexB);
System.out.println("arrayA:");
for (double content: arrayA) {System.out.print(content+",");}
System.out.println(); // To put arrayB on its own line
System.out.println("arrayB:");
for (double content: arrayB) {System.out.print(content+",");}
}
}
Run:
Practice 3: Fill in the blanks labeled (4) and (5)
/* Copy A to B, but first half, double the values of A */
public class IncreaseHalf {
public static void main(String[] args){
double[] arrayA = {20, 15, 13.2, 26, 19, 21, 0.897};
double[] arrayB = new double[arrayA.length];
for (int indexA = 0, // from beginning of A
indexB = 0; // from beginning of B
___________________; // (4) up to end of A
indexA++, indexB++) // indexA goes up, indexB goes up
{
if (indexA < arrayA.length/2)
______________________________; //(5)copy over doubled
else
arrayB[indexB] = arrayA[indexA]; // copy over
}
System.out.println("arrayA:");
for (double content: arrayA) {System.out.print(content+",");}
System.out.println(); // To put arrayB on its own line
System.out.println("arrayB:");
for (double content: arrayB) {System.out.print(content+",");}
}
}
Run:
Explanation / Answer
please rate - thanks
/* Copy the top half of A into the lower half of B,
* and lower half of A into the upper half of B */
public class SwitchHalves {
public static void main(String[] args){
double[] arrayA = {20, 15, 13.2, 26, 19, 21, 0.897};
double[] arrayB = new double[arrayA.length];
for (int indexA=(int) (arrayA.length/2)+1, // from halfway up A
indexB=0; // from start in B
indexA<arrayA.length; // (1) up to end of A
indexA++, indexB++) // indexA and indexB goes up
{
arrayB[indexB] = arrayA[indexA]; // copy over
}
for (int indexA=0, // from bottom of A
indexB=(int) (arrayB.length/2); // (2) into top-half of B
indexA < (arrayA.length/2)+1; // up to end of A
indexA++, indexB++) // indexA and indexB goes up
{
arrayB[indexB] = arrayA[indexA]; // copy over
}
System.out.println("arrayA:");
for (double content: arrayA) {System.out.print(content+",");}
System.out.println(); // To put arrayB on its own line
System.out.println("arrayB:");
for (double content: arrayB) {System.out.print(content+",");}
}
}
this wasn't clear what you wanted, just said indexA goes up 3
public class CopyAndScale3While {
public static void main(String[] args){
double[] arrayA = {20, 15, 13.2, 26, 19, 21, 0.897};
double[] arrayB = new double[arrayA.length];
int indexA=0, indexB=0; // Start from beginning
while (indexA < arrayA.length)
{
arrayB[indexB] = arrayA[indexA]; // copy over
indexA+=3; indexB++; // (3) indexA goes up by 3, //indexB goes up
}
System.out.println("Final values: indexA=" + indexA + " indexB=" + indexB);
System.out.println("arrayA:");
for (double content: arrayA) {System.out.print(content+",");}
System.out.println(); // To put arrayB on its own line
System.out.println("arrayB:");
for (double content: arrayB) {System.out.print(content+",");}
}
}
/* Copy A to B, but first half, double the values of A */
public class IncreaseHalf {
public static void main(String[] args){
double[] arrayA = {20, 15, 13.2, 26, 19, 21, 0.897};
double[] arrayB = new double[arrayA.length];
for (int indexA = 0, // from beginning of A
indexB = 0; // from beginning of B
indexA<arrayA.length; // (4) up to end of A
indexA++, indexB++) // indexA goes up, indexB goes up
{
if (indexA < arrayA.length/2)
arrayB[indexB] = arrayA[indexA]*2; //(5)copy over doubled
else
arrayB[indexB] = arrayA[indexA]; // copy over
}
System.out.println("arrayA:");
for (double content: arrayA) {System.out.print(content+",");}
System.out.println(); // To put arrayB on its own line
System.out.println("arrayB:");
for (double content: arrayB) {System.out.print(content+",");}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.