Finding Lowest Value In Array Using Linear Search?

Apr 5, 2014

I am attempting to find the element that holds the lowest time ( i have used System.currentMillisTime ) in the array using a linear search and to then print the times held by the array in lowest to highest order . While i understand how to do a linear search using user input as the key i am not to sure how to do it by initializing a search key in the program to the lowest number and have little experience in using a search in a program that is not a simply linear search. i have attempted to code this, as seen below, but i know i am definitely wrong and i have tried another of different ways even Array.sort and then a binary search .

static long store_MAX[]; // Now an ‘array’ of ints
static int deptSize; // Holds the length of the buffer
private int shopper_MAX; // Holds the number of items in the buffer

[Code].....

View Replies


ADVERTISEMENT

Make Linear Search Method To Check If Number N Is Included In Array

Jun 8, 2014

im trying to make a linear search method to check if a number 'n' is included in an array.

PHP Code:

package test;
public class Recursion {
public static void main(String[] args) {
}
static int linearSearch(int n, int[] array)

[code]....

Im getting the following error: this method must have a result type of type int ?? i already have a return type of type int...

View Replies View Related

Linear Search Of Arraylist By Last Name

Nov 5, 2014

I am having some trouble with linear search of a customers last name. I can't seem to get it to work. I have looked up examples but cant find many with Array Lists. Here is my code.

import java.util.ArrayList;
import java.util.Scanner;
public class AccountDriver {
public static void main(String[] args) {
ArrayList<Account> al = new ArrayList<Account>();
ArrayList<Customer> cust = new ArrayList<Customer>();
Scanner scan = new Scanner(System.in);

[code]....

View Replies View Related

Display Lowest And Then Next Lowest Number From Array

Jan 5, 2015

I am not getting the result I am looking for, but I'm sure I'm on the right track. I have an array, names + times of marathon runners, and should end up with a program that displays the slowest runner, and the next-slowest runner.

Currently, I get this:
The lowest time is 273.
The lowest time is 243.

Changing names.length to times.length changes nothing.
for (i = 1;... ) changes nothing.

The displayed number should currently only be 243. Why is it displaying 273 first, and at all?

The code:

class Marathon {
public static void main (String[] arguments) {
String[] names = {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"};
  int[] times = {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265};
  int min = times [0];
  for (int i = 0; i < times.length; i++) {
if (times[i] < min){
min = times[i];
System.out.println("The lowest time is " + min +".");
}

View Replies View Related

Finding Height Of Binary Search Tree?

Nov 13, 2014

So for an assignment I have to write a method that finds that height of this BST. I wrote this:

public int height(Node node)
{
if (node == null)
{
return 0;
}
else
{
return 1 +
Math.max(height(node.left),
height(node.right));
}
}

Which makes sense in my head. However, I added a couple of nodes in and realized I'm not sure how to call this method.

public static void main(String[] args) {
Tree<String, String> tree = new Tree<String, String>();
tree.add("A", "1");//some added stuff
tree.add("B", "2");
tree.add("C", "3");
tree.add("D", "4");
tree.add("E", "5");
tree.add("F", "6");
tree.add("G", "7");
tree.add("H", "8");
System.out.println(tree);
System.out.println(tree.height());

What exactly do I put in the parenthesis after tree.height? If you need to see more of my code I can do that but it is quite lengthy.

View Replies View Related

Calculate Lowest Value Of Array

Feb 13, 2015

So I have this code to calculate the lowest value of an array:

public class Exercise1 {
public static void main(String[] args) {
int[] theArray = {1/*/,2,3,4,5,6,7,8,9,10/*/};
int result = Exercise1.min(theArray);
System.out.println("The minimum value is: " +result);

[code].....

and I need to write an exception class that should be thrown if the array does not hold any elements.

View Replies View Related

Return Lowest Value From Array

Aug 12, 2014

I am trying to return the lowest value from the array. having trouble trying to capture the return value by placing it into the variable ..

int myLowest = getLowest(yourNumbers); and then printing out myLowest

import java.util.*;
public class numArrays
{
public static void main(String[] args){
int numbers;
int[]yourNumbers;

[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

Calling Methods For Java GradeBook - Calculate Highest And Lowest Grades In Array

Apr 15, 2015

In this project each individual will create a data analysis program that will at a minimum,

1) read data in from a text file,
2) sort data in some way,
3) search the data in some way,
4) perform at least three mathematical manipulations of the data,
5) display results of the data analysis in numeric/textual form, and
6) display graphs of the data. In addition,
7) your program should handle invalid input appropriately and
8) your program should use some "new" feature that you have not been taught explicitly in class.

(Note: this is to give you practice learning new material on your own - a critical skill of today's programmer.) If you do not have a specific plan in mind for your project, below is a specific project that meets all of the qualifications as long as 7) and 8) are addressed in the implementation.

Everything is done except I need to call my methods in my GradeTester.

GradeBook:

/**
*This class creates an array called scores.
*This class determines the length of the array scores and determines the last grade in the array scores.
*This class sorts the array using a bubble sort, and searches the array.
*This class calculates the mean, standard deviation, and the median of the grades in the array scores.
*Once the grades in the array is sorted, the class then calculates the highest and lowest grades in the array.
*/

public class GradeBook {
public final int MAXARRAY_SZ = 20;
double [] scores = new double [MAXARRAY_SZ];
int lastGrade = 0;
double mean = 0;

[Code] ....

View Replies View Related

Finding A Value In 2D Array

Dec 3, 2014

I have assigned random numbers to an 2D array, and I had to write a method that prompts the user for a number between 100 and 200. If that number is in the array, it should return 1. If not, 0. It only stays 0 though, even when the number matches one of the numbers in the array.

public static int Findit(int[][] numbers, int guess) {
int Findit = numbers[0][0];
int i = 0;
int x = 0;
boolean found = false;
for(i=0;i<numbers.length;i++)

[Code] ....

View Replies View Related

Finding A String In Array

Oct 4, 2014

My current problem is when im trying to choose case 2 and call markItemOnLoan i cant seem to find the title i want after i enter a few. I will include all of my code but i have mentioned what I am currently stuck on :

import java.util.Scanner;
public class Library {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MediaItem mCall = new MediaItem();
Library call = new Library();// makes instance

[code]....

View Replies View Related

Finding Sum Of Two Dimensional Array

Apr 1, 2014

How to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3. After this I need to add up all the element in this array and return the value.

final int ROWS = 10;
final int COLS = 10;
int[][] foo = new int[ROWS][COLS];
for (int i = 0; i < ROWS; i++){
for(int j = 0; i< COLS; i++){
foo[i][j] = 3;
}
}

This is what i have done so far(not 100% if is correct..) but how to add up all the array.

View Replies View Related

Finding Median Of Array?

May 3, 2014

I'm trying to find the median of a set of numbers inputted into an array and I wanted to know how I would pass the array to the median calculator after I use selection sort to organize the numbers.THIS IS THE CODE THAT ORGANIZES THE ARRAY:

public void selectionSort(double[] myArray) {
for (int i = 0; i < myArray.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < myArray.length; j++)

if (myArray[j] < myArray[index])

[code]....

The code that finds the median will only work if the array being used is already organized. How do I pass the new sorted array to the code that finds the median?

View Replies View Related

Finding Minimum Value Of Array

Sep 11, 2014

I'm almost finished with my assignment, I'm just having one small issue. The goal of this assignment is to have a user input as many values they want(up to 10) into a list. I need to store those in an array then figure out the sum of the numbers, the average, the Largest value, and the smallest value. I get everything to print out correct so far except for the Smallest number, it is returning a 0 every time. Here is my code:

import java.util.Scanner;
public class SimpleList{
Scanner in = new Scanner(System.in);
final private static int ARRAY_LENGTH = 10;
private float[] List = new float[ARRAY_LENGTH];
private int count = 0;

[Code] ......

View Replies View Related

Finding Longest Zig-Zag Sequence In Array

Oct 26, 2014

From a given array of positive and negative numbers, I have to find the longest SUB-Array which represents a Zig-Zag sequence...

A Zig-Zag sequence means that one number is possitive, the next one negative, the next one possitive, and so on and so on...

Like this: -1, 4, -5, 6, -9, 2, -9 etc....

So that means, if the array looks like this:

1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3

the longest sub-array which fulfills the requirement is (the part in bold):

1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3

and I only need it's length, which in this case is: 8

View Replies View Related

Finding Average Of Multidimensional Array

Mar 2, 2015

public class StuTest {
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args){
int[][] testScores; // [students][tests]
String[] stuNames;

[Code] ....

The method I am having issues with is the "printStudentReport" method. I am trying to calculate the average of a randomly generated number of students and tests. For my "printTestReport" method, I have to calculate the average of the test by test number.

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

Finding A String Word In Char Array

Feb 13, 2015

I'm trying to find a word in an array of char.....but I'm stuck. How to formulate the code to step through the array and pick out the word. This is what I have so far...

public static void searchAcross(String string, char[][] puzzle) {
// Gets the number of rows in the matrix
int rowLength = puzzle.length;
//Gets the number of columns in the matrix.
int colLength = puzzle[0].length;

[Code] ....

View Replies View Related

Finding Array Index Number Through For Loop

Jan 29, 2014

int[] a = new int[7];
a[0] = 0;
a[1] = 10;
a[2] = 256;
a[3] = 57;
a[4] = 33;
a[5] = -154;
a[6] = 168;

[code]....

What program needs to find is the most biggest number. It does the job, but another task of the program is to find the index of that number . The second loop should do just that, but for some reason, as the loop goes further, it passes through the if statement even though answer "a[i]" is not equal to "answer". The idea is that if a[i] and answer are equal, the "i" should represent the index number.

View Replies View Related

Finding Duplicate Values In Array List

Jul 8, 2014

I am working on this project that wants me to write a program that inputs 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. The only part I am confused about is how to go about checking for duplicate values that the user may enter. And IF the user does input a duplicate value, it should not be stored again.In addition, the value entered should be printed out after it is entered along side the value that have been previously entered by the user such as:

23
23 45
23 45 67
23 45 67 12
and so on.

I am still fairly new at java programming.

import java.util.*;
public class NumberArray
{
public static void main(String[] args) {
// declare an array with 5 elements

[code]....

View Replies View Related

Finding Simple Moving Average With Array

Apr 8, 2014

How can I find out the simple moving average by just using the numbers stored in an array?

View Replies View Related

Finding Fifty Biggest Numbers In The Array?

Feb 12, 2014

How to find fifty biggest numbers in the array?

View Replies View Related

Displaying Random Value Of Integers And Then Finding Missing Integer From Values In Array

Feb 7, 2015

I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.

=Java
import java.util.Random;
public class Task6
{
public static void main(String[] args)
{
int[] numbers = new int[7]; //create array of numbers
Random random = new Random();
boolean displayed = false; //boolean value to determine if number was displayed

[code].... 

View Replies View Related

No Compiler Errors / Finds Largest Number In Array But Not Finding Smallest

Oct 19, 2014

A java program to print 10 random numbers between 512 and 1024 and then finds the smallest and biggest numbers from the list using only one loop

class BSLab4d {
public static void main (String[] args){
int[] nums = new int[10];
int largest = nums[0];
int x = 0;
int smallest = nums[0];
int y = 0;
 
[code]....

View Replies View Related

Search For Key In Integer Array?

Jun 29, 2014

// 1 ***** student writes this method
/** Searches for key in integer array named arr
// arr is an instance variable of the class and has been instantiated and filled with random values.
// @param key value to search for
// @return if key is found, the index of the first element
// in array whose value is key; if key is not found,
// the method returns -1
*/

public int sequentialSearch( int key ) {
// Note: To animate the algorithm, put this method call as the first statement in your for loop
// animate( i, 0 );
// where i is the index of the current array element
return 0; // replace this statement with your return statement
} // end of sequentialSearch

[Code] ....

View Replies View Related

Algebra 2x2 Linear Equations?

Mar 15, 2015

Design a class named LinearEquation for a 2 X 2 system of linear equations:

ax + by = e
cx + dy = f
Where
x =
ed − bf/ad − bc
y =
af − ec/ad − bc

The class contains:

- Private data fields a, b, c, d, e, and f.
- A constructor with the arguments for a, b, c, d, e, and f.
- Six get methods for a, b, c, d, e, and f.
- A method named isSolvable() that returns true if ad−bc is not 0.
- Methods getX() and getY() that return the solution for the equation.

[code].....

how to display the information I need to display. Also I am not so sure that I wrote the code properly, I do not get any errors when I compile it.

View Replies View Related







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