I have to make a table in java and than using method fin max min value. Here is my code, i dont know how to input a method who wil return me max/min value.
import java.io.*;
public class Max {
public static void main(String[] args)throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader (System.in));
System.out.print("Input a numbers of rows: ");
int a = Integer.parseInt(input.readLine());
System.out.print("Input a numbers of a columns: ");
int b = Integer.parseInt(input.readLine());
int [][] tab = new int [a][b];
for (int i = 0; i < tab.length; i++){
for (int j = 0; j < tab[i].length; j++){
tab[i][j] = (int)(Math.random()*10001);
}
}
for (int i = 0; i < tab.length; i++){
for (int j = 0; j < tab[i].length; j++){
System.out.print(tab[i][j] + " ");
}
System.out.println();
}
}
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < tab.length; i++) {
for (int j = 0; j < tab[i].length; j++) {
if (tab[i][j] < min) {
min = tab[i][j];
}
if (tab[i][j] > max) {
max = tab[i][j];
}
}
}
System.out.println("min:" + min + ", max:" + max);
Either when you are setting up the array
int max = 0;
for (int i =0; i < tab.length; i++) {
for (int j = 0; j < tab[i].length; j++) {
int val = Math.random(100001);
if (val > max) {
max = val;
}
}
}
or after you have setup the array
int max = 0;
for (int i =0; i < tab.length; i++) {
for (int j = 0; j < tab[i].length; j++) {
if (tab[i][j] > max) {
max = tab[i][j];
}
}
}
Related
Given a link list of n floating-point numbers, it returns a two-dimensional link list, say M, of size n × n in which the entry
M[i][j] for i ≤ j contains the average of the array entries A[i]
through A[j]. That is: if i ≤ j, then M[i][j] = (A[i] + · · · + A[j])/( j − i + 1) , whereas for i > j we have that M[i][j] = 0.
I have already designed this task in Array but Now I want to do it in Arraylist.
public static void main(String[] args) {
System.out.println("Enter Element");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
float[][] a = new float[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j]= sc.nextInt();
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i > j) {
a[i][j]=0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < j) {
a[i][j] = 0;
for (int k = i; k <= j; k++) {
a[i][j] += a[k][k];
}
a[i][j] /= (j - i + 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(" "+a[i][j]);
}
System.out.println("");
}
}
I have tried with the same idea but I am getting at this stage OutOfBoundException
for (int i = 0; i < lis.length; i++) {
for (int j = 0; j < lis.length; j++) {
if (i > j) {
lis[i][j].add(j, zero);
}
}
}
I need the maximum elements position if there is more than one maximum element then the first one is to be printed.
My code prints the position of the maximum element but not the first one.
I don't understand why the last iteration is not working as I intend it to.
Please solve it using only Java.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// put your code here
Scanner sc = new Scanner(System.in);
// define lengths
int n = sc.nextInt();
int m = sc.nextInt();
// add length to matrix
int[][] matrix = new int[n][m];
// insert elements
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
}
}
// define max
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
// System.out.print(i + " " + j);
}
// System.out.print(max + " ");
// print index of highest element
// int pos1 = 0;
// int pos2 = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (matrix[i][j] == max) {
System.out.print(i + " " + j);
break;
}
// pos2 += 1;
break;
}
// pos1 += 1;
// break;
}
}
}
There is no need to go through the matrix twice. When you are searching for the max, store also the coordinates of the matrix where that max was found. A code example:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// put your code here
Scanner sc = new Scanner(System.in);
// define lengths
int n = sc.nextInt();
int m = sc.nextInt();
// add length to matrix
int[][] matrix = new int[n][m];
// insert elements
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
}
}
// define max
int max = Integer.MIN_VALUE, row=0, col=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
row=i;
col=j;
}
}
}
System.out.print("max: "+max + " is at: ");
System.out.print(col + " " + row); //indexes starting from zero
}
}
Create a new variable to hold the position of the max value and set it in the current loop
int max = matrix[0][0];
int[] maxPos = new int[2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
maxPos[0] = i;
maxPos[1] = j;
}
}
}
and then remove the rest of the code and print the result
System.out.printf("Max is %d and is found at [%d, %d]\n", max, maxPos[0], maxPos[1]);
I have a two dimensional array and I fill it with scanner. I want to copy the elements that start with letter 'a' to a new one dimensional array without using ArrayList. Please advise on what I can do to get this code functioning properly. the question is how can I know the new array size while I don't know how many words start with letter a
Here is what I have so far:
import java.util.Scanner;
class Untitled {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int c2 = -1;
String[] name2 = new String[count];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
c2++;
temp = arr[i][j];
name2[c2] = temp;
count++;
temp = "";
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
}
}
A two dimensional arrray of size [n][n] is equal to one dimensional array of size n. If you want to copy them on proper place then you can use this formula, it is useful if you later want to copy these elements back to twodimensional array at proper places:
int v = i * n + j; // i and j your loops and n is length of rows or colums.
array[v] = array[i][j];
for in your codes it's like:
int v = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i * arra.length +j;
name2[v] = arr[i][j];
count++;
Ok here is a working code:
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
System.out.println("Enter the name: ");
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int v = 0;
String[] name2 = new String[arr.length*arr[0].length];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i *+arr[0].length + j;
name2[v] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
System.out.println("printing without nulls");
//if you don't want null to be printed then do this:
for (int i = 0; i < name2.length; i++) {
if(name2[i] != null)
System.out.println(name2[i]);
}
}
I did it with two nested for loop one for indicating the array size and the other for filling the elements into the array, it does the work but is there any way to do this better
public static void student(String[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
size++;
}
}//inner
}//outer
String[] name2 = new String[size];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
name2[count] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
Trying to write a method that swaps the rows of a 2D array in order of increasing row sum.
For example, if I have the following 2d array:
int [][] array = {4,5,6},{3,4,5},{2,3,4};
I would want it to output an array as so:
{2 3 4}, {3 4 5}, {4 5 6}
Methodology:
a.) take the sums of each row and make a 1D array of the sums
b.) do a bubble sort on rowSum array
c.) swap the rows of the original array based on the bubble sort swaps made
d.) then print the newly row sorted array.
Here's my code so far:
public void sortedArrayByRowTot() {
int [][] tempArray2 = new int [salaryArray.length [salaryArray[0].length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
int [] rowSums = new int [tempArray2.length];
int sum = 0;
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
sum += tempArray2[i][j];
}
rowSums[i] = sum;
sum = 0;
}
int temp;
int i = -1;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] > rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
}
}
if(!isSwap){
break;
}
}
for (int k = 0; k < tempArray2.length; k++) {
temp = tempArray2[i-1][k];
tempArray2[i-1][k] = tempArray2[i][k];
tempArray2[i][k] = temp;
}
for (int b = 0; b < tempArray2.length; b++) {
for (int c = 0; c < tempArray2[b].length; c++) {
System.out.print(tempArray2[b][c] + " ");
}
}
}
}
Not sure if I am doing part c of my methodology correctly?
It keeps saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2"
As #shmosel said, you can do it like this:
public static void sortedArrayByRowTot() {
int [][] array = {{4,5,6},{3,4,5},{2,3,4}};
Arrays.sort(array, Comparator.comparingInt(a -> IntStream.of(a).sum()));
}
I was able to solve my question. Thanks.
public void sortedArrayByRowTot() {
//Creates tempArray2 to copy salaryArray into
int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];
//Copies salaryArray into tempArray2
for (int i = 0; i < salaryArray.length; i++) {
for (int j = 0; j < salaryArray[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
//Creates rowSum array to store sum of each row
int [] rowSums = new int [tempArray2.length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[0].length; j++) {
rowSums[i] += tempArray2[i][j];
}
}
//Modified Bubble Sort of rowSum array (highest to lowest values)
int temp;
int i = 0;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] < rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
//swaps rows in corresponding tempArray2
int [] temp2 = tempArray2[i-1];
tempArray2[i-1] = tempArray2[i];
tempArray2[i] = temp2;
}
}
if(!isSwap){
break;
}
}
//Prints sorted array
System.out.println("Sorted array: ");
for (i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
System.out.print("$"+ tempArray2[i][j] + " ");
}
System.out.println();
}
}
You may try this way. That I have solved.
public class Solution{
public static void sortedArrayByRowTot() {
int [][] salaryArray = { {4,5,6},{3,4,5},{2,3,4} };
int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];
for (int i = 0; i < salaryArray.length; i++) {
for (int j = 0; j < salaryArray[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
// Buble Sort to store rowSums
int [] rowSums = new int [tempArray2.length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[0].length; j++) {
rowSums[i] += tempArray2[i][j];
}
}
//Buble Sort by Rows Sum (Lowest Value to Highest)
int temp;
int i = 0;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] > rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
//swaps rows in corresponding tempArray2
int [] temp2 = tempArray2[i-1];
tempArray2[i-1] = tempArray2[i];
tempArray2[i] = temp2;
}
}
if(!isSwap){
break;
}
}
/** No Need.
for (int k = 0; k < tempArray2.length; k++) {
temp = tempArray2[i-1][k];
tempArray2[i-1][k] = tempArray2[i][k];
tempArray2[i][k] = temp;
}
*/
for (int b = 0; b < tempArray2.length; b++) {
for (int c = 0; c < tempArray2[b].length; c++) {
System.out.print(tempArray2[b][c] + " ");
}
}
}
public static void main(String[] args) {
sortedArrayByRowTot();
}
}
I am trying to print out the largest number in a 2D array. My problem is that my output are three numbers instead of one - the largest. Why?
Here is my code:
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int maxRows = 3;
int maxCols = 4;
int [] onedArray = new int [maxRows];
for (int i = 0; i < maxRows; i++){
onedArray[i] = (int) ((Math.random() * 100) * maxCols);
}
int [][] twodArray = new int[maxRows][];
for (int i = 0; i < maxRows; i++){
twodArray[i] = new int[maxCols];
}
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
twodArray[i][j] = (int) (Math.random() * 100);
}
}
System.out.println("2 - The 2D array: ");
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
System.out.print(twodArray[i][j] + " ");
}
System.out.println("");
}
int maxValue = 1;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
}
}
Everything up until the last sequence of instructions is correct (although poorly formatted).
Here is original:
int maxValue = 1;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
Here is better version:
int maxValue = 0;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray[i].length; j++) {
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
}
System.out.println("Max value of row " + i + ": " + maxValue);
}
Look carefully and you'll see that I added the { character after the second for-loop.
If you wanted to find total max, and minimize open and close curly-braces here is another version:
int maxValue = 0;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++)
for (int j = 0; j < twodArray[i].length; j++)
if (twodArray[i][j] > maxValue)
maxValue = twodArray[i][j];
System.out.println("Maximum value: " + maxValue);
Good luck.
int m,n,max;
int a[][]=new int[10][10];
Scanner S=new Scanner(System.in);
System.out.println("Enter m*n matrix");
m=S.nextInt();
n=S.nextInt();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=S.nextInt();
}
}
max=a[0][0];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}
System.out.println(max);
Your line System.out.println(maxValue); needs to come out of the loop over the variable i. It's being printed 3 times because it's inside this loop.
This would be easier to see if your code was indented properly; this is a good habit to get into anyway.
The answer is in your code once it's indented correctly:
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
}
Don't underestimate how useful good indentation can be for catching this kind of bug :)
int max;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows : ");
int n = sc.nextInt();
System.out.println("Enter number of columns : ");
int m = sc.nextInt();
int[][] array = new int[n][m];
System.out.println("Enter the elements of array : ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print("X[" + i + "," + j + "]" + "=");
array[i][j] = sc.nextInt();
}
}
max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (array[i][j] > max) {
max = array[i][j];
}
}
}
System.out.println("Max value of the array is " + max);
}