0

In this problem set, you have to ask the user to enter 10 values, then cube the elements in that entered array and in the end print out the original and final array

This question is not about printing an array, my concerns is how to cube all elements in an array. I tried doing this as shown below but I keep on getting random stuff in output?

public class arraysprob4 {
    //raahul marreddy
    // have user enter 10 numbers
    // find and print the cube's

    static Scanner console = new Scanner(System.in);

    public static void main(String[]args) {
        int newcube[] = new int[10];
        int cube[] = new int[newcube.length];
        System.out.println("Enter 10 numbers:");

        for(int i = 0; i < cube.length; i++) {
            cube[i] = console.nextInt();
            newcube[i] = cube[i] * cube[i] * cube[i];
        }

        System.out.println("New Array is:" + newcube);
        System.out.println("Old Array is :" + cube);
   }
}
Marr
  • 1
  • 1
  • This is not a duplicate, read carefully please Alexis, i do not know how to cube elements, has nothing to do with printing arrays – Marr Nov 30 '15 at 00:18
  • 1
    This _does_ have something to do with printing arrays, as `System.out.println("New Array is:" + newcube)` will not print the contents of the array as you expect. It will instead print the array's reference. Your cube code is probably working (it looks correct to me). – Simon MᶜKenzie Nov 30 '15 at 00:45
  • Well when I execute it, both arrays have an output with random symbols, but other than that it works fine, it just that error that I can't figure out. – Marr Nov 30 '15 at 01:42
  • So use `System.out.println("New Array is:" + Arrays.toString(newcube))` as per [the accepted answer to the linked duplicate](http://stackoverflow.com/a/409795/622391). – Simon MᶜKenzie Nov 30 '15 at 01:52

0 Answers0