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

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    [Homework] Array Help


    I have a couple array problems that I can't for the life of me figure out. I've spent countless hours just getting to the point I'm at and even more hours trying to figure out why these two programs still don't work. I am very new to Java so bear with me...

    General guidelines: In order to accomplish this work as quickly as possible, we will not use keyboard or file input. Instead, you may assign the 10 values to your initial arrays in the programs. Using initializer lists to load them with the data types expected for each exercise. Please use methods to do each task. Pass your arrays to the method to complete each task.

    1. Write a program that takes 10 integers as input. The program places the even integers into an array called evenList, the odd integers into an array called oddList, and the negative integers into an array called negativeList.The program displays the contents of the three arrays after all of the integers have been entered.

    Here is my code:
    Code:
    import java.util.*;
    import java.io.*;
    
    public class arraysproject1
    {
      private static int i = 0;
      private static int number = 0;
    
      public static void main(String[] args) throws IOException
      {
        int[]  intArray = {13, -2, 10, 7, -15, 20, 51, 87, 22, -90};
        sortEvens(intArray);
        sortOdds(intArray);
        sortNegatives(intArray);
      }
    
      public static void sortEvens(int[] intArray)
        {
          System.out.println("The even numbers are: ");
          int[]  evenList = new int[5];
          int even = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] % 2 == 0){
               intArray[i] = evenList[even];
             }
            }
          System.out.println(evenList[even]);
        }
    
      public static void sortOdds(int[] intArray)
        {
          System.out.println("The odd numbers are: ");
          int[]  oddList = new int[5];
          int odd = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] % 2 != 0){
               intArray[i] = oddList[odd];
              }
             }
          System.out.println(oddList[odd]);
        }
    
      public static void sortNegatives(int[] intArray)
        {
          System.out.println("The negative numbers are: ");
          int[]  negativeList = new int[3];
          int negative = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] < 0){
               intArray[i] = negativeList[negative];
              }
             }
          System.out.println(negativeList[negative]);
        }
    }
    And my output:
    The even numbers are: 0
    The odd numbers are: 0
    The negative numbers are: 0

    2.Write a program that randomly inputs 20 numbers from 1 to 10 into a 2-D array of 5 rows and 4 columns. The program should output the 2-D array, the sum of each row storing numbers in a parallel array and the sum of each column storing number in a parallel array.

    Code:
    Code:
    import java.util.*;
    import java.io.*;
    import java.util.Random;
    
    public class arraysproject3
    {
      private static int i = 0;
      private static int j = 0;
    
      public static void main(String[]args)
      {
        int[][] randArray = new int[5][4];
        Random randomizer = new Random();
        for (i = 0; i < randArray.length; i++){
          for (j = 0; j < randArray[i].length; j++){
            randArray[i][j] = randomizer.nextInt(11);
          }
        }
        print(randArray);
        sumRows(randArray);
        sumCols(randArray);
      }
      public static void print(int[][] randArray)
      {
        System.out.println("Elements in 2D Array:");
        for (i = 0; i < randArray.length; i++){
          for (j = 0; j < randArray[i].length; j++){
            System.out.println(randArray[i][j]);
          }
        }
       }
    
      public static void sumRows(int[][] randArray)
      {
        int[] sumOfRows = new int[randArray.length];
        int sumOfR = 0;
        for (int row = 0; row < randArray.length; row++){
          for (int col = 0; col < randArray[row].length; col++){
            sumOfR += randArray[row][col];
          }
        sumOfRows[row] = sumOfR;
        }
        System.out.println("Sum of rows: " + sumOfR);
      }
    
      public static void sumCols(int[][] randArray)
      {
        int[] sumOfCols = new int[randArray[i].length];
        int sumOfC = 0;
        for (int col = 0; col < randArray.length; col++){
          for (int row = 0; row < randArray[col].length; row++){
            sumOfC += randArray[col][row];
          }
        sumOfCols[col] = sumOfC;
        }
        System.out.println("Sum of columns: " + sumOfC);
      }
    
    }
    And output:
    Elements in 2D Array:
    9
    9
    6
    1
    3
    1
    6
    1
    4
    10
    7
    0
    4
    4
    5
    6
    5
    5
    10
    3
    Sum of rows: 99
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at arraysproject3.sumCols(arraysproject3.java:55)
    at arraysproject3.main(arraysproject3.java:28)

    Any guidance on what I'm doing wrong would be great! Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    5
    You only have a few issues, which is good.

    In the first program, the sortEvens method is iterating through a 10 element array, but the array you set up for storing the even numbers is only 5 elements long. What happens if you have 6 even numbers? Secondly, the index where you storing data into evenList is never increased. This means when your program does find an even number it stores it into the same position as the previous ones it found. Fortunately, all three of your methods are similar. Hopefully, you should be able to make these same 2 changes to all of them.

    In your second program, for both methods you're not printing the array storing the totals. You're only printing the last total you calculated. Also, in the sumCols method, you make an array with a length of randArray[i].length, then you try to access it up to an index of randArray.length.

    Try fixing the errors I pointed out, and see if you can get it working from there.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by JonthnC

    Try fixing the errors I pointed out, and see if you can get it working from there.
    Thanks so much for the help. I will work on it and check back if I have any more questions.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Ok I've been able to fix the second one, but I'm still struggling with the odd/even/negative one. I made the changes you said and still get the same output.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    5
    Code:
      public static void sortEvens(int[] intArray)
        {
          System.out.println("The even numbers are: ");
          int[]  evenList = new int[10];
          int even = 0;
          for(i = 0; i < 10; i++){
            if(intArray[i] % 2 == 0){
               evenList[even] = intArray[i];
               even++;
             }
            }
          System.out.println(evenList);
        }
    I made a few changes to get this working. (I did overlook a few errors and failed to mention them in the last post)
    I set the length of evenList to 10. The for loop is set to run 10 times. I added an increment operator to even, and I reversed the line in the if statement so the new value is put into the evenList (you had it backwards). I also print the entire array rather than the last element.

    I didn't check this with a compiler, but it should be right.
    --- One more thing, we use the number 10 in there twice. You should change this to the length of the array in both instances (10 becomes intArray.length). This way, if you change the number of elements in your initial array, your code will still run fine.

IMN logo majestic logo threadwatch logo seochat tools logo