Finding largest element in a two dimensional array - java

/*
finding largest element in an 2d array
*/
hi im trying to find the largest element in a two dimensional array can anybody help.the output i get is 8 but it should be 45
class LargestSmallestOfTwoDimensionalArray
{
public static void main(String[] args)
{
int[][] data ={ {8,1,25,3,4},{45,12,13,2,3} }; // array of data
int large = data[0][0];
for(int i =0 ; i < data.length ; i++)
{
for(int j =i + 1 ; j < data.length ; j++)
{
if(large < data[i][j])
{
large= data[i][j];//checking and storing the largest element
}
}
}
System.out.println("Largest Element :" + large);
}
}

The problem lies in second loop, start at zero and check your condition:
for (int j = 0; j < data[i].length ; j++)
Remember, you need to check for the length of the inner list, not outer.

The problem is that you are not checking every position in the array.
Change your code to this:
int[][] data ={ {8,1,25,3,4},{45,12,13,2,3} }; // array of data
int large = data[0][0];
for(int i =0 ; i < data.length ; i++)
{
for(int j =0 ; j < data[i].length ; j++)
{
if(large < data[i][j])
{
large= data[i][j];//checking and storing the largest element
}
}
}
System.out.println("Largest Element :" + large);
Hope it helps..

Related

How to create an array from a pre-existing array?

New to java and am trying to create a program/method that will take an int array, and return another int array but it replaces the values of the indexes with the value of the elements. (Example {2,1,3} will return {0,0,1,2,2,2}
public static void main(String[] args) {
int[] pracArray = {2, 1, 3};
int sum = 0;
for (int i = 0; i < pracArray.length; i++)
{
sum = sum + pracArray[i];
}
System.out.println("Amount of array indexes: " + sum);
int[] newArray = new int[sum];
System.out.println(Arrays.toString(pracArray));
for (int i = 0; i < pracArray.length; i++)
{
for (int j = 0; j < pracArray[i]; j++)
{
newArray[j] = i;
}
}
System.out.println(Arrays.toString(newArray));
}
}
Currently I am getting [2,2,2,0,0,0]. I have tried changing the how many times each for loop iterates with no avail. I have also tried to make the elements of newArray equal to a counter ( int count = 0; and having count++ in the for loop) since the values of the new array will always be 0 - however many runs.
Given the length of your array is 3, your outer 'i' loop is iterating through the values 0,1,2. That means your inner 'j' loop never writes to index 3,4,5 (hence why they are 0 in the output), and why the first 3 indexes are set to '2' (2 is the last indexed processed in the 'i' loop). Try this instead...
int h = 0;
for (int i = 0; i < pracArray.length; i++)
{
for (int j = 0; j < pracArray[i]; j++)
{
newArray[h] = i;
h++;
}
}

Java : Selection Sort Logic

