I want to create a 1D array that saves the min columns from my 2D array, but I get wrong the min values. My theseisLine array is a copy from my intP array that I store there the column numbers, so theseisLine shows the min from each column.
package themab2018;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class THEMAB2018 {
static void displayP(int intP[][]) {
System.out.println("intP2D");
for(int i = 0; i < intP.length; i++) {
for(int j = 0; j < intP[i].length; j++)
System.out.print(intP[i][j] + " ");
System.out.println();
}
}
static void findMinCol(int intP[][] ,int n) {
int theseisLine[] = new int[n];
if(theseisLine.length < intP.length)
for(int i = 0; i < theseisLine.length; i++)
theseisLine[i] = intP[0][i];
else
for(int i = 0; i < intP.length; i++)
theseisLine[i] = intP[0][i];
int min = theseisLine[0];
System.out.println();
System.out.println("Min Array");
for(int i = 0; i < theseisLine.length; i++) {
if(theseisLine[i] < min) {
min = theseisLine[i];
}
System.out.print(min + " ");
}
}
public static void main(String[] args){
int m , n;
System.out.println("Import m , n");
do {
System.out.print("Give m ");
m = scannerUserInput.getInteger();
System.out.print("Give n ");
n = scannerUserInput.getInteger();
} while (m < 1 && n < 1);
int intP2D[][] = new int [m][n];
int theseisLine[] = new int[n];
for(int i = 0; i < intP2D.length; i++){
for(int j = 0; j < intP2D[i].length; j++) {
intP2D[i][j] = (int) (Math.random() * (10 - 1)+1)+1;
}
}
displayP(intP2D);
findMinCol(intP2D, m);
}
}
The result that I get example :
Import m , n
Give m 3
Give n 4
intP2D
3 9 7 2
8 9 7 6
5 8 8 7
Min Array
3 3 3
The result that i want in this example :
Import m , n
Give m 3
Give n 4
intP2D
3 9 7 2
8 9 7 6
5 8 8 7
Min Array
3 8 7 2
Firstly, while calling findMinCol(intP2D, m); you need to pass the count of column i.e. n and not count of row.
Secondly, while calculating the min element in the column, you are comparing all element with int min = theseisLine[0]; which might not be correct.
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class THEMAB2018 {
static void displayP(int intP[][]) {
System.out.println("intP2D");
for(int i = 0; i < intP.length; i++) {
for(int j = 0; j < intP[i].length; j++)
System.out.print(intP[i][j] + " ");
System.out.println();
}
}
static void findMinCol(int intP[][] ,int n) {
int theseisLine[] = new int[n];
for(int i = 0; i < theseisLine.length; i++)
theseisLine[i] = intP[0][i];
System.out.println();
System.out.println("Min Array");
for(int i = 0; i < theseisLine.length; i++) {
for (int j = 1; j < intP.length; j++) {
if (intP[j][i] < theseisLine[i]) {
theseisLine[i] = intP[j][i];
}
}
System.out.print(theseisLine[i] + " ");
}
}
public static void main(String[] args){
int m , n;
System.out.println("Import m , n");
m = 3; n = 4;
int intP2D[][] = new int [m][n];
int theseisLine[] = new int[n];
for(int i = 0; i < intP2D.length; i++){
for(int j = 0; j < intP2D[i].length; j++) {
intP2D[i][j] = (int) (Math.random() * (10 - 1)+1)+1;
}
}
displayP(intP2D);
findMinCol(intP2D, **n**);
}
}
the output I am getting:
intP2D
2 5 8 7
10 6 9 3
7 9 3 8
Min Array
2 5 3 3
I don't know what you want to check in your finding method with the if-else block. You don't need it anyway to find the minimum of each column, that's why I removed it. A simple nested for loop solves your problem.
Additionally, you don't need to pass the number of columns as a parameter, because it can be easily queried from the matrix. I still left it in to avoid having to change the method call.
static void findMinCol(int intP[][] ,int n) {
int rows = intP.length;
int cols = intP[0].length;
int[] result = new int[cols];
for(int i = 0; i < cols; i++){
int min = Integer.MAX_VALUE;
for(int j = 0; j < rows; j++){
if(min > intP[j][i]){
min = intP[j][i];
}
}
result[i] = min;
}
for(int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
Related
I'm trying to create a 2D int array of numbers that are the product of 2 via user input defining the length of the rows and columns, as the one below:
1 2 4 8
2 4 8 16
4 8 16 32
8 16 32 64
I've only got up to here, and cannot figure out how to make the matrix to start from 1 and to look like the one above. I'd appreciate your help on this one!
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
int[][] matrix = new int[n][n];
matrix[0][0] = 1;
int temp = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = temp * 2;
temp *= 2;
}
}
System.out.println(Arrays.deepToString(matrix));
I'd say that each element of the matrix would be:
matrix[i][j] = (int) Math.pow(2, i+j) ;
So, your loop would look like this:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = (int) Math.pow(2, i+j);
}
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
int[][] matrix = new int[n][n];
matrix[0][0] = 1;
int temp = 1;
for (int i = 0; i < n; i++) {
temp = (int)Math.pow(2, i);
for (int j = 0; j < n; j++) {
matrix[i][j] = temp;
temp *= 2;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
Closed 3 years ago.
I am working with 2-D arrays and I require help on this topic. My task is to create a 2-D array such that it is n by n (i.e. the number of rows and columns are equal). Fill the array with alternating 0's and 1's
void setup()
{
int n=3;
// code to populate the array
// code to display the output of array in a table format
/* output should be as follows:
The expected result when n=3 should be as the following:
1 0 1
0 1 0
1 0 1
*/
}
Solution:
class test{
public static void main(String[] args) {
int n = 3;
int[][] arr = setup(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
static int[][] setup(int n){
int[][] arr = new int [n][n];
boolean even = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = even ? 1 : 0;
even = !even;
}
}
return arr;
}
}
Output:
0 1 0
1 0 1
0 1 0
This should work.
You can replace the 3 that is given to n with any number you want as the 2D-Array's length.
void setup(){
int n = 3;
int count = 0;
int[][] myArray = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(count % 2 == 0){
myArray[i][j] = 1;
}
else{
myArray[i][j] = 0;
}
System.out.print(myArray[i][j] + " ");
count++;
}
System.out.println();
}
}
Output:
0 1 0
1 0 1
0 1 0
It's not complex, you just have to iterate two loops, that's it. Although you can get the solution on different ways.
public static void main(String[] args) {
int n = 3;
int [][] arr = new int[n][n];
int val = 1;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j] = val;
val = (val == 0) ? 1 : 0;
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
Output:
0 1 0
1 0 1
0 1 0
A simple implementation:
void setup()
{
int n=3;
final int[][] array = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((n * i + j) % 2 == 0) {
array[i][j] = 1;
}
}
}
}
Here is how you can approach your logic , create a counter and negate each time after it prints value.
public static void main(String[] args) {
int counter = 0;
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(counter==0?1:0);
counter = ~counter;
}
System.out.println();
}
}
Output
101
010
101
Here is Online source code.
user input n number of rows in 2d array
Elements are stored in an array of size [n][4].
Input Format:
First line of the input is an integer ānā that denotes the number of rows.
Next n lines contain the elements of the row
Output Format:
if 2 rows match then separate it by hyphen
If no rows is same, print "None".
If the array size is negative, print "Invalid Input" and terminate the program.
Sample Input 1:
5
3 1 2 4
2 4 5 1
3 1 2 4
2 4 5 1
3 1 2 4
Sample Output 1:
1-3
1-5
2-4
3-5
Sample Input 2:
3
3 1 2 4
2 4 5 1
3 4 2 5
Sample Output 2:
None
Sample Input 3:
-5
Sample Output 3:
Invalid Input
i have tried the following code but it didnt work
import java.util.Arrays;
import java.util.Scanner;
import java.lang.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n <= 0)
{
System.out.println("Invalid Input");
}
else
{
int[][] data = new int[n][4];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = sc.nextInt();
}
}
Object[] array = new Object[4];
Object[] array1 = new Object[4];
int idx = 0;
int idx1 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
array[idx] = data[i][j];
array1[idx1] = data[i + 1][j];
idx++;
idx1++;
if (Arrays.deepEquals(array, array1))
{
System.out.println(i + "-" + j);
}
else
System.out.println("None");
}
}
} int[][] data = new int[n][4];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = sc.nextInt();
}
}
Object[] array = new Object[4];
Object[] array1 = new Object[4];
int idx = 0;
int idx1 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
array[idx] = data[i][j];
array1[idx1] = data[i + 1][j];
idx++;
idx1++;
if (Arrays.deepEquals(array, array1))
{
System.out.println(i + "-" + j);
}
else
System.out.println("None");
}
}
}}}
you are taking only the 1st elements in array and array1 in each step not the entire row.
what you need to do is you have to take entire 1 row and compare with the other rows more 'for' loops
This is what I want to do when A is a square matrix.
P - is power.
A & B are square matrices.
User will be asked to enter size of matrix A, and elements of matrix A and to what power they want to raise the matrix to.
Once they input what power, and what elements my program is supposed to calculate this:
(Assuming P = 5)
A^5 + A^4 + A^3 + A^2 + A
I have written a method that adds matrices a method that multiplies them, and a method that raises them to the power and they all work correctly.
The problem I am having is the final step which I showed above A^5 + A^4 + A^ 3...
This is where the problem gets even weirder, my program works when the elements in the matrix are all the same... such that a
2 2 2
2 2 2
2 2 2
matrix will give me the CORRECT output, BUT
1 2 3
4 5 6
7 8 9
matrix will give me the WRONG output and I have no idea why.
This is the method in which the problem is occuring
public static void addPowers(int [][] a, int[][] b, int p) {
while( p != 1){
b = addMatrices(powerMatrix(a,p), b) ;
addPowers(a,b,p-1) ;
return ;
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++)
System.out.print(b[i][j] + "\t") ;
System.out.println();
}
}
Just in case you ask, reason I have the recursive under a while loop is so it won't print over and over and over again.
Thanks for your time! :)
Edit: More clarifying information.
addMatrices is a method that adds matrices with an two int[][] arguments.
powerMatrix is a method that finds the power of a matrix with (int[][], int) arguments.
EDIT Methods being called...
public static int[][] multiplyMatrices(int matrixA[][], int matrixB[][]) {
int temp[][] = new int[matrixA.length][matrixB.length];
int matrix[][] = new int[matrixA.length][matrixB.length];
int sum = 0 ;
for (int i = 0; i < matrixA.length; i++)
{
for (int j = 0; j < matrixB.length; j++)
{
for (int l = 0; l < matrixA.length; l++)
{
sum += matrixA[i][l] * matrixB[l][j] ;
}
temp[i][j] = sum ;
sum = 0 ;
}
}
matrix = temp;
return matrix ;
}
public static int[][] addMatrices(int matrixA[][], int matrixB[][]) {
int temp[][] = new int[matrixA.length][matrixB.length];
int sum = 0 ;
for (int i = 0; i < matrixA.length; i++)
{
for (int j = 0; j < matrixB.length; j++) {
{
sum = matrixA[i][j] + matrixB[i][j] ;
}
temp[i][j] = sum ;
}
}
return temp ;
}
public static int[][] powerMatrix (int[][] a, int p) {
int[][] result = a;
for (int n = 1; n < p; n++)
result = multiplyMatrices(result, a);
return result;
}
In your addMatrices method, you should remove the third loop.
Like this:
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
temp[i][j] = matrixA[i][j] + matrixB[i][j] ;
}
}
I have to sort elements in each row and then display the array.
For example, if the input array is:
5 1 3 1 3 5
INPUT: 7 6 4 OUTPUT: 4 6 7
9 8 2 2 8 9
My code for that was:
for (int i = 0; i < size; i++) { //"size" is the size of the square matrix
for (int j = 0; j < size; j++) {
for (int k = 0; k < size - 1; k++) {
for (int l = 0; l < size - k - 1; l++) {
if (arr[i][j] > arr[i][j+1]) { //arr[][] is of datatype int
int temp = arr[i][j];
arr[i][j] = arr[i][j+1];
arr[i][j+1] = temp;
}
}
}
}
}
Any suggestions?
for (int i = 0; i < size; i++){ //"size" is the size of the square matrix
for (int j = 0; j < size; j++){
for (int k = j+1; k < size; k++){
if (arr[i][j] > arr[i][k]){ //arr[][] is of datatype int
int temp = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = temp;
}
}
}
}
I don't think you need 4th loop
I would do it simpler
for(int[] r : arr){
Arrays.sort(r);
}
I would create a method to sort rows, and then just iterate through the rows in the matrix and sort them one at a time. For example:
public static int[] sortRow(int[] row) // selection sort
{
for (int i = 0; i < row.length - 1; i++) {
for (int j = i + 1; j < row.length; j++) {
if (row[i] > row[j]) {
int temp = row[i];
row[i] = row[j];
row[j] = temp;
}
}
}
return row;
}
public static void main(String args[])
{
int[][] arr = {{5, 1, 3}, {7,6,4}, {9,8,2}};
for (int r = 0; r < arr.length; r++) { // for every row in the matrix
arr[r] = sortRow(arr[r]); // set the row to be the sorted row
}
// print out the array to the console
for (int r[] : arr) {
for (int c : r)
System.out.print(c + " ");
System.out.println();
}
}
Output:
1 3 5
4 6 7
2 8 9