Adding large matrices ArrayIndexOutOfBoundsException - java

I am writing a program which adds two integer matrices together and I am running into an ArayIndexOutOfBounds exception when trying to process larger matrices. Any advice on how to fix this issue would be greatly appreciated.
ArrayOutOfBoundsException on cmd prompt
Here is the class in which the error is occuring:
import java.util.ArrayList;
public class ThreadMaker
{
public static int count=0;
public static void addmatrix(int[][] mat_A,int[][] mat_B,int[][] submatrix1,int[][] submatrix2,int N,int M)throws Exception
{
ThreadOperation m1;
ThreadOperation.initialize(N,M);
ArrayList<Thread> threads = new ArrayList<Thread>();
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for(k=0,i=c; i < c + N/2; i++, k++)
{
for(l=0,j=d; j < d + M/2; j++, l++)
{
submatrix1[k][l]=mat_A[i][j];
submatrix2[k][l]=mat_B[i][j];
}
}
m1=new ThreadOperation(mat_A,mat_B,submatrix1,submatrix2,N,M,c,d);
Thread thread =new Thread(m1);
thread.start();
threads.add(thread);
thread.join();
d=d+M/2;
}
c=c+N/2;
}
}
public static void devidematrix(int[][] mat_A,int[][] submatrix,int N,int M)
{
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for( k=0,i=c;i<c+N/2;i++,k++)
{
for( l=0,j=d;j<d+M/2;j++,l++)
{
submatrix[k][l]=mat_A[i][j];
}
}
d=d+M/2;
}
c=c+N/2;
}
}
public synchronized static void printmatrix(int[][] matrix,int row,int col)
{
System.out.print(" ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print(matrix[i][j]+" ");
}
}
System.out.print(" ");
}
}
Here is the martix from the matrix2.txt file:
4 7
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
If any more information is need please let me know.

Related

Change only one of the many iteration

I have 0-10 for loop I want to change the value to 100 when is only 4 and the rest will not be affected and no break how should I achieve that. And as for the petitioner, do not repel [him].
class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
int n = 0;
for (int i = 0; i < 10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
}
else
{
System.out.println(i);
}
}
}
}
Another Solution!!
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
for (int i = 0; i <=10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
System.out.println(i);
i=4;
continue;
}
else
{
System.out.println(i);
}
}
}
}
Try This one !!
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
for(int i = 0; i < 10; i++){
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if(i == 4){
System.out.println ("100");
}else{
System.out.println(i);
}
}
}

Java - thread priorities (example from Thinking in Java)

I read now chapter about concurrency from Thinking in Java and I try to reproduce example from this book in my computer and I got very different results.
I do example from subchapter Priorities:
public class SimplePriorities implements Runnable {
private int countDown = 5;
private volatile double d;
private int priority;
public SimplePriorities(int priority) {
this.priority = priority;
}
#Override
public String toString() {
return Thread.currentThread() + ": " + countDown;
}
#Override
public void run() {
Thread.currentThread().setPriority(priority);
while (true) {
for (int i = 1; i < 100000; i++) {
d += (Math.PI + Math.E) / (double) i;
if (i % 1000 == 0) {
Thread.yield();
}
System.out.println(this);
if (--countDown == 0) return;
}
}
}
public static void main(String[] args) {
final ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
executorService.execute(new SimplePriorities(Thread.MIN_PRIORITY));
}
executorService.execute(new SimplePriorities(Thread.MAX_PRIORITY));
executorService.shutdown();
}
}
and in result I got:
Thread[pool-1-thread-1,1,main]: 5
Thread[pool-1-thread-4,1,main]: 5
Thread[pool-1-thread-4,1,main]: 4
Thread[pool-1-thread-4,1,main]: 3
Thread[pool-1-thread-4,1,main]: 2
Thread[pool-1-thread-4,1,main]: 1
Thread[pool-1-thread-6,10,main]: 5
Thread[pool-1-thread-6,10,main]: 4
Thread[pool-1-thread-6,10,main]: 3
Thread[pool-1-thread-6,10,main]: 2
Thread[pool-1-thread-6,10,main]: 1
Thread[pool-1-thread-3,1,main]: 5
Thread[pool-1-thread-5,1,main]: 5
Thread[pool-1-thread-2,1,main]: 5
Thread[pool-1-thread-1,1,main]: 4
Thread[pool-1-thread-2,1,main]: 4
Thread[pool-1-thread-5,1,main]: 4
Thread[pool-1-thread-3,1,main]: 4
Thread[pool-1-thread-5,1,main]: 3
Thread[pool-1-thread-2,1,main]: 3
Thread[pool-1-thread-1,1,main]: 3
Thread[pool-1-thread-2,1,main]: 2
Thread[pool-1-thread-5,1,main]: 2
Thread[pool-1-thread-3,1,main]: 3
Thread[pool-1-thread-5,1,main]: 1
Thread[pool-1-thread-2,1,main]: 1
Thread[pool-1-thread-1,1,main]: 2
Thread[pool-1-thread-3,1,main]: 2
Thread[pool-1-thread-1,1,main]: 1
Thread[pool-1-thread-3,1,main]: 1
The author wrote that the last result has the highest priority, but when I run this code in my system I get different priorities and on average often low priorities than high, like above.
I use Ubuntu 16.04.

