Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}.
import java.util.Scanner;
public class ModifyArray {
public swapArrayEnds(10, 20, 30, 40) {
for (i = 0; i < (arrVals.length / 2); ++i) {
tempValue = arrVals[i]; // Do swap //*please fix code//
arrVals[i] = arrVals[arrVals.length - 1 - i];
arrVals[arrVals.length - 1 - i] = tempValue;
Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}.
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int numElem = 4;
int[] sortArray = new int[numElem];
int i;
int userNum;
for (i = 0; i < sortArray.length; ++i) {
sortArray[i] = scnr.nextInt();
}
swapArrayEnds(sortArray);
for (i = 0; i < sortArray.length; ++i) {
System.out.print(sortArray[i]);
System.out.print(" ");
}
System.out.println("");
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps