Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default to print a matrix recursively until all its elemets are zero

    hi guys i am just stuck with dis code here what i actually want is im taking a matrix and i have to print the maximum value in the matrix after that the max value is assigned 0 and i have to print the matrix with modified value,then it should take this output matrix as input and find the next maximum element print it and again assign the value 0 until all the matrix values are 0's hope u people help it
    this is the code i am working on:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package project;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.ArrayList;
    import java.util.Scanner;

    /**
    *
    * @author kusuma
    */
    public class SimilarityMatrix {


    public static void main(String[] args) throws IOException {

    int array[][]=new int[120][120];

    outputArray(array);
    }

    public static void outputArray(int[][] array) throws IOException {
    FileOutputStream out=new FileOutputStream("similaritymatrix.txt");
    PrintStream p=new PrintStream(out);

    int rowSize = array.length;
    int columnSize = array[0].length;
    for(int i = 1; i <= 110; i++) {
    System.out.print("[");
    p.print("[");

    for(int j = 1; j <= 109; j++) {
    if(i!=j && i<j)
    {
    app obj=new app();


    array[i][j]= obj.main(i,j);


    }

    else if(i>j || i==j)
    {
    array[i][j]=0;
    }
    System.out.print(" " + array[i][j]);
    p.print(" " + array[i][j]);






    }
    System.out.println(" ]");
    p.println(" ]");

    }
    System.out.println();
    p.println();
    int maxValue = 1;
    int i=1;

    int j=1;

    for ( i = 1; i <= 110; i++) {
    for ( j = 1; j <= 109; j++)
    {
    if (array[i][j] > maxValue) {
    maxValue = array[i][j];

    }}}
    for ( i = 1; i <= 110; i++) {
    for ( j = 1; j <= 109; j++)
    {
    if(maxValue==array[i][j]){
    System.out.println("\nMax values in 2D array at d"+i+"d"+j+"max value ="+maxValue);


    p.println("\nMax values in 2D array at d"+i+"d"+j+"max value ="+maxValue);

    array[i][j]=0;
    }
    }}


    for( i=1;i<=110;i++)
    { p.print("[");
    System.out.print("[");

    for( j=1;j<=109;j++)
    {
    System.out.print(" "+ array[i][j]);
    p.print(" "+ array[i][j]);

    }

    p.println(" ]");
    System.out.println("]");

    }
    }
    }
    class app {



    private static ArrayList<String> load(String f1) throws FileNotFoundException{

    Scanner reader = new Scanner(new File(f1));

    ArrayList<String> out = new ArrayList<String>();

    while (reader.hasNext()){

    String temp = reader.nextLine();

    String[] sts = temp.split(" ");

    for (int i = 0;i<sts.length;i++){

    if(sts[i] != "" && sts[i] != " " && sts[i] != "\n")

    out.add(sts[i]);

    }

    }

    return out;

    }



    private static void write(ArrayList<String> out, String fname) throws IOException{


    FileWriter writer = new FileWriter(new File(fname));

    for (int i = 0;i<out.size();i++){

    writer.write(out.get(i) + "\t");

    }

    writer.close();

    }



    public static int main(int a,int b) throws IOException {

    int count=0;

    ArrayList<String> file1;

    ArrayList<String> file2;

    ArrayList<String> out = new ArrayList<String>();

    file1 = load(a+".txt");

    file2 = load(b+".txt");

    for(int i = 0;i<file1.size();i++){

    String word1 = file1.get(i);

    for (int z = 0; z <file2.size(); z++){

    if (word1.equalsIgnoreCase(file2.get(z))){


    boolean already = false;

    for (int q = 0;q<out.size();q++){

    if (out.get(q).equalsIgnoreCase(file1.get(i))){

    already = true;


    }

    }


    if (already==false){

    out.add(file1.get(i));

    count++;
    }}}}


    return count;








    }


    }


    my problem is i am able to print the matrix for only one time by making its maximum value to 0 but i need to print the matrix recursively until all the matrix values are 0 hope u people will help it its urgent pls

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 12-08-2012, 07:20 PM
  2. Counting the length of a string recursively
    By Zoralink23 in forum New To Java
    Replies: 1
    Last Post: 11-29-2012, 03:37 AM
  3. Recursively searching a string
    By fam2315 in forum New To Java
    Replies: 0
    Last Post: 10-13-2012, 06:25 PM
  4. (Recursively?) Getting Files in a Directory
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 05-21-2009, 08:37 AM
  5. recursively searching through arraylists
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:36 AM

Tags for this Thread

Posting Permissions

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