Results 1 to 3 of 3
  1. #1
    Zerpol is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Thumbs up Sparse matrix in java

    Good day!

    How do I display a sparse matrix with input values? I do have a code here but it just don't run the way I want it to.

    int array[][]= new int [5][5];
    int size=array.length;
    int i, j, n=1;
    String input, output;
    JTextArea pad=new JTextArea();

    JOptionPane.showMessageDialog(null,"Sparse Matrix with NON-ZERO Elements");
    output="Sparse Matrix\n";
    input=JOptionPane.showInputDialog("Enter rows");
    i=Integer.parseInt(output);
    array[i][0]=i;
    input=JOptionPane.showInputDialog("Enter rows");
    j=Integer.parseInt(output);
    array[0][j]=j;

    for(i=0; i<size; i++){
    output+="\n";
    for(j=0; j<size; j++)
    output+=array[i][j]+"";
    }
    pad.setText(output);
    JOptionPane.showMessageDialog(null, pad,"Sparse Matrix.Java",JOptionPane.PLAIN_MESSAGE);
    }
    I just can't display them in a two dimensional output.
    Last edited by Zerpol; 12-15-2010 at 06:57 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,311
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by Zerpol View Post
    Java Code:
        	for(i=0; i<size; i++){
        		output+="\n";
        		for(j=0; j<size; j++)
        			output+=array[i][j]+"";
        	}
        	pad.setText(output);
        	JOptionPane.showMessageDialog(null, pad,"Sparse Matrix.Java",JOptionPane.PLAIN_MESSAGE);
    I just can't display them in a two dimensional output.
    How is it displayed now? b.t.w. what is sparse about your matrix?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    7

    Default

    The thing is, because sparse matrices don't have many non-zero elements, using a 2d array to represent one is the exact opposite of what you want to accomplish. If you have an 8x8 matrix with 3 non zero entries, you want a data structure with 3 elements, not 64. This is probably a school project, and since you have this kind of assignment you probably learned about other data structures than arrays.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Transposing a matrix
    By atomant in forum New To Java
    Replies: 11
    Last Post: 12-05-2010, 09:10 AM
  2. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  3. Matrix Message
    By shindry in forum New To Java
    Replies: 2
    Last Post: 05-04-2009, 04:32 AM
  4. implementing sparse and nonsparse matrices together
    By ishakteyran in forum New To Java
    Replies: 0
    Last Post: 12-07-2007, 09:10 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •