In: Computer Science
Twin elements
Create a program that:
Run the program 5 times with representative sizes
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute
it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static int randInt(int min,int max)
{
Random rand = new Random();
int randNum;
randNum = rand.nextInt((max - min) + 1) + min;
return randNum;
}
//This method is for filling a 2D array with random
integers
public static int[][] fillArrayWithRandom(int[][] inputArray,int
min,int max){
int[][] resultArray = new
int[inputArray.length][inputArray[0].length];
for(int r = 0; r < inputArray.length; r++){
for(int c = 0; c < inputArray[0].length; c++){
resultArray[r][c] = randInt(min,max);
}
}
return resultArray;
}
//This method is for for summing rows and columns, and printing
rows, columns, the sum of ruws at each row, and the sum of columns
at each column.
public static void findSumOfRowsAndCols(int[][] inputArray){
int[] colSum = new int[inputArray[0].length];
int[] rowSum = new int[inputArray.length];
for(int i = 0; i < inputArray.length; i++){
for(int j = 0; j < inputArray[i].length; j++){
colSum[j] += inputArray[i][j];
rowSum[i] += inputArray[i][j];
}
}
System.out.println("Matrix: ");
displayArray(inputArray);
System.out.println("Col sum:");
printArray(colSum);
System.out.println("Row sum:");
printArray(rowSum);
}
/**
* @param args the command line arguments
*
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no of rows :");
int rows = sc.nextInt();
System.out.print("Enter no of columns :");
int cols = sc.nextInt();
System.out.print("Enter minValue :");
int min = sc.nextInt();
System.out.print("Enter maxValue :");
int max = sc.nextInt();
//Declare a 7x10 2D array
int [][] twoDArray = new int[rows][cols];
//call fillArrayWithRandom method.
int[][] fillingArray = fillArrayWithRandom(twoDArray,min,max);
//call findSumOfRowsAndCols method
findSumOfRowsAndCols(fillingArray);
computeSameNeighbourElementsCount(fillingArray);
}
public static void computeSameNeighbourElementsCount(int[][]
arr){
int count = 0;
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
try{ if(arr[i][j]==arr[i][j+1]) {count++;break;} }catch(Exception
e){}
try{ if(arr[i][j]==arr[i][j-1]) {count++;break;} }catch(Exception
e){}
try{ if(arr[i][j]==arr[i-1][j]) {count++;break;} }catch(Exception
e){}
try{ if(arr[i][j]==arr[i+1][j]) {count++;break;} }catch(Exception
e){}
}
}
System.out.println("\nNo Of Elements Having Same Neighbours
:"+count);
}
public static void displayArray(int[][] arr){
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void printArray(int arr[]){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}