I am quite new to Java and in the process of learning Arrays. What I am trying to do here is to subtract the largest with the second largest element in an Array. I don't need any help with the process of the code (whether it is correct of not), as I believe I can figure this out myself once I have the Array printed in the console.
However, what I am having trouble with is invoking the diff method to work on the array I have given. The following is the code:
package com.Practice;
import java.util.Arrays;
public class Main {
public static int diff(int[] a) {
int largest = 0;
int secLargest = 0;
for (int i = 0; i < a.length; i++) {
if (a.length < 2) {
System.out.println("Array less than 2 elements!");
}
for (int j = 0; j < a.length; j++) {
if (a[i] > a[j]) {
a[i] = largest;
largest = a[i];
}
if (a[i] < largest) {
a[i] = secLargest;
}
}
}
return largest - secLargest;
}
public static void main(String[] args) {
int[] arr = {22, 3, 2, 55, 34, 56, 34, 123, 56, 34, 21, 5, 65};
System.out.println("Original Array of numbers = " + Arrays.toString(arr));
// How do get the diff() method to work through the array given above down below?
for (int i = 0; i < arr.length; i++) {
System.out.println(diff(arr[]);
}
}
}
I would appreciate the help.
Thank you.
You should pass the array without the [], and you are missing a ):
for (int i = 0; i < arr.length; i++) {
System.out.println(diff(arr));
}
instead of:
for (int i = 0; i < arr.length; i++) {
System.out.println(diff(arr[]);
}
I would like to know, how to find out which column in the 2D-array has got the largest sum. How would I approach this?
public static void main(String args[]) {
int[][] array = {
{ 132, 154, 118 },
{ 355, 101, 50 },
{ 432, 143, 365 },
{ 462, 234, 185 }
};
}
You could go with a nested for loop
int maxCol = 0;
int valOfMaxCol = 0;
for(int i = 0; i < 3; i++){
int sum = 0;
for(int j = 0; j < array.length; j++){
sum += array[j][i];
}
if(sum > valOfMaxCol){
valOfMaxCol = sum;
maxCol = i;
}
}
System.out.println("Max Col is " + (maxCol + 1) + " with the value " + valOfMaxCol);
I am implementing Selection Sort Algorithm in java using ArrayList.
Algorithm I implemented is correct but I am not getting valid output.
can anyone help me please if I am going wrong with this Arraylist.
code :
import java.util.*;
public class SelectionSort {
public static void main(String[] args) {
ArrayList <Integer> array = new ArrayList<Integer>();
array.add(50);
array.add(30);
array.add(10);
array.add(60);
array.add(40);
System.out.println(array);
selsort(array);
}
private static ArrayList<Integer> selsort(ArrayList<Integer> array){
int i = 0;
int imin = 0;
int len = array.size();
for (i = 0;i <len-1; i++){
imin = i;
for (int j = i+1; j<len; j++) {
if ((Integer) (array.get(j)) < (Integer) (array.get(imin))){
imin = j;
}
Collections.swap(array,i,j);
}
}
System.out.println(array);
return array;
}
}
output :
[50, 30, 10, 60, 40] //before
[40, 60, 10, 30, 50] //after
You are swapping the elements with the wrong indices in the wrong place.
The correct swap is i with imin.
The correct place is outside the inner loop:
private static ArrayList<Integer> selsort(ArrayList<Integer> array){
int i = 0;
int len = array.size();
for (i = 0; i < len - 1; i++) {
int imin = i;
for (int j = i + 1; j < len; j++) {
if (array.get(j) < array.get(imin)) {
imin = j;
}
}
Collections.swap(array,i,imin);
}
System.out.println(array);
return array;
}
[50, 30, 10, 60, 40]
[10, 30, 40, 50, 60]
So here's the deal I was messing around with Java trying to make a programme that would take a given int[] and make another int[] which would be in ascending order...
so here the code:
import java.util.*;
public class Accending_order {
public static void main(String args[]) {
int[] dArray = {
1, 34, 25, 67, 35, 68, 88
};
int[] oArray = new int[200];
int[] countOf = new int[200];
for (int i = 0; i < dArray.length; i++) {
int NumForLoop = dArray[i];
for (int j = 0; j < dArray.length; j++) {
int diff = 0;
if (j != i) diff = NumForLoop - dArray[j];
if (diff < 0) countOf[i]++;
}
for (int k = 0; k < dArray.length; k++) {
oArray[k] = dArray[dArray.length - countOf[k]];
}
for (int i2 = 0; i2 < oArray.length; i2++) {
System.out.print(oArray[i2]);
}
}
}
}
and this the ERROR it's showing :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Accending_order.main(Accending_order.java:19)
so help.....
the problem is here
oArray[k] = dArray[dArray.length - countOf[k]];
when countOf[k] = 0, you are trying to access dArray[dArray.length] and dArray.length is 7, but your array contains elements at indexes 0..6
What I'm trying to do is print the largest number within a two dimensional array and it's index location. I'm able to find the largest number, but I can't seem to figure out how to print it's index location. Anyway, here's what I have so far:
public static void main(String[] args) {
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
double max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}
System.out.println(max);
System.out.println(i + j); //No idea what I should be doing here, just trying out everything I can think of
Right now, you should consistently get 2 * arr.length as the final value. That isn't what you are probably looking for. It looks like you want to know the coordinates for the max value. To do this, you'll need to cache the values of the indexes and then use them later:
public static void main(String[] args) {
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int tmpI = 0;
int tmpJ = 0;
double max = arr[0][0];
// there are some changes here. in addition to the caching
for (int i = 0; i < arr.length; i++) {
int[] inner = arr[i];
// caches inner variable so that it does not have to be looked up
// as often, and it also tests based on the inner loop's length in
// case the inner loop has a different length from the outer loop.
for (int j = 0; j < inner.length; j++) {
if (inner[j] > max) {
max = inner[j];
// store the coordinates of max
tmpI = i; tmpJ = j;
}
}
}
System.out.println(max);
// convert to string before outputting:
System.out.println("The (x,y) is: ("+tmpI+","+tmpJ+")");
Be careful with your array dimensions! The second for-statement most of you have is wrong. It should go to up to arr[i].length:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
tmpI = i; tmpJ = j;
}
}
}
Store i, j whenever you update max.
This would be if you wanted a single index into a flatten array:
public static void main (String[] args) throws java.lang.Exception
{
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int[] flattened = new int[6*3]; // based off above
int maxIndex = 0;
double max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
flattened[i + j] = arr[i][j];
if (arr[i][j] > max) {
max = arr[i][j];
maxIndex = i+j;
}
}
}
System.out.println(max);
System.out.println(flattened [maxIndex]);
}
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int max = arr[0][0];
int maxI = 0, maxJ = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
maxI = i;
maxJ = j;
}
}
}
System.out.println(max);
System.out.println(maxI + "," + maxJ);
You've got a two-dimensional array, therefore you need to know both indexes. Adding them together won't do because you lose which-is-which. How about this:
System.out.println("[" + i + "][" + j + "]");
//C++ code
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> b;
vector<int> c;
int Func(int a[][10],int n)
{
int max;
max=a[0][0];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
b.push_back(i);
c.push_back(j);
}
}
}
b.push_back(0);
c.push_back(0);
return max;
}
void display(int a[][10],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int a[10][10],n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<endl;
display(a,n);
cout<<endl;
cout<<Func(a,n)<<" is the greatest "<<endl;
if(b.size()==1&&c.size()==1)
{
cout<<"Location is (1,1)"<<endl;
}
else
{
b.erase(b.end() - 1);
c.erase(c.end() - 1);
cout<<"Location is "<<"("<<b.back()+1<<","<<c.back()+1<<")"<<endl;
}
return 0;
}
You're just adding the indices i and j together and then printing it to the screen. Since you're running throug the entire loop it's just going to be equal to 2*arr.length-2. What you need to do is store the values of i and j when you encounter a new max value.
For example:
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int max = arr[0][0]; //dunno why you made it double when you're dealing with integers
int max_row=0;
int max_column=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
max_row=i;
max_column=j;
}
}
System.out.println("The max is: "+max+" at index ["+max_row+"]["+max_column+"]");
Don't sure that you implement effective algorithm, but why you just don't save indices i,j in another variables when you set max.
This is very simple.
if (arr[i][j] > max) {
max = arr[i][j];
maxX = i;
maxY = j;
}
FYI If you want look at "insertion sorting" algorithms if you want better implementation.