Question

In: Computer Science

Twin elements Create a program that: Input: Receive as input in command line the sizes m...

Twin elements

Create a program that:

  • Input:
  • Receive as input in command line the sizes m and n of a two dimensional array
  • Receive as input in command line the minValue and maxValue between which the random numbers will be generated for your two dimensional array
  • Generate:
  • Generate a two-dimensional with numbers between minValue and maxValue (inclusive) with the given size (mxn)
  • Compute:
  • Compute the number of elements that have a neighbour (up, down, left or right) with the same value.
  • Print:
  • Print the array as a table with the computed sums per rows and columns.
  • Print the computed numbers of twins.

Run the program 5 times with representative sizes

Solutions

Expert Solution

/******************************************************************************

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();
}
}


Related Solutions

Minimum value and its position Create a program that: Receive as input in command line the...
Minimum value and its position Create a program that: Receive as input in command line the sizes m and n of a two dimensional array, and minValue and maxValue between which the random numbers will be generated Generate a array with numbers between minValue and maxValue (inclusive) with the given size (mxn) Compute the minimum value in the array and one of the rows and columns on which it appears. Print the array as a table. Print the minimum value...
Maximum value and its index in an array java program that: Receive as input in command...
Maximum value and its index in an array java program that: Receive as input in command line the size of an array (n) Generate an array with numbers between 1 and 100 (inclusive 1 and 100) with the given size (a) Print the array with the format: {a, b, c, ..., x}, for instance {1, 2} Compute and print in the console the following: maximum value in the array one index on which the maximum value appears (a number from...
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...
IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your program will create two files: even.txt - contains all integers from the input file that are even and greater than the threshold odd.txt - contains all integers from the input file that are odd and greater than the threshold The input file will exist and only contain a set of integers. It will always be valid data. Output whitespace will be ignored. Name the...
Python programming: Instructions: The python program should respond to user input on a command line Below...
Python programming: Instructions: The python program should respond to user input on a command line Below are the four options - if the user input is A, the program will quit -if the user input is B, the program will display the number of times the prompt has been displayed -if the user input is C, will display whatever is stored. -if the user inputs something else, it will store that user input as a string and append it to...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4 ME 2 finish 2! Output: Consonants: WMfnsh 1) Name your program command_consonants.c. 2) If no command line arguments was provided, your program should print a usage message “Usage: ./a.out input #include<stdio.h> int main() { printf("Input: "); int i = 0; char ch; // array to store consonants char consonant[100]={""}; //do loop to take input do { ch = getchar(); //checks to see if consonants...
Create a python program that will: prompt a user for a command Command get_data Level 1:...
Create a python program that will: prompt a user for a command Command get_data Level 1: Take one of the commands my_max my_min my_range my_sum mean median mode fib factorize prime Requirements: Your commands should be case-insensitive You should use python lists to store data You should NOT use built-in python math functions, or math libraries to compute these values Tips: Write one function that will convert a string with comma-separated numbers into a python list with the numbers. You...
java program: Input and output the following details. Your program can only receive the correct input....
java program: Input and output the following details. Your program can only receive the correct input. (For example: a notification will be given if you not enter Char data type for Name) Name: Ali bin Ahmad Occupation: Technician Age: 30 Hometown: Negeri Sembilan Years of Service: 12 Gender: Male
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT