How To Find Maximum Element Of Each Column In 2D Array

Oct 26, 2014

import java.io.IOException;
public class Largestcolumn
{
public static void main ( String[] args ) throws IOException
{
int largest = 0;
int newnumber = 0;
int[][] data = { {3, 2, 5},

[Code] ....

When I run this code, I get this following output: The largest element in column 0 is: 9.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at largestcolumn.Largestcolumn.main(Largestcolumn.java:27)
Java Result: 1

It outputs the first column's maximum element but then throws an out of bounds error. I'm new to Java and I can't figure out how to fix my code so that it will work for this multidimensional array and output the maximum elements in all of the columns.

View Replies


ADVERTISEMENT

Array Is A Set Of Numbers In Triangle - Find Maximum Path

Aug 12, 2014

If you aren't familiar with the euler problem 67, you are given an array that is a set of numbers in a triangle. Like this

3
7 4
2 4 6
8 5 9 3

and you have to find the maximum path, which for this one is

(3)
(7) 4
2 (4) 6
8 5 (9) 3

I have solved this problem iteratively with the code below

depth = depth-2;
while (depth >=0) {
for (int j = 0; j <= depth; j++) {
values[depth][j] += Math.max(values[depth+1][j], values[depth+1][j+1]);
}
depth -= 1;
}

depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have

public static int findMax(int[][] array,int depth) {
if (depth==0)
return array[0][0];
else if
}

View Replies View Related

User To Input 10 Numbers - Find Smallest Element In Array

Dec 2, 2014

I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.

This is the most current draft of my code:

import java.util.Scanner;
 public class Exercise7_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
//Enter ten double numbers: Scanner(System.in)
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter ten numbers: ");

[Code] .... 
 
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/

And these are the exact error messages:

----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {

[Code] .....

View Replies View Related

Recursion Method With 2d Array To Find A Column With Lowest Sum

Jan 28, 2015

i have to write a method, The method receives a parameter of two-dimensional array of integers. The method returns the number of the column which has the lowest sum of the integers.I'm allowed to use only recursion! no loops allowed!-of course i need to make a private method that will sum a column as a single array and then i have to do another private method that compares the column , but it doesn't really work .

View Replies View Related

How To Find Maximum Of Two Enum Values

Oct 6, 2014

I have a set of enum values (let's call then ONE, TWO, THREE.....). I want to find the larger of two of them. But max(ONE,THREE) gives a compile error as MAX isn't defined for type-safe enums. Fair enough.
 
I also agree that one shouldn't be able to use arithmetic functions on enums.
 
But as Enum implements Comparable, one can write a function which implements max and min, rather inefficiently I assume.
 
Is there a better way of getting the max/min of an enum? And if not, can the Java team be persuaded to implement it?

View Replies View Related

Find Maximum Between Three Numbers Using Ternary Operator?

Aug 23, 2014

Write a program to find maximum between three numbers using ternary operator.

View Replies View Related

Sorting Arraylist Of Calendar Objects To Find Maximum

Oct 14, 2014

I have a requirement to find the greatest/maximum of the given list of Calendar objects in Java.

i.e., 2013/01/26
2014/03/03
2012/02/27
2014/01/15

So the above list of calendar objects are in Arraylist. I need to get the max/greatest among these. Eventually, the answer should be 2014/03/03

View Replies View Related

How To Find Sum Of Each Column

Jan 14, 2014

i want to write a program have a array 2d like that :

int array [][] = new int [3][3]

after that the user input value by using scanner then give summation of each column in matrix , i wrote this one

Java Code:

class MatrixSum{
public static void main(String[] args) {
int matrix1[][]= {{7,8,9},{1,5,2}};
int matrix2[][]= {{1,6,4},{2,7,3}};
System.out.println("Number of Row= " + matrix1.length);
System.out.println("Number of Column= " + matrix1[1].length);
int l = matrix1.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++) {

[code]...

but i want to change that the user input the value by using scanner.

View Replies View Related

Java Program To Find Index Of Greater Element Whose Value Is Sum Of Remaining Elements

Aug 25, 2014

I need to write a java program to find the index of the element whose value is the sum of the remaining elements. Recently I have been asked this question in an Interview which I couldnt solve properly.

View Replies View Related

Find The Column With Highest Average Number

Feb 14, 2015

code a 10 * 10 matrix, with random numbers from 1 to 100,

i) Find the most repeated number.
ii) Use the Binary Sort, to sort the matrix in ascending order.
iii) Find the Column, with the highest average number.

View Replies View Related

Input 10 Integer Numbers Into Array And Determine Maximum Value Entered

Apr 22, 2015

a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)

b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:

The maximum value is: _________
This is element number __________ in the list of numbers

Have your program display the correct values in place of the underlines in the messages.

c. Repeat 1b, but have your program locate the minimum value of the data entered.

I did parts a and b but for part see i just want to know if i did it correctly or not

import java.util.Scanner;
public class MinimumValueArray {
public static void main(String[] args) {
//Variable Declaration
Scanner keyboard = new Scanner(System.in);
int size = 10;

[Code] ,.....

When I run it i get this The minimum value is 0.0

The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?

View Replies View Related

Return Maximum And Minimum Numbers In Elements Of Integer Array

Oct 21, 2014

1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java

2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java

3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)

1 .... 87654321

12 .... 7654321

123 .... 654321

1234 .... 54321

12345 .... 4321

123456 .... 321

1234567 .... 21

12345678 .... 1

(Without the dots, i just put them to give spaces)

View Replies View Related

Accept Array Of Ints And Squares Each Element Of Array

May 13, 2014

I need to write a method that accepts an array of ints and squares each element of the array. No creating new arrays and no returning any values.

public void squareInts(int[] ints) {
for(int i = 0; i < ints.length; i++) {
ints[i] = (ints[i] * ints[i]);
}
}

View Replies View Related

Smallest Element In Array

Nov 7, 2014

I have been working on this assignment for a bit now. I seem to have most of the logic of it down, as far as I can tell, but I seem to have fallen into a bit of a brain lapse when it comes to invoking methods I've made in my main method. Here's the code:

package Module5;
import java.util.Scanner;
public class Exercise6Nine {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 numbers.");
double[] numbers = new double[10];

[Code] .....

My question is, in my main method, I have code in there to attempt to call my method "min" from below, and to use the result of the min's calculations and print them via my main. Only, I can't seem to properly invoke the method from my main method!

Also, if you could check over my min method and see if anything blatantly wrong is there and let me know, that would be great.

Looking over my question, and it seems there is no EDIT button, but I may as well put the assignment parameters here, since I asked for double checking on my method. Write a method that finds the smallest element in an array of double values using the following header:

public static double min( double[ ] array )

Write a test program that prompts the user to enter 10 numbers, invokes this method to return the minimum value, and displays the minimum value.

View Replies View Related

Adding Space Between Each Element Of Array

Dec 8, 2014

I have been trying to space out output on a Java console window so that I have three columns with 6 rows of data from three different arrays. The code I have so far outputs the data with no problem however the spacing between the columns is uneven. My loop so far is made up as follows

for (int i = 0; i < printVotes.length; i++) {
System.out.println(printNames[i] + "
" + printVotes[i] + "
" + printPrecent[i] + "%");
}

As you can see I have been manually adding the space between each element of the array but this means that the space between each element is different because the size of each element is different if work out a loop that works out an even amount of space between the elements and then print this along with the elements ....

View Replies View Related

Adding Element From Arraylist To Array

Sep 11, 2014

So I'm trying to write a method which returns the number of vowel characters in arraylist. My idea is to convert the arraylist element by element to array each time iterating through the array counting the vowels of that element. When I started I immediately got an error(surprise, surprise). Excuse me if the problem is too simple, but I am very new to programming.

At line 9 I get the following error "Type mismatch: cannot convert from String to int". I want to get the element at this position, not to convert to int..

ublic class One {
public static void main(String[] args) {
ArrayList<String> bla = new ArrayList<String>();
bla.add("aaa");
bla.add("brr");
bla.add("unn");
}
public static ArrayList<String> averageVowels (ArrayList<String> list){
String[] arrListWord = list.toArray(new String[list.get(0)]);
return list;
}
}

View Replies View Related

Each Bar In Graph Will Represent Value Of Each Element Of Array

Nov 5, 2014

So I have an array which holds 19 elements, each element represents a value of 'income'. I'm trying to code the graph so that each bar will represent the value of each element of the array (income). I have been given the code ' for (int Bar = 0; Bar < array of values.length; bar++);' however i'm unsure if this is how to do it, or what to add to this code to make it work.

View Replies View Related

Calling Element Of Array From Different Class

Mar 13, 2014

So I just finished up my term project and have everything working but I wanted to make one slight adjustment to the code and Im not exactly sure what I'm doing wrong - it involves retrieving a set element from an array from a different class so to some what show what I have going on:

public class example1 {
private example2 Examp;
public example1() {
Examp = new example2();
} public void getArray() {
if(Var >= 10 && Var <= 20) {

[Code] ....

I have an if statement that looks at a sum of numbers, and predetermined upon the set of numbers I want it to output a message by calling the index number in the array and returning the string. I currently just have the message in the if statements but would be nicer to just pull them from a different class to keep it consolidated.

View Replies View Related

Shuffling Array - Swapping Every Element

Aug 25, 2014

While shuffling an array, if I use Collections.shuffle(), there is a chance that an element in a particular index in the input array can be present in the same index in the output array. Is there an existing method that handles that too? If not, how can I best handle it? After shuffling, will swapping every element with the last element work?

View Replies View Related

Comparing String To Array Element?

Oct 17, 2014

I have the following code:

Java Code:

public class Equals {
String[] s1 = {"red", "white", "black", "blue"};
String[] s2 = {"red", "black", "green"};
String[] s3 = {"red", "green"}; mh_sh_highlight_all('java');

What I need is to give me the following output:

Select Strings: s1,s2,s3
Comparing String s1, s2, s3

red 3 matches.

black 2 matches.

green 2 matches.

View Replies View Related

Print Duplicates Element Of Array

Apr 16, 2014

This code is not best way to find the duplicate elements in a given array. Any alternative method for an optimized code.

Java Code:

import java.util.Arrays;
public class Find_Dupliicate_ArrayElement {
public static void main(String[] args) {
int[] Array1 = {1, 9,8,1,2,8,9,7,10, -1, 1, 2, 3, 10, 8, -1};
// Store the array length
int size = Array1.length;
//Sort the array
Arrays.sort(Array1);

[code]....

View Replies View Related

Comparing String To Array Element

Oct 17, 2014

I have the following code:

public class Equals {

String[] s1 = {"red", "white", "black", "blue"};
String[] s2 = {"red", "black", "green"};
String[] s3 = {"red", "green"};

What I need is to give me the following output:

Comparing String s1, s2, s3

red 3 matches.
black 2 matches.
green 2 matches.

View Replies View Related

How To Extract First Digit From Int Array Element (java)

Nov 18, 2014

How do you extract first int digit from an int array element (java)?

if a[3] = 45, how do I extract 4 out of 45?

View Replies View Related

How To Retrieve Index Of Specific Element In Array

Mar 3, 2015

I'm trying to iterate through an array of integers and find the start position of the part of the array containing the most consecutive 1s.

For example given the array {1,3,1,1,1,1,5,3,2,1,1}, the method should return 2 and given {1,4,5,1,1,1,5,1} the method should return 3.

So far, I've managed to retrieve the element where the consecutive 1s begin. However, I'm unsure how to get the index of this element and this implementation doesn't work if there is more than one set of consecutive 1s.

public class GetIndex {
public static int getPosition(int[] myArray) {
int index = 0;
int tracker = 0;
int mostOnes = 0;
for(int i = 0; i < myArray.length; i++) {

[Code] .....

View Replies View Related

Using Mutator Method To Set Data To Array Element

Jun 2, 2014

the problem I'm having is I want to use a mutator method to set data for an array element. The code I have so far is:

public void addProduct(String productName)
//Goes through and sets the name of a product and assigns it to the array
{
int index;
for (index = 0; index < product.length(); index++)
{
product[index].setName(productName);
numberOfProducts++;
}
}

The array was initialised like this:

Product[] product = new Product[3];

And the setName(String) method is just your typical mutator method.However, in Eclipse, I have an error messages. It is:

"-The method setName(String[]) is undefined for the type String" .....

View Replies View Related

How To Access Element Of Multi Dimensional Int Array Using For Loop

Jul 12, 2014

I have written following code.

I want to print all elements in Array to output here is my code

int[][] arr={{12,12,13},
{14,1223,14}};
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
System.out.println(arr[i][j]);
}
}

but I am getting following output

12
12
14
1223

how to print all elements of int array?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved