- Mar 19, 2006
- 1,539
- 1
- 81
My friend is having trouble with an arrays assignment. It asks to scan an array and put it in reverse order without creating another array. I already looked at it, and I seriously cannot tell what is wrong; working it out on a piece of paper, the first couple instances should put the array in reverse order. Here is the code. Any help would be greatly appreciated.
Assuming there are 5 integers, being {1, 2, 3, 4, 5}, this is the output:
Top five is the original array, bottom five is the new array. Bottom array SHOULD be the reverse of the top.
Code:
import java.util.Scanner;
public class Reverse
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int number;
int temp;
System.out.println("Bob Saget");
System.out.println("How many integers?");
number = scan.nextInt();
int[] array = new int[number];
int max = array.length - 1;
for(int i = 0; i < array.length; i++)
{
System.out.println("Number " + (i + 1) + " :");
array[i] = scan.nextInt();
}
for(int a = 0; a < array.length; a++)
System.out.println("Number " + (1+a) + " " + array[a]);
for (int b = 0; b < array.length; b++)
{
temp = array[b];
array[b] = array[max];
array[max] = temp;
max--;
}
for(int c = 0; c < array.length; c++)
System.out.println("Number " + (1+c) + " " + array[c] + " ");
}
}
Assuming there are 5 integers, being {1, 2, 3, 4, 5}, this is the output:
Code:
Number 1 1
Number 2 2
Number 3 3
Number 4 4
Number 5 5
Number 1 1
Number 2 2
Number 3 3
Number 4 4
Number 5 5
Top five is the original array, bottom five is the new array. Bottom array SHOULD be the reverse of the top.
