print the reverse of an array

printing reverse of an array of floating point numbers

Page 1 of 1

2 Replies - 22686 Views - Last Post: 27 July 2008 - 12:33 PM Rate Topic: ****- 1 Votes

#1 dfecke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-July 08

print the reverse of an array

Posted 27 July 2008 - 12:08 PM

I am trying to have this program print out an array of preselected floating point numbers then print the array in reverse order then print the smallest value entered.

This is the error that is occuring with current code when run.
need some help on reversing the numbers and finding the smallest number input.

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

*****************************************

/**
 * @(#)arrayrev.java
 *
 * arrayrev application
 *
 * @author 
 * @version 1.00 2008/7/26
 */
 
 public class arrayrev
 	
 {
 
 public static void reverse(double[] array) {
 	
 	
         double [] anArray;              // declares an array of floating point numbers
         anArray = new double[6];      // allocates memory for 6 floating point numbers
            
          anArray[0] = 5.0; // initialize first element
          anArray[1] = 4.4; // initialize second element
          anArray[2] = 1.9; // etc.
          anArray[3] = 2.9;
          anArray[4] = 3.4;
          anArray[5] = 3.5;
 
          //print the original array order
          System.out.println("Element at index 0: " + anArray[0]);
          System.out.println("Element at index 1: " + anArray[1]);
          System.out.println("Element at index 2: " + anArray[2]);
          System.out.println("Element at index 3: " + anArray[3]);
          System.out.println("Element at index 4: " + anArray[4]);
          System.out.println("Element at index 5: " + anArray[5]);
          
          
          // print the reverse of how the numbers were input
         // need help on this.
          
          
          //find  and print the smallest value of the numbers input
          // need help on this
     }
 }


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 27 July 2008 - 12:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: print the reverse of an array

#2 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: print the reverse of an array

Posted 27 July 2008 - 12:11 PM

Please use code tags next time. Also, your problem is easy to solve: you have no main method, so make one.

public static void main(String[] args) {
  // Code that calls your reverse method
}


Was This Post Helpful? 1
  • +
  • -

#8 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: print the reverse of an array

Posted 27 July 2008 - 12:33 PM

In addition to the wonderful advice Jeroen has given, to print the array in reverse order you are going to need to have a loop. It will have to execute a certain number of times, so a for loop would be ideal, and it will have to loop from the highest subscript down to 0.

Something like this should work for you...

// Start from the size of the array (minus 1 to get the highest subscript)
// and go down to zero
for (int i = anArray.length - 1; i >= 0; i--) {
     System.out.println("Index : " + i + " value: " + anArray[i]);
}



That will give you the array in reverse order for printing. If you need to actually adjust the array values, then you will have to do some swapping from the last value to the first value, the second to last value to the second value and so on.

Enjoy!

"At DIC we be reverse array printing code ninjas... some of us live in reverse. I feel like I am getting younger all the time!" :snap:

This post has been edited by Martyr2: 27 July 2008 - 12:34 PM

Was This Post Helpful? 1

Page 1 of 1