Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Reversing an Array

Reversing an Array
Write a program that prompts the user for an integer, then asks the user to enter that many values. Store these values in an
array and print the array. Then reverse the array elements so that the first element becomes the last element, the second
element becomes the second to last element, and so on, with the old last element now first. Do not just reverse the order in
which they are printed; actually change the way they are stored in the array. Do not create a second array; just rearrange the
elements within the array you have. (Hint: Swap elements that need to change places.) When the elements have been
reversed, print the array again.

My program compiles and run but, I need someone to check this to see if it is right. Make sure that I have everything that it needs. Thanks

//****************************************************************
// ReverseArray.java
//
//
// Program that prompts the user for an integer, enter many value
// prints out the array, reverses the array then outputs result
//****************************************************************
import java.util.Scanner;

public class ReverseArray {

public static void main(String[] args) {
int size, temp;

Scanner scan = new Scanner(System.in);

System.out.print("Enter the size of your array: ");
size = scan.nextInt();


int[] myArray = new int[size];
// input array of ints

for(int i = 0; i< myArray.length; i++){
System.out.print("Enter number " + i+": ");
myArray[i] = scan.nextInt();
}
// print original array
printArray(myArray);

//reverse elements in array using swap technique
for(int j = 0; j<(myArray.length/2); j++){
temp = myArray[j];
myArray[j] = myArray[myArray.length-1-j];
myArray[myArray.length-1-j] = temp;
}
// print reversed array
printArray(myArray);
}


// printing an int array

public static void printArray(int[] a){

for(int i =0; i<a.length;i++)
System.out.print(a[i]+"\t");
System.out.println();
}
}
[2255 byte] By [Tmcclain] at [2007-11-11 10:00:23]
# 1 Re: Reversing an Array
You can't test it yourself? Just run the program, input number 1-10 in order. Should be fairly simple to see if it works or not.
Phaelax at 2007-11-11 22:32:09 >
# 2 Re: Reversing an Array
It compiles and run, but I just wanted to make sure that I have everything that the program was asking for that's all.
Tmcclain at 2007-11-11 22:33:09 >
# 3 Re: Reversing an Array
Can you think of a way to do your reversing using a while loop?
nspils at 2007-11-11 22:34:07 >
# 4 Re: Reversing an Array
As programmer, first you need is confidence. If you are not confident enough to say that your code will work fine, then nobody can help you..
Try doing a lot of programs like this..that will give enough confidence..
By the way, why can't you try reversing without using a variable temp?
sudheerprem at 2007-11-11 22:35:07 >
# 5 Re: Reversing an Array
You can use a stack if you just only need to reverse elements in this array
lenleo at 2007-11-11 22:36:06 >
# 6 Re: Reversing an Array
j2se library has already done for you.

public class ReverseArray
{

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
size = scanner.nextInt();

List result = new LinkedList();
for(int i = 0; i< myArray.length; i++)
{
result.add(Integer.valueOf(scan.nextInt()));
}
Collections.reverse(result);
printIntergers(result.toArray());
}
}

simple!
wason at 2007-11-11 22:37:16 >