Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7158785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:07:25+00:00 2026-05-28T13:07:25+00:00

Here is my code, the point of which is to write a program that

  • 0

Here is my code, the point of which is to 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, and the sum of each row and the sum of each column. I can’t figure out where I’m getting this exception (here’s my total output):

Project 3...
2D Array elements:  6, 6, 7, 10, 4, 4, 0, 9, 0, 1, 3, 10, 1, 10, 10, 9, 10, 5, 8, 2, 
Sum of Rows: 115
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at arrayProject.projectthree(arrayProject.java:96)
    at arrayProject.main(arrayProject.java:19)

Here is my code:

public static void projectthree(){
    System.out.println("Project 3...");
    /*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 numbers in a parallel array*/
     int array[][] = new int[5][4];
     Random randomizer = new Random();
     for (int i = 0; i < array.length; i++){
        for (int j = 0; j < array[i].length; j++){
            array[i][j] = randomizer.nextInt(11);   //Assign random values to each element in the array
        }
     }
    System.out.print("2D Array elements: ");
     for (int i = 0; i < array.length; i++){
        for (int j = 0; j < array[i].length; j++){
            System.out.print(" " +array[i][j]+ ",");    //Output 2D Array
        }
     }

    int[] sumOfRows = new int[array.length];
    int[] sumOfColumns = new int[array[0].length];

    int sumR = 0;
    int sumC = 0;
    for(int row = 0; row < array.length; row++){
        for(int col = 0; col < array[0].length; col++){     //Sum of rows
        sumR += array[row][col];
        }
    sumOfRows[row] = sumR;
    }
    System.out.println(" ");
    System.out.println("Sum of Rows: " + sumR);


    for(int col = 0; col < array.length; col++){
        for(int row = 0; row < array[0].length; row++){
            sumC += array[row][col];
            }
    sumOfColumns[col] = sumC;
    }
    System.out.println("Sum of Columns:" + sumC);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:07:26+00:00Added an answer on May 28, 2026 at 1:07 pm

    Look at this loop:

    for(int col = 0; col < array.length; col++){
        for(int row = 0; row < array[0].length; row++){
            sumC += array[row][col];
            }
    

    You’re accessing array[row][col] but you should be accessing array[col][row]… or change the way you’re looping. Compare this loop with all the others which have worked:

     for (int i = 0; i < array.length; i++){
        for (int j = 0; j < array[i].length; j++){
            System.out.print(" " +array[i][j]+ ",");
        }
     }
    

    Note how here i runs from 0 to array.length - 1, and is used as the first bit of indexing in the expression array[i][j].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

-- I am to write a program that prompts for N which is number
Ok, I need some help here. I'm attempting to write a program that rolls
I have a program that prints the cube and which can be rotated. Here's
Here's my code that is not working: print To: ; my $to=<>; chomp $to;
Here is code from MSDN . I don't understand why the work isn't just
enter code here Hi All, I have a simple windows service application that connects
I'm trying to complete an exercise to write a program that takes the following
I'm trying to write a simple program that will receive a string of max
Am trying to write a program for AVR STK600 board in which am cycling
I have a program that I run from the command line that looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.