Problem:
Implement the array reversal algorithm that starts out with two indices, i=0 and j = n+1. With each iteration i is increased and j is decreased for i<j
Solution:
package array.programs;
import java.util.Scanner;
public class ArrayOrderReversal {
int number;
int[] numberArray;
Scanner scanner;
public static void main(String[] args) {
ArrayOrderReversal arrayOrderReversal = new ArrayOrderReversal();
arrayOrderReversal.getTheNumbers();
System.out.println("Before reversal");
arrayOrderReversal.printTheArray();
arrayOrderReversal.reverseTheOrder();
System.out.println("\n After reversal");
arrayOrderReversal.printTheArray();
}
public void getTheNumbers(){
scanner = new Scanner(System.in);
System.out.println("enter how many numbers");
number = scanner.nextInt();
numberArray = new int[number];
initializeTheNumbers();
scanner.close();
}
public void initializeTheNumbers(){
for(int i = 0; i < number; i++){
System.out.println("enter the number");
numberArray[i] = scanner.nextInt();
}
}
public void reverseTheOrder(){
int temp = 0;
int i = 0;
int j = number + 1;
for (; i < j; i++, j--) {
temp = numberArray[i];
numberArray[i] = numberArray[number - i - 1];
numberArray[number - i - 1 ] = temp;
}
}
public void printTheArray(){
for(int i = 0; i< number; i++){
System.out.print(numberArray[i] + " ");
}
}
}
Output:
enter how many numbers
7
enter the number
1
enter the number
2
enter the number
3
enter the number
4
enter the number
5
enter the number
6
enter the number
7
Before reversal
1 2 3 4 5 6 7
After reversal
7 6 5 4 3 2 1
Implement the array reversal algorithm that starts out with two indices, i=0 and j = n+1. With each iteration i is increased and j is decreased for i<j
Solution:
package array.programs;
import java.util.Scanner;
public class ArrayOrderReversal {
int number;
int[] numberArray;
Scanner scanner;
public static void main(String[] args) {
ArrayOrderReversal arrayOrderReversal = new ArrayOrderReversal();
arrayOrderReversal.getTheNumbers();
System.out.println("Before reversal");
arrayOrderReversal.printTheArray();
arrayOrderReversal.reverseTheOrder();
System.out.println("\n After reversal");
arrayOrderReversal.printTheArray();
}
public void getTheNumbers(){
scanner = new Scanner(System.in);
System.out.println("enter how many numbers");
number = scanner.nextInt();
numberArray = new int[number];
initializeTheNumbers();
scanner.close();
}
public void initializeTheNumbers(){
for(int i = 0; i < number; i++){
System.out.println("enter the number");
numberArray[i] = scanner.nextInt();
}
}
public void reverseTheOrder(){
int temp = 0;
int i = 0;
int j = number + 1;
for (; i < j; i++, j--) {
temp = numberArray[i];
numberArray[i] = numberArray[number - i - 1];
numberArray[number - i - 1 ] = temp;
}
}
public void printTheArray(){
for(int i = 0; i< number; i++){
System.out.print(numberArray[i] + " ");
}
}
}
Output:
enter how many numbers
7
enter the number
1
enter the number
2
enter the number
3
enter the number
4
enter the number
5
enter the number
6
enter the number
7
Before reversal
1 2 3 4 5 6 7
After reversal
7 6 5 4 3 2 1
No comments:
Post a Comment