Input matrices from a file

I'm trying to read 2D matrices from an input file.The​ ​input​ ​file​ ​contains​ ​a​ ​series​ ​of​ ​inputs.​ ​First​ ​line​ ​contains​ ​the matrix ​size​ ​​n.​Next​ ​​n line​ ​contains​ ​​n ​integer​ ​each,​ ​i.e.,​ ​an​ ​n*n matrix.​​The​ ​file​ ​ends with​ ​a​ ​zero​ ​as​ ​matrix ​size.A small sample is below.
2
1 1
1 1
3
3 1 2
1 1 2
2 2 1
6
1 2 3 4 2 3
3 3 4 5 2 1
4 3 3 1 2 3
5 4 3 6 2 1
3 2 4 3 4 3
2 3 4 1 5 6
0
I wrote the following code but it doesn't show what i need.
import java.util.*;
import java.io.*;
public class trial{
public static void main(String[] args) {
try{
//System.out.println(new File("input.txt").getAbsolutePath());
Scanner input = new Scanner(new File("./input.txt"));
while (true){
int n = input.nextInt();
//System.out.println("%d",n);
if(n!=0) {
int[][] grid = new int[n][n];
while (input.hasNext()) {
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
grid[row][column] = input.nextInt();
System.out.printf(" %d ", grid[row][column]);
}
System.out.println();
}
System.out.println("Array done");
}
}
input.close();
}
}catch (FileNotFoundException e){
System.out.println("File not found");
}
}
}
The code output is below.It always generates a 2*2 matrix.
1 1
1 1
Array done
3 3
1 2
Array done
1 1
2 2
Array done
2 1
6 1
Array done
2 3
4 2
Array done
3 3
3 4
Array done
5 2
1 4
Array done
3 3
1 2
Array done
3 5
4 3
Array done
6 2
1 3
Array done
2 4
3 4
Array done
3 2
3 4
Array done
1 5
6 0
Array done
First of all, it looks like you want to stop if the user inputs a size of 0, but you don't actually do that, instead you just loop forever. Second, after reading the first matrix, you close the scanner, which isn't what you should do. input.close() should be outside the while loop. Try this.
try {
Scanner input = new Scanner(new File("./input.txt"));
int n;
while (input.hasNextInt() && (n = input.nextInt()) > 0) {
int[][] grid = new int[n][n];
for (int row = 0; row < n; row++) {
for (int column = 0;
column < n && input.hasNextInt(); column++) {
grid[row][column] = input.nextInt();
System.out.printf("a %d ", grid[row][column]);
}
System.out.println();
}
System.out.println("Array done");
}
input.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
In your code you are not check when the nextInt is a size and when is a number in matrix . Another thing is that a 0 in size it is for you an EOF , so you shoul get out .
Below a working example :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
try{
Scanner input = new Scanner(new File("pathtofile"));
while (true){
int n = input.nextInt();
//System.out.println("%d",n);
int cont =0;
if(n!=0) {
int[][] grid = new int[n][n];
while (input.hasNext() && cont != n) {
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
grid[row][column] = input.nextInt();
System.out.printf("a %d ", grid[row][column]);
}
System.out.println();
cont++;
}
System.out.println("Array done");
}
}
else{
input.close();
break;
}
}
}catch (FileNotFoundException e){
System.out.println("File not found");
}
}}
Result :

