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 8707841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:57:54+00:00 2026-06-13T03:57:54+00:00

public class Homework2 { public static void main(String[] args){ int num1 = (int) (Math.random()*(10-3+1)+3);

  • 0
public class Homework2 {
    public static void main(String[] args){
        int num1 = (int) (Math.random()*(10-3+1)+3);
        int num2 = (int) (Math.random()*(10-3+1)+3);

        double[][] doubMatrix1 = new double[num1][num2];
        double[][] doubMatrix2 = new double[num1][num2];
        double[][] doubMatrix3 = new double[num1][num2];

        doubMatrix1 = getdoubMatrix(num1,num2);
        doubMatrix2 = getdoubMatrix(num1,num2);
        doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2, num1, num2);        
        printDoubMatrix("First matrix", doubMatrix1);
        printDoubMatrix("Second matrix", doubMatrix2);
        printDoubMatrix("Result of adding", doubMatrix3);
        doubMatrix2 =transposeMatrix(num1,num2);



    }

    public static double[][] getdoubMatrix(int num1,int num2){

        double[][] tempArray = new double[num1][num2];
        for(int i = 0;i < tempArray.length;i++)
            for(int j = 0;j < tempArray[i].length;j++)
            {
            tempArray[i][j] = Math.random() * (100);                
            }
        return tempArray;
    }

    public static double[][] addMatrices(double[][] doubMatrix1, double[][] doubMatrix2,int num1,int num2)
    {

        double[][] tempArray = null;
        if(doubMatrix1.length == doubMatrix2.length)
            if(doubMatrix1[0].length == doubMatrix2[0].length)
            {
                 tempArray = new double[num1][num2];
                for(int i = 0; i< doubMatrix1.length;i++)
                      for(int j = 0; j< doubMatrix1[i].length;j++ )
                      {
                          tempArray[i][j] = doubMatrix1[i][j] + doubMatrix2[i][j];
                      }
            }
            else
            {
                return tempArray = new double[0][0];

        }

        return tempArray;
    }

    public static void printDoubMatrix(String text,double[][] doubMatrix1){

        System.out.println(text);
        for(int i = 0; i< doubMatrix1.length;i++)
              for(int j = 0; j< doubMatrix1[i].length;j++ )     
                  System.out.printf("%f\n", doubMatrix1[i][j]);                         
}

    public static double[][] transposeMatrix(int num1, int num2){
        double[][] tempArray = new double[num2][num1];
        for(int i = 0;i < tempArray.length;i++)
            for(int j = 0;j < tempArray[i].length;j++)
            {
                tempArray[i][j] = tempArray[j][i];
                System.out.printf("%f\n", tempArray[i][j]); 
            }    
        return tempArray;
    }

}

I have a problem when running this program, there was no error but when I run it it said the array index is out of bound, the problem is at the transpose method, can anyone tell me how to fix this problem?

  • 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-06-13T03:57:55+00:00Added an answer on June 13, 2026 at 3:57 am

    The assignment in the for-loop of transpose method should be like: –

    tempArray[i][j] = doubleMatrix2[j][i];
    

    rather than: –

    tempArray[i][j] = tempArray[j][i];
    

    In the above code, you are assigning the value from a newly created array tempArray to itself only. It doesn’t make sense. It will not affect the array. Also it will throw an ArrayIndexOutOfBounds exception if row != col

    You need to use the matrix you want to transpose.

    Since you are invoking this method for doubleMatrix2

    doubMatrix2 =transposeMatrix(num1,num2);
    

    And your two matrix are like: –

    tempArray[][] = new double[num2][num1];
    
    doubleMatrix[][] = new double[num1][num2];
    

    So it makes sense to assign doubleMatrix[j][i] to tempArray[i][j]. Because number of rows and columns are reversed in the two matrices.

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

Sidebar

Related Questions

import java.lang.Math; public class Homework2 { public static void main(String[] args){ int d1 =
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
import java.util.*; public class SoSanh { public static void main(String[] args) { Scanner input
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
package homework5; import java.io.*; import java.util.Arrays; public class Main { /** * @param args
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Knowing { static final long tooth = 343L; static long doIT(long tooth)
Why does this code crash? CustomDatabaseHelper.java: public class CustomDatabaseHelper { SQLiteDatabase db; private static
This is my homework assignment: Random r = new Random(); public int get100RandomNumber() {
Im trying to use a onclick method. My class has.. Public class Homework extends

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.