I am printing Sorted Array elements using Selection Sort. But I am getting my input array elements as output in same sequence without sorting.
public class SelectionSort {
public static void main(String[] args) {
int[] arr= {1,9,3,0,7};
int n=arr.length;
for(int i=0; i<n-1; i++)
{
int minimumIndex = i;
for(int j=i; j<n; j++)
{
if(arr[j]<arr[minimumIndex])
{
minimumIndex=j;
}
}
int temp=arr[i];
arr[i]=arr[minimumIndex];
arr[i]=temp;
}
for(int e: arr)
{
System.out.print(e+" ");
}
}
}
Expected o/p : 0 1 3 7 9
Actual o/p: 1 9 3 0 7
In Your method code, the actual problem is swapping elements,
the Sequence needs to be like this as below,
int temp=arr[minimumIndex];
arr[minimumIndex]=arr[i];
arr[i] =temp;
instead of
int temp=arr[i];
arr[i]=arr[minimumIndex];
arr[i]=temp;
There are two issues I see. One is the way you are swapping the items. You need to replace the item where you found the minimum index. Also, your J index should start one after your I index. You can assume that the one before it is the smallest as you are looping through. I have changed a few pieces of the code and tested it and it works fine for me.
for (int i = 0; i < arr.length - 1; i++)
{
int minimumIndex = i;
for (int j = i + 1; j < arr.length; j++)
{
if (arr[j] < arr[minimumIndex])
{
minimumIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minimumIndex];
arr[minimumIndex] = temp;
}

Declaring a 2D array with 2 for loops: Java

The question is:
Create a method display2DArray().
a) Inside the method, declare a 2D array that will hold the following integers:
{10,20} {11,21}
{15,25} {17,28}.
b) Display this information using two for loops.
public static void display2DArray()
{
int[][] arrays = new int[][]
{
{10, 20}, {11,21}, {15,25}, {17,28}
};
for(int i = 0; i < 3; i++)
{
for(int j = 0; i < 1; j++)
{
System.out.println(arrays[i][j]);
}
}
}
This is what ive come up with, but its not correct.
Can someone tell me what i need to be doing?
You are almost there!
Few things:
1) Typo - In your inner for loop, you are using an "j" instead of "i".
2) The same "j" must be j<=1 OR j<2 because you have 2 columns i.e. 2 elements in each sub-array. So the indexes will be 0 and 1.
3) In your outer for loop, you are using i<3. Since you have 4 rows i.e. 4 sub arrays, your indexes will be 0,1,2,3. So you need to use i<=3 OR i<4.
4) You can print an empty line in the outer for-loop for a better display.
for(int i = 0; i <= 3; i++) // Since you have 4 rows, indexes would be 0,1,2,3
{
for(int j = 0; j <= 1; j++) // Since you have 2 columns, indexes would be 0,1
{
System.out.print(arrays[i][j]+","); // Print each row i.e. sub-array
}
System.out.println(""); // Print an empty line after each row
}
This gives you the output:
10,20,
11,21,
15,25,
17,28,
In your second for loop, you have "i < 1" instead of "j < 1"
Use j in second array
public static void display2DArray()
{
int[][] arrays = new int[][]
{
{10, 20}, {11,21}, {15,25}, {17,28}
};
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 2; j++)
{
System.out.println(arrays[i][j]);
}
}
}
Also the boundaries were wrong
Your Problem with the inner loop the you used . You used i instead of J. So, Your loop will not give you the result as you Expected.
for(int i = 0; i < 3; i++)
{
for(int j = 0; j <= 1; j++) //use j instead of i here.
{
System.out.println(arrays[i][j]);
}
}
Thanks
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j <= 1; j++)
{
System.out.println(arrays[i][j]);
}
}
1.) second for loop condition check is j< 1 not i< 1
2.) first for loop condition check is i<=3

2D array populating not working

I'm trying to populate a 2D array with char's from a string I've read in. I'm having a problem with actually populating this 2D array. It keeps printing a 2D array bigger than what I've given it, and the number always seems to be 6 rather than the letters from the string.
I store the string in an ArrayList called tempArray.
Input strings:
WUBDLAIUWBD
LUBELUFBSLI
SLUEFLISUEB
I instantiate a 2D array with columnlength = 11, and rowcount 3
epidemicArray = new int[rowCount][columnCount];
Array before I try to populate it:
00000000000
00000000000
00000000000
My code:
public static void updateArray(){
//extract string from temp
for (int i = 0; i < tempArray.size(); i++){
String temp = tempArray.get(i);
char[] charz = temp.toCharArray();
for (int j = 0; j < charz.length; j++){
for (int k = 0; k < rowCount; k++){
for (int l = 0; l < columnCount; l++){
epidemicArray[k][l] = charz[j];
}
}
}
}
}
Output: Which I didn't expect
6666666666666666666666
6666666666666666666666
6666666666666666666666
Expected output: (2D array)
WUBDLAIUWBD
LUBELUFBSLI
SLUEFLISUEB
Thanks, this is really bugging me.
Change your code to this:
public static void updateArray(){
//extract string from temp
for (int i = 0; i < tempArray.size(); i++){
String temp = tempArray.get(i);
char[] charz = temp.toCharArray();
for (int j = 0; j < charz.length; j++){
epidemicArray[i][j] = charz[j];
}
}
}
This edit should work since the number of columns is the length of one of the string (same length for the 3 of them).
Here is my output
[EDIT]. #magna_nz, I used the following methods to print the array
public static void printRow(int rowNumber) {
for (int i = 0; i < 11; i++) {
System.out.print( epidemicArray[rowNumber][i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
updateArray();
for (int i = 0; i < 3; i++) {
printRow(i);
}
}
This will print the numbers, but if you want to print characters you can change the above printRow method to something like:
public static void printRow(int rowNumber) {
for (int i = 0; i < 11; i++) {
System.out.print( (char)epidemicArray[rowNumber][i] + " ");
}
System.out.println();
}
And this will give you the following result:
You're overwriting your entire epidemicArray with the last value that charz[j] gets. Which is apparently 66. Actually you're overwriting that entire array with every value from charz and the last one won.

Finding the index of a multidimensional array?

What would be the last valid index for this array?
double[][] array = new double[11][17];
The answer is [10][16], let's see how in code.
public static void main(String[] args) {
double[][] array = new double[11][17];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("[%d][%d]%n", i, j);
}
}
}
And the last line of output is
[10][16]
Or, you could remember that array indexes start at 0 and the last element of an array is at the length of the array - 1.

Categories

Resources