Array Question
Author |
Message |
Raza
|
Posted: Wed Mar 11, 2009 7:16 pm Post subject: Array Question |
|
|
code: | //Complete the following program so that it reverses the order of the values in data, then prints it out.
//In the first version of the program there is only one array and its values are reversed with some fairly tricky programming.
import java.io.* ;
class ReverserVersion1
{
public static void main ( String[] args )
{
int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
// reverse the data
for ( int j=0; j < be careful here; j++)
{
}
// write out the new data
for ( int j=0; j < data.length; j++)
{
}
}
} |
This question has given me something of a headache. You're supposed to solve it without creating another array. The closest I've gotten to solving is data[j]=data[13-j]; which gives me (for obvious reasons) a {14,13,12,11,10,9,8,8,9,10,11,12,13,14} array for an answer. I can't find a way around it, unless I were to find the sum of the array and then subtract each number.. or some other ludicrously time-consuming method, but I'm sure there's a quicker solution.
Help? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
|
|
|
|
Raza
|
Posted: Wed Mar 11, 2009 7:37 pm Post subject: RE:Array Question |
|
|
Yes, please elaborate. |
|
|
|
|
|
Tony
|
Posted: Wed Mar 11, 2009 8:17 pm Post subject: RE:Array Question |
|
|
would int[] + int give you enough space to reverse the order of values in the int[] ? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
|
|