Insertion Sort pt2 -Hackerrank

can someone please check my code as to why the last index is not working as intended? Any advice on how to improve this is much appreciated.
import java.io.*;
import java.util.*;
public class Solution {
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 0 ; i < ar.length-1; i++){
key = ar[i];
seen = i;
while (seen <ar.length-1 && ar[seen+1]<key){
ar[seen]= ar[seen+1];
seen = seen+1;
}
ar[seen]=key;
printArray(ar);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] ar = new int[s];
for(int i=0;i<s;i++){
ar[i]=in.nextInt();
}
insertionSortPart2(ar);
}
private static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}
}
Input (stdin) 6 1 4 3 5 6 2
Your Output (stdout) 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 2 6
Expected Output 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6 Compiler Message Wrong Answer
You are doing it in wrong way. Read the part 1 again.
Assume that first element is sorted. Then try to insert element one by one in sorted list.
Try this:
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 1 ; i < ar.length; i++){
key = ar[i];
seen = i;
while (seen > 0 && ar[seen-1] > key) {
ar[seen] = ar[seen-1];
seen = seen - 1;
}
ar[seen]=key;
printArray(ar);
}
}

Whats wrong with my QuickSort implementation?

class PartitionIt
{
public static void partitionIt(int[] a, int l, int r, int pivot)
{
int i,j;
i = j = l+1;
while(j<= r)
{
if(a[j] <= a[pivot])
{
swap(a,j,i);
i++;
}
j++;
}
swap(a,pivot,--i);
}
public static void swap(int[] a, int j, int i)
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
public static void displayArray(int[] a)
{
for(int i:a)
System.out.print(i+" ");
System.out.println();
}
public static void QuickSort(int[] a, int l, int r)
{
if(r <= l)
return;
int pivot = getPivot(a,l,r);
partitionIt(a,l,r,pivot);
QuickSort(a,l,pivot);
QuickSort(a,pivot+1,r);
}
public static int getPivot(int[] a,int l,int r)
{
return l;
}
public static void main(String[] args)
{
int[] a = {3,2,8,5,1,4,7,6};
int[] b = {1,2,3,4,5,6,7,8,9,0};
int[] c = {5,4,2,4,7,6,5,3,2,1,10};
displayArray(a);
System.out.println("After Parititon with pivot 3");
QuickSort(a,0,a.length-1);
displayArray(a);
System.out.println();
displayArray(b);
System.out.println("After Parititon with pivot 1");
QuickSort(b,0,b.length-1);
displayArray(b);
System.out.println();
displayArray(c);
System.out.println("After Parititon with pivot 5");
QuickSort(c,0,c.length-1);
displayArray(c);
System.out.println();
}
}
3 2 8 5 1 4 7 6
After Parititon with pivot 3
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 0
After Parititon with pivot 1
0 1 2 3 4 5 6 7 8 9
5 4 2 4 7 6 5 3 2 1 10
After Parititon with pivot 5
1 2 2 4 3 4 5 5 6 7 10
It is not sorting properly in last case.
Can anyone help here. I m stuck from so long.
Thanks in advance!
At This snippet:
if(a[j] <= a[pivot])
{
swap(a,j,i);
i++;
}
The '<=' should be '<'.
while the sequence was sorted to 1 2 2 4 3 4 5 5 7 6 10,the pivot is '4'(the left one),while compare 4 <= 4,i++, this cause the swap(a,pivot,--i) change the location of '4'(the right one ) to '4'(the left one),rather than change the '3' to '4'.

Categories

Resources