矩阵程序在java中使用一维数组

时间:2014-06-03 07:21:04

标签: java

我想在java中使用一维数组编写矩阵乘法程序,但是使用二维数组值?

我想知道如何编程吗?

package group1;
import java.util.Scanner;
public class multiplication {
    public static void main(String[] args) {
        int m,n,p,q,sum=0,c,d,k,s;
        Scanner scan=new Scanner(System.in);
        System.out.println("enter the num of rows and columns:");
        m=scan.nextInt();
        n=scan.nextInt();

        int first[] =new int[m*n];
        System.out.println("enter the value of first matrix");
        for(c=0;c<m*n;c++){
            first[c]=scan.nextInt();
        }


        System.out.println("enter the num of rows and columns second matrix");
        p=scan.nextInt();
        q=scan.nextInt();
        int second[] = new int[p*q];
    int answer[] = new int[c];


        System.out.println("enter the elements of second matrix");
        for(c=0;c<p*q;c++){
            second[c]=scan.nextInt();
        }

        for(c=0;c<m;c++){
            for(d=0;d<n;d++)
                for(k=0;k<4;k=k+4){
                    sum=sum+first[d]*second[k];
                        //sum=sum+first[c]*second[c+3];
                }
            answer[c]=sum;
            sum=0;
        }

        System.out.println("The product is ");
        for(c=0;c<m*q;c++){
            System.out.print(answer[c] +"\t");
        System.out.println("");
        }
    }
}

我试过这样但是我收到了错误 有人告诉我该怎么做或程序本身

1 个答案:

答案 0 :(得分:0)

你的程序没有用,因为用于访问1D阵列的逻辑是错误的。通常,如果您想以2D方式使用一维数组,则需要执行类似

的操作
oneDimensionalArray[row * columns + column] 

这相当于twoDimensionalArray[row, column] 还尝试使用描述性名称,如firstRows或firstColumns,而不是m或n。这提供了更多的可读性。 有了这个,假设用户以行列格式输入值,以下程序使用单维数组计算矩阵乘积。

import java.util.Scanner;

public class multiplication {
    public static void main(String[] args) {
        int firstRows, firstCols, secondRows, secondCols, c, d, k;
        Scanner scan = new Scanner(System.in);
        System.out.println("enter the num of rows and columns:");
        firstRows = scan.nextInt();
        firstCols = scan.nextInt();

        int first[] = new int[firstRows * firstCols];
        System.out.println("enter the value of first matrix");
        for (c = 0; c < firstRows * firstCols; c++) {
            first[c] = scan.nextInt();
        }

        System.out.println("enter the num of rows and columns second matrix");
        secondRows = scan.nextInt();
        secondCols = scan.nextInt();
        int second[] = new int[secondRows * secondCols];
        int answer[] = new int[firstRows * secondCols];

        System.out.println("enter the elements of second matrix");
        for (c = 0; c < secondRows * secondCols; c++) {
            second[c] = scan.nextInt();
        }
        scan.close();

        if ( firstCols != secondRows ) {
            throw new IllegalArgumentException("A:Rows: " + firstCols + " did not match B:Columns " + secondRows + ".");
        }

        for (c = 0; c < firstRows; c++) {
            for (d = 0; d < secondCols; d++) {
                for (k = 0; k < firstCols; k++) {
                    answer[(c * secondCols) + d] += first[(c * firstCols) + k] * second[(k * secondCols) + d];
                    //Equivalent to  answer[c][d] += first[c][k] * second[k][d];
                }
            }
        }

        System.out.println("The product is ");
        for (c = 0; c < firstRows; c++) {
            for (d = 0; d < secondCols; d++) {
                System.out.print(answer[(c * secondCols) + d] + "\t");
            }
            System.out.println();
        }
    }
}