#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Help with Array please!


    Hey guys I need some help getting this to run correctly. It calls the method when the user presses the number but it does not do anything it looks like. just prints a bunch of zeros. The reset method works almost right it just doesn't space the numbers out.

    public void reset(int a[]) - Initialize the array to contain the values from 0 to ARRAY_SIZE - 1, in array positions 0 to ARRAY_SIZE - 1, i.e., array position 3 has the value 3 and so on.

    void square(int a[]) - Square every value in the array, as illustrated below.

    void halve(int a[]) - Halve every value in the array, as illustrated below.

    void accumulate(int a[]) - For every array index N, set the value at array index N to be the sum of the values from array indexes 0 through N, as illustrated below. For instance, a[3] should be set to a[0] + a[1] + a[2] + a[3].

    void transpose(int a[]) - For every pair of neighbors in the array, switch their values. That is, switch the value of a[0] with that of a[1], and a[2] with a[3], etc.

    void shift(int a[]) - Move every value in the array one position to the left, e.g., the value of a[4] is moved to a[3]. Move the value of the first index to the last index, e.g., a[0] is moved to a[19] for an array of size 20.

    void reverse(int a[]) - Reverse the values in the array, e.g., in an array of size 20 switch a[0] with a[19], a[1] with a[18], etc.




    Any help would be greatly appreciated.
    Sorry for long post but the green is what the methods are supposed to do.

    Code:
    package program6;
    
    import java.util.Scanner;
    
    public class Program6 {
    
        public static final int ARRAY_SIZE = 20;
    
        public static void main(String[] args) {
            int userAnswer;
            int array[];
            array = new int[ARRAY_SIZE];
            Scanner keyboard = new Scanner(System.in);
            System.out.println("The array is:");
            reset(array);
            System.out.println("");
            System.out.println("What would you like to do?");
            System.out.println("1) square\n2) halve\n3) accumulate\n"
                    + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
            System.out.print("?:");
            userAnswer = keyboard.nextInt();
            while((userAnswer !=0) || (userAnswer>7)){
            if (userAnswer == 1) {
                System.out.println("The array is:");
                square(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else if (userAnswer==2){
                System.out.println("The array is:");
                halve(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else if(userAnswer==3){
                System.out.println("The array is:");
                accumulate(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else if(userAnswer==4){
                System.out.println("The array is:");
                transpose(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else if(userAnswer==5){
                System.out.println("The array is:");
                shift(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else if(userAnswer==6){
                System.out.println("The array is:");
                reverse(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else if(userAnswer==7){
                System.out.println("The array is:");
                reset(array);
                System.out.println("");
                System.out.println("What would you like to do?");
                System.out.println("1) square\n2) halve\n3) accumulate\n"
                        + "4) transpose\n5) shift\n6) reverse\n7) reset\n0) Quit");
                System.out.print("?:");
            }else  if(userAnswer==0){
                System.exit(1);
            }
    
        }
        }
    
        public static void reset(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = 0; i < ARRAY_SIZE; i++) {
                array[i] = i;
                System.out.print(array[i]);
            }
        }
    
        public static void square(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = 0; i < ARRAY_SIZE; i++) {
                array[i] = array[i] * array[i];
                System.out.print(array[i] * array[i]);
            
           
            }
        }
    
      public static void halve(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = 0; i < ARRAY_SIZE; i++) {
                array[i] = array[i] / 2;
                System.out.print(array[i]);
            }
        }
    
       public static void accumulate(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = 0; i < ARRAY_SIZE; i++) {
                array[i] += array[i];
                System.out.print(array[i]);
            }
        }
    
        public static void transpose(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = 0; i < ARRAY_SIZE - 1; i += 2) {
                int temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = array[i];
                System.out.print(array[i]);
            }
        }
    
        public static void shift(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = ARRAY_SIZE - 2; i >= 0; i--) {
                int temp = array[i];
                array[i + 1] = array[i];
                System.out.print(array[i]);
            }
        }
    
        public static void reverse(int a[]) {
            int array[];
            array = new int[ARRAY_SIZE];
            for (int i = 0; i < ARRAY_SIZE; i++) {
                array[i] = array[ARRAY_SIZE - 1 - i];
                System.out.print(array[i]);
            }
        }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Here is my advice. You have many methods there. Some of them may be working. Some of them may not work. Test them all individually to see if they work. If you find one that doesn't work, then try to fix it. If you need help fixing that method then come back here and ask for help with that method only. Once you get one method working move onto the next.

    How do you test the methods? You know what input they are expecting and you know what output you are expecting. Print the input and output to the console to make sure you are getting what you should be.

    If you do this, then we can eliminate the code in the methods as bugs.

    It's never a good idea to write all your code at once, click run and pray that it all works. You should test your code as you write it to make sure that it is working. That way you have a better idea of when something went wrong and where the problem may be.

    Comments on this post

    • NormR agrees : Nice post.
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,575
    Rep Power
    1906
    You are creating a new array in each function, why?
    You never use the parameter a[]!

IMN logo majestic logo threadwatch logo seochat tools logo