1. (100 pts) Write a program that swaps the elements of an array pairwise. Start
ID: 645675 • Letter: 1
Question
1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from the left of the array, take 2 elements at a time and swap the two elements. Continue in this fashion until you reach the end of the array. Declare an integer array of size 10 and place random numbers in the range [0?9]. Print the original array, pairwise swap the elements of the array and print the final array after swap. Consider the following example.
0123456789
After pairwise swap you get the following array
0123456789
In the array first two numbers 1 and 7 are swapped, 4 and 0 are swapped, 9 and 4 are swapped, 8 and 8 are swapped and 2 and 4 are swapped. Sample execution of the program is given below
Original array 1740948824
After pairwise swap 7104498842
Explanation / Answer
Please Find the below Program for Swapping two consecutive values in an Array.
The Program has three functions:
1. generateArray() : It generates random number and inserts them in the Array. It is currently using the Math.random() function which generates random Values
2. printArray() : It prints the value of the array.
3. swapArrayValue() : It is having the logic which swaps the values inside the array. Logic is simple, take two values from the array at a time,swap them and insert it back to array and then pick the next two values till the array ends.
Note: Here as the array is of predefined size of 10, we do not need an necessary check if the size of the array is odd. In that case we would keep the last odd place value as it is.
The Program can also directly run. Copy the conten of the files in suppose "X.java". Open Command Prompt and go to the location where the file is kept.
Compile : javac X.java
Run : java X
public class SwapProgram {
int[] randArray = new int[10];
public void generateArray() {
for (int i = 0; i < randArray.length; i++) {
randArray[i] = (int) (Math.random() * 10);
}
}
public void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
public void swapArrayValue() {
int temp;
/*
* Swapping Logic Here the loop will iterate and would take on two
* consecutive values and swap them for e.g : in first run i=0,j=1 ->
* Swap the two values in secound run i=2,j=3 -> Swap the two Values
*/
for (int i = 0, j = 1; i < randArray.length; i += 2, j += 2) {
temp = randArray[i];
randArray[i] = randArray[j];
randArray[j] = temp;
}
}
public static void main(String[] args) {
SwapProgram sp = new SwapProgram();
// Generate Array of Random Integers
sp.generateArray();
// Print the Generated Array
System.out.println("Generated Array:");
sp.printArray(sp.randArray);
// Swap the Array Value
sp.swapArrayValue();
// Print the New Swapped Array
System.out.println(" Swapped Array:");
sp.printArray(sp.randArray);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.