-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayReverser.java
More file actions
48 lines (38 loc) · 1.22 KB
/
arrayReverser.java
File metadata and controls
48 lines (38 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
# reverse an integer array inputed by the user
steps:
-> pass the original array (pass by reference) to a reverser function with void return type
-> the function will swap 1st half of the array with last half of the array using a for loop
-> print out the original function from the main function
*/
import java.util.*;
public class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.print ("Enter array length: ");
int length = sc.nextInt();
// initializing array
int[] array = new int [length];
// taking array elements input
System.out.print ("Enter array elements: ");
for (int i = 0; i < length; i++)
array [i] = sc.nextInt();
// reversing array
reverse (array);
// printing reversed array
System.out.println (Arrays.toString (array));
}
public static void reverse (int array[])
{
int temp;
int j = array.length - 1;
for (int i = 0; i <= j/2; i++)
{
temp = array [j - i];
array [j - i] = array [i];
array [i] = temp;
}
}
}