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]);
}
Related
I am trying to figure out how to increment a pyramid based on input from the user.\
If a user enters the number 3, my program will print a pyramid of height 3, three times.\
What I would like it to do instead, is to print 3 pyramids, but the first pyramid should have a height of 1, and the second a height of 2, and the third a height of 3.
Here is my code:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int l = 0; l < n; l++) {
System.out.println("Pyramid " + n);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < n - i; k++) {
System.out.print(".");
}
System.out.println();
}
}
You use l to keep track of the number of pyramid you're working on, so you could just use it in the loop instead of n. Note that l starts with 0, not 1, so you may want to amend the loop accordingly and run from 1 to n, not from 0 to n-1
for (int l = 1; l <= n; l++) { // Note the loop starts at 1
System.out.println("Pyramid " + l);
for (int i = 1; i <= l; i++) { // Note the usage of l instead on n
for (int j = 0; j < l - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < l - i; k++) { // Note the usage of l instead on n
System.out.print(".");
}
System.out.println();
}
}
All you have to change is this part of code
System.out.println("Pyramid " + (l+1));
for (int i = 1; i <= l+1; i++) {
I am trying to multiply one matrix by another which are different sizes. The 1st matrix is 4 x 2, and the 2nd matrix is 2 x 2. Currently, the answer produced for this multiplication is by my program is not correct. Please help.
public class MatrixCipher {
public static void main(String[] args) {
int[][] matrix = new int[4][2];
int[][] matrix2 = new int[2][2];
matrix2[0][0] = 1;
matrix2[0][1] = 2;
matrix2[1][0] = 3;
matrix2[1][1] = 4;
int[][] product = new int[4][2];
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 2; n++) {
matrix[m][n] = (int) (6 * Math.random() + 1);
}
}
System.out.print("==1==");
System.out.println(" ");
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 2; n++) {
System.out.print(matrix[m][n] + " ");
}
System.out.println(" ");
}
System.out.print("=====");
System.out.println("");
System.out.println(" x");
System.out.print("==2==");
System.out.println(" ");
for (int m = 0; m < 2; m++) {
for (int n = 0; n < 2; n++) {
System.out.print(matrix2[m][n] + " ");
}
System.out.println(" ");
}
System.out.print("=====");
System.out.println("");
System.out.println(" =");
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 2; n++) {
for (int f = 0; f < 2; f++) {
product[m][n] = matrix[m][n] * matrix2[f][n];
}
}
}
System.out.print("=====");
System.out.println(" ");
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 2; n++) {
System.out.print(product[m][n] + " ");
}
System.out.println(" ");
}
System.out.print("=====");
System.out.println("");
}
}
You miss the whole addition part and use a wrong order during the mutliplication.
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 2; n++) {
for (int f = 0; f < 2; f++) {
product[m][n] = product[m][n] + matrix[m][f] * matrix2[f][n];
}
}
}
So for an assignment i need to make a dual-wedge figure with for loops,so far I have had no luck, can anyone help?
Here is a sample of the outcome:
*******
*** ***
** **
* *
Here's my code
int dual_wedge_length=9;
int half_length = dual_wedge_length/2;
int space=1;
int height2 = (dual_wedge_length/2) +1;
for (int line1 = 1; line1 <= dual_wedge_length; line1++)
{
System.out.print("*");
}
System.out.println();
for (int height = 1; height <= (dual_wedge_length+1)/2; height++)
{
for (int half1 = 1; half1 <= half_length; half1++)
{
System.out.print("*");
//half_length--;
space+=2;
}
for (int space_counter = 0; space_counter == space;space_counter++)
{
System.out.print(".");
}
for (int half1 = 1; half1 >= half_length; half1++)
{
System.out.print("*");
half_length--;
}
System.out.println();`
int dual_wedge_length=9;
int height2 = (dual_wedge_length+1)/2;
for(int i = 0;i < dual_wedge_length; i++)System.out.print("*");
System.out.println();
for(int i = 1; i < height2;i++){
int num = height2 - i;
for(int j = 0; j < num; j++){
System.out.print("*");
}
for(int k = 0; k < 2*i -1; k++){
System.out.print(" ");
}
for(int m = 0; m < num; m++){
System.out.print("*");
}
System.out.println();
}
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();
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);
}