I am getting an index out of bound error for this.
I set on netbeans the argument on the project and works fine.
But how can i set an argument for n within the code without going on the project properties and changing the value there?
When i try to put static int N = 4 ; i get error throughout code, can someone help me please?
package matrix;
// performing matrix multiplication parallely by using two threads
// In thread 1 we will multiply matrix a and b and store in C with range of 0 to N/2
// In thread 2 we will multiply matrix a and b and store in C with range of N/2 to N
// For our convenience I'm used 4 instead of N ( we can replace 4 with N)
public class Mymainclass implements Runnable {
static int n;
// a and b are input matrix's
static int a[][];
static int b[][];
/* we will multiply the elements in a and b matrix's
* parallely by using two threads
and will store in the c matrix sequentially.*/
static int c[][];
public Mymainclass(int n1) {
n = n1;
a = new int[n][n];
b = new int[n][n];
c = new int[n][n];
}
public void run() {
int i, j, k;
System.out.println("in thread1 class");
for (i = 0; i < this.n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
this.c[i][j] += a[i][k] * b[k][j];
}
}
}
System.out.print("\n");
System.out.println("Matrix c in thread1");
for (int a = 0; a < n; a++) {
for (int b = 0; b < n; b++) {
System.out.print(c[a][b] + " ");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
String n1 = args[0];
n = Integer.parseInt(n1);
System.out.println(n);
Mymainclass th1 = new Mymainclass(n);
int z = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = z++;
}
System.out.print("\n");
}
z = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = z++;
}
System.out.print("\n");
}
System.out.println("Matrix a");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
System.out.println("Matrix b");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
Thread t = new Thread(th1);
t.start();
}
}
That is working for me:
public class Mymainclass implements Runnable {
static int n;
// a and b are input matrix's
static int a[][];
static int b[][];
/* we will multiply the elements in a and b matrix's
* parallely by using two threads
and will store in the c matrix sequentially.*/
static int c[][];
public Mymainclass(int n1) {
n = n1;
a = new int[n][n];
b = new int[n][n];
c = new int[n][n];
}
public void run() {
int i, j, k;
System.out.println("in thread1 class");
for (i = 0; i < this.n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
this.c[i][j] += a[i][k] * b[k][j];
}
}
}
System.out.print("\n");
System.out.println("Matrix c in thread1");
for (int a = 0; a < n; a++) {
for (int b = 0; b < n; b++) {
System.out.print(c[a][b] + " ");
}
System.out.print("\n");
}
}
static int N = 4 ;
public static void main(String[] args) {
Mymainclass th1 = new Mymainclass(N);
int z = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = z++;
}
System.out.print("\n");
}
z = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = z++;
}
System.out.print("\n");
}
System.out.println("Matrix a");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
System.out.println("Matrix b");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
Thread t = new Thread(th1);
t.start();
}
}
For measuring time just change code where you start thread to:
ExecutorService service = Executors.newSingleThreadExecutor();
long start = System.currentTimeMillis();
Future<?> future = service.submit(th1);
try {
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("time: " + (System.currentTimeMillis() - start));
service.shutdown();
Related
How to flip this triangle?
So i was making aritmethic sequance triangle. It was upside down.
How do I turn it 180 degree?
for example:
1=1
1+2=3
1+2+3=6
etc...
my code:
package javaapplication4;
public class NewClass5 {
public static void main(String[] args) {
int i=5,a;
for(int j=i; j>=1; j--) {
for(a=1; a<=i; a++)
System.out.print(a +" + ");
int n = 0;
for(a = 1; a<=i; a++) {
n = n + a;
}
System.out.print(" = "+ n);
System.out.println();
i--;
}
}
}
You can do it for any n, by getting input from the user
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int add = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
add += j;
}
System.out.println(add);
}
I think you just need to change the sequence of loop from descending to ascending, rest of the things should be the same:
for (int i = 1; i <= 5; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
sum += j;
}
System.out.println(sum);
}
When I run this, I'm able to see expected output:
src : $ java NewClass5
1=1
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15
I'm currently working on implementing the Floyd Warshall algorithm from pseudocode to java. I felt like I had it correct, but anytime I ran it on a graph, I was getting an output of large negative number and I feel that it was probably the way I implemented my algorithm. After looking I realized I may have missed something. I haven't implemented the k-1 part of the pseudocode and I'm not sure how to implement this. Im hoping this will correct the issue.
Here it the code i have written.
public static int[][] floyd(int n, int[][] W, int[][] P, int[][] D) {
D = W;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
P[i][j] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if ((D[i][j] == Integer.MAX_VALUE) && (D[i][k] + D[k][j] == Integer.MAX_VALUE)) { // add
continue;
} else if (D[i][j] <= D[i][k] + D[k][j]) {
D[i][j] = D[i][j];
} else {
D[i][j] = D[i][k] + D[k][j];
}
}
return D;
}
And here is the pseudocode im writing based off of.
Floyd-Warshall(W)
n = W.rows
D(0) = W
for (k = 1 to n)
for ( i = 1 to n )
for ( j = 1 to n )
if (dij(k-1) == INF) && (dik(k-1) + dkj(k-1)) == INF)
continue
else if (dij(k-1) ≤ dik(k-1) + dkj(k-1))
dij(k) = dij(k-1) // add
else
dij(k) = dik(k-1) + dkj(k-1) // add
return (D(n))
EDIT
Heres all of my code thats relevant with some corrections
int V = g.nodeList.size();
int W[][] = new int[V][V];
int P[][] = new int[V][V];
// Adding wights to graph
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
W[j][i] = Integer.parseInt(g.edgeList.get((j * V) + i).label);
if (W[j][i] == 0) {
W[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Matrix to find the shortest path of.");
printMatrix(W, V, g);
System.out.println("Shortest Path Matrix.");
printMatrix(floyd(V, W, P), V, g);
System.out.println("Path Matrix");
printPredMatrix(P, V, g);
}
public static int[][] floyd(int n, int[][] W, int[][] P) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
P[i][j] = 0;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if ((W[i][j] == Integer.MAX_VALUE) && (W[i][k] + W[k][j] == Integer.MAX_VALUE)) { // add
continue;
} else if (W[i][j] <= W[i][k] + W[k][j]) {
W[i][j] = W[i][j];
} else {
W[i][j] = W[i][k] + W[k][j];
}
}
return W;
}
public static int min(int i, int j) {
if (i > j) {
return j;
}
return i;
}
public static void printMatrix(int[][] Matrix, int V, Graph g) {
System.out.print("\n\t");
for (int j = 0; j < V; j++) {
System.out.print(g.nodeList.get(j).name + "\t");
}
System.out.println();
for (int j = 0; j < 35; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < V; i++) {
System.out.print(g.nodeList.get(i).name + " |\t");
for (int j = 0; j < V; j++) {
if ((Matrix[i][j] == Integer.MAX_VALUE)||(Matrix[i][j] == Integer.MIN_VALUE)) {
System.out.print("~");
} else {
System.out.print(Matrix[i][j]);
}
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("\n");
}
public static void printPredMatrix(int[][] Matrix, int V, Graph g) {
System.out.print("\n\t");
for (int j = 0; j < V; j++) {
System.out.print(g.nodeList.get(j).name + "\t");
}
System.out.println();
for (int j = 0; j < V * 3; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < V; i++) {
System.out.print(g.nodeList.get(i).name + " |\t");
for (int j = 0; j < V; j++) {
System.out.print(Matrix[i][j]);
System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("\n");
}
}```
I wrote a code for minesweeper problem. It creates an MxN minesweeper game where each cell is a bomb with probability p. Prints out the m-by-n game and the neighboring bomb counts.
Code:
class Minesweeper {
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
try {
boolean[][] bombs = new boolean[m+2][n+2];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
bombs[i][j] = (Math.random() < p);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (bombs[i][j]) System.out.print("* ");
else System.out.print(". ");
System.out.println();
}
int[][] sol = new int[m+2][n+2];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
// (ii, jj) indexes neighboring cells
for (int ii = i - 1; ii <= i + 1; ii++)
for (int jj = j - 1; jj <= j + 1; jj++)
if (bombs[ii][jj]) sol[i][j]++;
System.out.println();
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (bombs[i][j]) System.out.print("* ");
else System.out.print(sol[i][j] + " ");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Do I need to give any condition after parsing?
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
Please help me.
Just give a extra condition before parsing the argument.
if (args.length >= 3) {
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
p = Double.parseDouble(args[2]);
}
import java.util.Scanner;
public class array1 {
public static void main(String [] args){
int table[][] = new int[5][5];
Scanner scan = new Scanner(System.in);
for(int i =0; i < 5; i++){
for(int j =0; j < 5; j++){
System.out.println("Write a value for row " +i + " column " +j);
int n = scan.nextInt();
table[i][j] = n;
}
}
for(int i =0; i < 5; i++){
for(int j =0; j < 5; j++){
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
int sum = 0;
boolean prime = true;
for(int i =0; i < 5; i++){
for(int j =0; j < 5; j++){
for(int e = 2; e < table[i][j]; e++ ){
if(table[i][j] % e == 0){
prime = false;
}
}
if(prime == true){
sum += table[i][j];
}
else{}
}
}
System.out.println();
System.out.println("Sum of all prime numbers in this array is " +sum);
}
}
Well, as the title itself says uhmm The program is supposed to sum up all the prime numbers in the user defined Array table but it's just summing up the first row. i checked up all the brackets, nothing helps. Any help would be appreciated! Thank you!
You should reset prime for each iteration:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int e = 2; e < table[i][j]; e++) {
if (table[i][j] % e == 0) {
prime = false;
}
}
if (prime == true) {
sum += table[i][j];
} else {
}
prime = true;
}
}
Here is the code:Getting Array index out of Bound exception
class Max {
public static void main(String args[]) {
int a[][];
Scanner src = new Scanner(System. in );
System.out.println("Enter the no of rows");
int rows = src.nextInt();
a = new int[rows][5];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = src.nextInt();
}
}
System.out.println("Array is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" " + a[i][j]);
}
System.out.println();
}
int l = a[0][0];
int i;
for (i = 0; i < rows; i++)
l = a[i][0];
for (int j = 0; j < 5; j++)
if (l < a[i][j])
l = a[i][j];
System.out.println("Max" + l);
}
}
on run time it gives the following:
Exception in thread "main" java.lang.Array index out of Bound Exception:3
at Max.main Max.java:33
Can Anyone suggest what is wrong in the code????
You miss a { after the for (i = 0; i < rows; i++) so that the for (int j = 0; j < 5; j++) loop is inside the first one.
Without that, this gives:
for (i = 0; i < rows; i++)
l = a[i][0];
// End of the for i loop, now i = rows.
for (int j = 0; j < 5; j++)
if (l < a[i][j]) // i = rows: bang.
l = a[i][j];
Try this:
public static void main(String args[]) {
int a[][];
Scanner src = new Scanner(System. in );
System.out.println("Enter the no of rows");
int rows = src.nextInt();
a = new int[rows][5];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = src.nextInt();
}
}
System.out.println("Array is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" " + a[i][j]);
}
System.out.println();
}
int l = a[0][0];
int i;
for (i = 0; i < rows; i++) {
l = a[i][0];
for (int j = 0; j < 5; j++)
if (l < a[i][j])
l = a[i][j];
}
System.out.println("Max" + l);
}