import java.util.*;
class Matrice
{
    private int[][] a;
    private int row;
    private int col;

    public static Scanner in()
    {   return new Scanner(System.in);  }

    public static void out(String m)
    {   System.out.print(m);    }

    public static int readInt(String m)
    {
        out(m);
        return in().nextInt();
    }

    public Matrice()
    {
        row=2;
        col=2;
        a=new int[row][col];
    }

    public Matrice(int row,int col)
    {
        this.row=row;
        this.col=col;
        a=new int[row][col];
    }

    public void getMatrice(int row,int col,int[][] a)
    {
        int[][] b=new int[row][col];
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
            {
                b[i][j]=readInt("Enter A["+i+","+j+"]: ");
            }
    }

    public void displayMatrice()
    {
        for(int k[]:a)
        {
            for(int p:k)
                out(p+" ");
            out("\n");
        }
    }

    public void sumInRow(int row,int col)
    {
        int[] s=new int[row];
        for(int i=0;i<row;i++)
        {
            s[i]=a[i][0];
            for(int j=0;j<col;j++)
                s[i]+=a[i][j];
        }
        for(int k:s)
            out(k+"");
    }

    public void sortInRow(int row,int col,int[][] a)
    {
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col-1;j++)
                for(int k=j+1;k<col;k++)
                    if(a[i][j]>a[i][k])
                    {
                        int t=a[i][j];
                        a[i][j]=a[i][k];
                        a[i][k]=t;
                    }
        }
    }

    public void sortInColumn(int row,int col,int[][] a)
    {
        for(int j=0;j<col;j++)
        {
            for(int i=0;i<row-1;i++)
                for(int k=i+1;k<row;k++)
                if(a[i][j]>a[k][j])
                {
                    int t=a[i][j];
                    a[i][j]=a[k][j];
                    a[k][j]=t;
                }
        }
    }

    public void updateElement(int row,int col,int item,int nrow,int ncol)
    {
        if(nrow<0 | nrow>row) return;
        if(ncol<0 | ncol>col) return;
        a[nrow][ncol]=item;
    }
}

class Test
{
    public static void main(String[] args)
    {
        Matrice ob=new Matrice();
        int[][] a=new int[3][3];
        ob.getMatrice(3,3);
        ob.displayMatrice();
    }
}

Recommended Answers

All 4 Replies

No problem description, just dropped code with no comments and no code tags...
Bad, bad, bad!

public void getMatrice(int row,int col,int[][] a)
{
int[][] b=new int[row][col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
b[i][j]=readInt("Enter A["+i+","+j+"]: ");
}
}

I don't know what you intended this to do, but it won't do it.
You create a new array "b" (local to this method), carefully fill it with values, then return from the method leaving b to be destroyed.
In the meantime, the array "a" that you pass in as a parameter is ignored completely.
Did you intend to put the values in a?

Then in the Test class you create yet another array called a, and do nothing with it.

// In class Matrice
public void getMatrice(int row,int col,int[][] a)
    {
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
            {
                a[i][j]=readInt("Enter A["+i+","+j+"]: ");
            }
    }

// class Test
class Test
{
    public static void main(String[] args)
    {
        Matrice ob=new Matrice();
        int[][] a=new int[3][3];
        ob.getMatrice(3,3,a);
        ob.displayMatrice();
    }
}

thanks, but still incorrect.

commented: You been asked to use code tags nice way, but you wouldn't listen. Bad -4

1. "still incorrect" is useless. Do you think I can read your mind? You MUST specify exactly what is wrong.
2. Your Test main method is still wrong, as I pointed out in my previous post. You declare a new array called "a", and then seem to confuse it with the "a" that's declared in Matrice.
Your methods in Matrice sometimes work with the "a" that's declared in the class, but other methods pass it in as a parameter.
I suggest you (1) delete every declaration of "a" except the one in the Matrice class and (2) delete every use of the array as a parameter and just let the methods use the version you declared in the class. and (3) if you want more help, explain your problem properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.