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


ADVERTISEMENT

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

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 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

How To Find A Path Through Array Maze

Mar 4, 2015

So what I'm trying to do is write a code in java which finds a path through a maze. I want it to go through the maze and determine if there's a * symbol at that location or not. If there is a * symbol at the specified location then the program would search for another position until it finds one without the * symbol and if it can't then I'll have the program return null. I also want it to implement backtracking which I'm not sure how to do. Here's my code so far

private boolean findPath(int fromRow, int fromCol, int toRow, int toCol){
boolean solved = true;
for(int i=fromRow; i<toRow; i++){
for(int j=fromCol; j<toCol; j++){
if (x[i][j] == ('*')){
//do something
}
}
}
return false;
}

the code isn't finished yet however what I'm not sure is what do I do with the if statement and how do I implement backtracking?

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 Arithmetic Mean Of The Numbers Stored In Array

Feb 23, 2014

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();
for ( double x : q ) {
d.add( x );
e.add ( new Double ( x ) );

[Code] .....

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.

View Replies View Related

Logic To Find Adjacency Of Numbers 0 And 1 In Array

Oct 3, 2013

I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)

I attempted at the problem like so :

public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {

[Code] .....

To me it looks right and it seems to work.

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

Trying To Find 3 Largest Numbers In Array Using Single For Loop

Feb 5, 2014

I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers.

public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);

[Code] ....

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

Loop That Calculates Maximum / Minimum And Average Of 10 Input Numbers?

Feb 26, 2014

It does not calculate the maximum, minimum or average of the 10 numbers that the user is prompted to input.

My code:

