sol5 - } for(int t=N-1;t>=0;--t){ for(int...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
COP3530 Solution 5 package dataStructures; import java.util.*; public class matrixAdd extends Chain{ public static class nodeElement{ int a; int b; } public static void displayMatrix(Chain[] c, int N) { for(int i=0;i<N;i++) { Iterator y = c[i].iterator(); System.out.print("Chain " + i + ":"); while (y.hasNext()) { nodeElement m = (nodeElement)y.next(); System.out.print(m.a + " " + m.b + " " ); } System.out.println(); } System.out.println("\n"); } public static Chain [] addMatrices(Chain[] a, Chain[] b, int N) { Chain[] c= new Chain[N]; for(int g=0;g<N;++g){ c[g]= new Chain(); } int[][] ae= new int[N][N]; int[][] be= new int[N][N]; int[][] ce= new int[N][N]; for(int u=0;u<N;++u){ for(int f=0;f<a[u].size();++f){ int y=((nodeElement)(a[u].get(f))).a; ae[u][y-1]=((nodeElement)(a[u].get(f))).b; } } for(int u=0;u<N;++u){ for(int f=0;f<b[u].size();++f){ int y=((nodeElement)(b[u].get(f))).a; be[y-1][u]=((nodeElement)(b[u].get(f))).b; } } for(int h=0;h<N;++h){ for(int jj=0;jj<N;++jj){ ce[h][jj]= ae[h][jj] + be[h][jj]; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } for(int t=N-1;t>=0;--t){ for(int q=N-1;q>=0;--q){ int hj= ae[t][q]+be[t][q]; if(ce[t][q]!=0){ nodeElement gi= new nodeElement(); gi.a=q+1; gi.b=ce[t][q]; c[t].add(0,(Object)gi); } } } return c; } public static void main(String args) { int N = 4; Chain a = new Chain[N]; Chain b = new Chain[N]; Chain c; for(int i=0;i<N;i++) { a[i] = new Chain(); b[i] = new Chain(); } nodeElement n,m; // Matrix A (represented using row chains) n = new nodeElement(); n.a = 2; n.b = 10; a[0].add(0,(Object)n); n = new nodeElement(); n.a = 2; n.b = 2; a[1].add(0,(Object)n); n = new nodeElement(); n.a = 1; n.b = 2; a[1].add(0,(Object)n); n = new nodeElement(); n.a = 3; n.b = 3; a[2].add(0,(Object)n); //Matrix B (represented using column chains) n = new nodeElement(); n.a = 3; n.b = 1; b[0].add(0,(Object)n); n = new nodeElement(); n.a = 1; n.b = 1; b[1].add(0,(Object)n); n = new nodeElement(); n.a = 3; n.b = 1; b[2].add(0,(Object)n); c = addMatrices(a,b,N); displayMatrix(a, N); displayMatrix(b, N); displayMatrix(c, N); } }...
View Full Document

This note was uploaded on 01/15/2010 for the course COP 3530 taught by Professor Davis during the Fall '08 term at University of Florida.

Page1 / 3

sol5 - } for(int t=N-1;t>=0;--t){ for(int...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online