import java.util.Scanner;
public class moretest {
public static void main( String [ ] args ) {
int total = 0;
int number;
int minGrade = 101;

[code]...

View Replies View Related

Pair Of Two Numbers - Return Average / Distance / Maximum And Minimum Value

Sep 27, 2013

I have been having difficulty with the weeks concepts in my online Java class, the program is to be as followed:

For this exercise you will implement a class called Pair, that represents a pair of two numbers.The Pair class should include the following constructor and methods:

CONSTRUCTORS
public Pair(double num1, double num2) -- Creates an object that represents a pair of double values

METHODS

public double getAverage() -- Returns the average value of the two numbers
public double getDistance() -- Returns the absolute vale of the distance between the two numbers
public double getMaximum() -- Returns the maximum value of the two numbers
public double getMinimum() -- Returns the minimum vale of the two numbers

Write a class called PairTest that tests your Pair implementation. The PairTest should prompt the user for the two values, create a Pair object with the values and then print the average, distance, maximum, and minimum of the pair. The input / output should look like the following:

Enter the first number: 5.5
Enter the second number: 3.0

Average: 4.25
Distance: 2.5
Maximum: 5.5
Minimum: 3.0

NOTE: For this exercise, your solution should not use any conditional statements. Instead you should use the methods provided by thejava.util.Math.

So far I have:

import java.lang.Math;
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
{
System.out.println("Please enter a value for the first number");

[Code] ....

View Replies View Related

Print Triangle Shape Using Numbers?

Jun 1, 2014

i want to print triangle shape using number like

this

1

12

123

1234

12345

123456

this is my code

class shape{
public static void main(String [] args){
for(int x =0 ; x<=6;x++){
for(int y =0 ; x > y ; y++){
System.out.print(x);
}
System.out.print("
");
}
}
}

but my output is

1

22

333

4444

55555

666666

View Replies View Related

Find Area Of Triangle / Square / Rectangle Or Trapezoid

May 19, 2014

I started with finding the area of a triangle, but now I'm trying to ask a user what kind of shape they want the area for, then ask questions to get the area. I can't figure out how to take the shape a person types to go to a certain case. It also says shape hasn't been initialized. I don't know how to do that.

import java.util.Scanner;
public class TriangleArea {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
char shape;
String text = "Do you want to find the area of a triangle, square, rectangle, or trapezoid?";
System.out.print("Text");
switch(shape){

[code]....

View Replies View Related

The System Cannot Find The Path Specified

Feb 25, 2015

I have the Java Development Kit downloaded in my C file and my book tells me to compile the program I need to open command windowand change the directory where the program is stored. I tried the command cd to change directory and received this message "The system cannot find the path specified."

I checked the Environment Variables on Windows 7 and the Path says: C:Program Files (x86)Javajre1.8.0_31in

This is after many tries and i still can't change directory and i keep getting the same message.

View Replies View Related

System Cannot Find Path Specified

Feb 24, 2015

I have the Java Development Kit downloaded in my C file and my book tells me to compile the program I need to open command windowand change the directory where the program is stored. I tried the command cd to change directory and received this message "The system cannot find the path specified." I checked the Environment Variables on Windows 7 and the Path says: C:Program Files (x86)Javajre1.8.0_31in

This is after many tries and i still can't change directory and i keep getting the same message.The book I am using to learn Java is "Java How to Program: Tenth Edition" from Paul and Harvey Deitel.

View Replies View Related

Unable To Find JDK Path

Sep 19, 2013

I am new to linux and using centos ver 6.4 64bit . i want to install sqldeveloper so i run below rpms
 
sqldeveloper-3.2.20.09.87-1.noarch.rpm
jdk-7u40-linux-x64.rpm
 
After install  when i run sqldeveloper command then it prompt me for jdk path

1. Which I don't know how to find
2. When I try to give /usr/bin/java1.7.0_09/ and enter then it give me below error
 
/usr/bin/java1.7.0_09
Error: /usr/bin/java1.7.0_09/bin/java not found
Type the full pathname of a J2SE installation (or Ctrl-C to quit), the path will be stored in ~/.sqldeveloper/jdk

View Replies View Related

How To Find Longest Path In A Matrix

Feb 18, 2015

I have some N*M matrix or N*N matrx , and there's a "worm" that can start from any index in the first column, or, any index in the first row. i need to choose the longest worm that satisfying this :

The index that comes after the index before him must be greater then 1. and i must use recursion, also for helper methods. no loops at all. that's it. i'm having a problem to find the longest worm in the matrix.

My idea was to create an helper array and assign to the array indexes a variable that will count the steps of the worm and finally assigns in to the helper array index. then i used findMax method to find the max value in an index. and thats will be the longest worm. i wrote a lot of code so i wont put it here. i will say that i'm close. if the longest worm is 6 i get in my output 7.

View Replies View Related

How To Find The Current Path Of A File

Jun 24, 2014

First of all, i am using ubuntu and jdk8. My problem: displaying the current path of a file in my system Approach: I have a file called dummy.txt in a given directory which have enough permissions and i did the following:

File file=new File("dummy.txt");
System.out.println(file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("/")));

I expected to see displayed the current path of the file without the name of the file but it is showing a different path. I just want to display the current path of the file without the name.

View Replies View Related

Simple Agent Find Path?

Feb 13, 2015

i want to build a simple x,y grid in eclipse. Where an simple agent can search for a goal and end the episode.

I have java experience but I cant get my head around something simple as this.

I know there will be state, actions and environment and agent class.

But I cannt figure out where to start with this, do i start with state or action.

View Replies View Related

Trying To Find Cheapest Path Recursively

May 25, 2015

Trying to find cheapest path recursively, given a grid a integers trying to recursively find the cheapest path from the top to the bottom. cheapest being the sum of the path. I think I've made my code over complicated. recursive things are usually much more elegant

import java.io.FileInputStream; 
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException; 
public class trial {
public void readFile(String fileName)
 
[Code] ....

and here is the grid

27 19 18 85 32 11 18 24 22 98
60 83 52 61 18 64 74 33 95 42
56 27 71 56 65 70 18 78 35 74
15 89 19 92 61 76 92 42 57 26
88 28 78 45 21 98 11 72 82 97
49 54 88 79 16 43 27 78 52 71
17 18 60 40 72 39 70 52 96 11 
62 79 25 50 73 40 98 64 44 72
25 79 72 25 64 35 29 16 77 96 
12 93 49 64 61 34 83 87 34 36

View Replies View Related

Returns Area Of Triangle / Unless Triangle Not Value So Return String?

Dec 28, 2014

I'm doing a problem where the area of a triangle is returned (if valid). However, I want to return a message (i.e. 'triangle is not valid) if the triangle is invalid.

I'm not sure how to go about to doing this as my method (called area) will only let me return doubles. Possible to return a string in an else within my area method?

public class MyTriangle {
public static void main(String[] args) {
//triangle is valid if the sum of any two sides is bigger than the third
System.out.println(isValid(3, 4, 5));
System.out.println(area(543, 4, 5));

[Code] ...

View Replies View Related

Finding Path Through 2D Array From Top To Bottom?

Apr 20, 2015

I have a 2D array that is of type int[][]. it is populated with 1's and 0's. I need to create a method that allows me to search from the top row finding a possible path to the bottom--the path is a path of connecting cells containing 1. I need to create an array that stores the cells that are needed to be searched. When the search carries out, for each cell stored it needs to check the cells to the left, right, below and above the current cell.

I also need to create a variable to store the current cell. I thought initially it would be an int but it can't be because it needs to hold the the index of the current cell. and any of the cells searched that are an immediate neighbour of the current cell are added to the storage array.

I have these instructions but I am having trouble converting into code

Finding a path through vegetation from the top row to the bottom row: The idea is we will start from a cell in the top row and advance below for as long as we can. Since we are still not done exploring a cell until we have explored ALL it’s vegetation neighbors, we will add the cell to an array of cellsToExplore and only come back to it if the current cell we are examining is fully explored. Here is pseudo-code of the algorithm !

• Create array cellsToExplore (what size should this array be?)!

• Create an integer count that stores the number of cells in cellsToExplore!

• Go through the top row and add ALL vegetation cells to cellsToExplore!

• Create a variable to store currentCell we are exploring and set it to null.!

• while count > 0 do!

- set currentCell to the last cell in cellsToExplore (last cell added to the array).!

- label currentCell as burning vegetation!

- If the currentCell is on the bottom row then we return true (we found the path).!

- if the cell below currentCell is vegetation then add the cell below to the cellsToExplore array.!

- else if cell to the right of currentCell is vegetation then add the cell to the right to cellsToExplore!

- else if cell to the left of currentCell is vegetation then add the cell to the left to cellsToExplore!

- else if cell above the currentCell is vegetation then add the cell above to cellsToExplore.!

- else remove the currentCell from the cellsToExplore (we are done with this cell). !

• Return false!

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related







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