# Array Issues

• April 26th, 2012, 06:58 PM
daemonlies
Array Issues
Hi everyone,

Only new to programming and having issues with arrays and inputing values into that array.

My mission is to ... select a random number .... get the user to guess that random number ... and from what inputs the user has inputed, find the middle number of that array length.

I have worked out how to generate the random number and guess the random number from a previous method, but i cant work out how to implement the guessed numbers into an array to find the middle number.

Could someone please point me in the right direction .. as to how i would acomplish this .... i dont have the code i'm working on as im in transit ... but will post when available.

Any help would be greatly appreciated
• April 26th, 2012, 07:07 PM
arsparfven
Re: Array Issues
I don't know how your methods are set up, but if you have a global array of integers ( int[] guesses or something like that ), each the user inputs a guess, which has to be looped somehow I assume, you could add that value to the array. To do this, you increment a counter each time they guess, and make a statement in the loop where they guess (or method, if it's separate) such as guesses[counter] = guess (guess being the integer that was input by the user).

This could be done with doubles in the same way, if you're using doubles.
• April 26th, 2012, 07:34 PM
Norm
Re: Array Issues
Quote:

find the middle number of that array length.
Quote:

how to implement the guessed numbers into an array to find the middle number
Can you define what a "middle number" is? Perhaps give an example.
• April 26th, 2012, 08:05 PM
daemonlies
Re: Array Issues
the middle number is the 'middle number of the array length ... e.g. 1 2 3 4 5 .. so 3 would be the middle number
• April 26th, 2012, 08:10 PM
Norm
Re: Array Issues
Your example is an array with an odd number of elements. What if there are an even number?
Is the middle element = (array.length+1)/2

Or are you talking the the value of the element?
• April 26th, 2012, 08:23 PM
daemonlies
Re: Array Issues
yes even numbers would be considered as well .. and the value of the element doesnt matter .. i just need the middle element of an array
• April 26th, 2012, 08:27 PM
Norm
Re: Array Issues
Which is the middle element in an array with an even number of elements?
• April 26th, 2012, 08:36 PM
daemonlies
Re: Array Issues
from the information provided to us ... that the middle number of an array if an even number of elements existed, would be 2 elements e.g. 1 2 3 4 5 6 middle numbers = 3,4
• April 26th, 2012, 08:38 PM
daemonlies
Re: Array Issues
I'm still not understanding how this would be done .... could you provide some coding to help please arsparfven !
• April 26th, 2012, 08:40 PM
Norm
Re: Array Issues
Quote:

how to implement the guessed numbers into an array to find the middle number
Quote:

from what inputs the user has inputed, find the middle number of that array length.
Not sure what those mean. The "middle" number(s) is a function of the size of the array. There is no need for guessed numbers to find it.
• April 26th, 2012, 08:46 PM
daemonlies
Re: Array Issues
Sorry if ive confused people ... the guessed numbers are what a user inputs into the program to guess a random number(guessed numbers could be 1 to whatever until the user gets the correct random number). Those guessed numbers need to be implemented into an array to be able to find the middle number/s of that array.
• April 26th, 2012, 08:47 PM
arsparfven
Re: Array Issues
I know for just finding the middle index, you could use the line of code Norm put up for you..."middle element = (array.length+1)/2"
If you have a count, you could use "count/2" as count would essentially be the same as the length.
If you need two, you're going to have to check with some if statements.

IF the length can be divisible by two (using the mod operation, like length % 2 = 0) THEN the middle is the length divided by two.
IF the length cannot be divisible by two (length % 2 != 0) THEN the middle is floor(length/2) and floor(length/2) + 1

If you are using integers, you can just use integer division (floor will not be necessary), if you are using doubles, you need to use floor. Floor is a method from the Math class. You need to import that above your class and the formal code would be Math.floor(operation). It's just a method of rounding, it will give you the lower integer, if you look it up, it's a very common java operation if you want to understand it in depth
• April 26th, 2012, 08:50 PM
Norm
Re: Array Issues
Still not clear what you are trying to do with an array. Given an array, by your definition, the middle can be found from the length of the array.
Where do the guessed numbers get involved?
Can you give an example of what you mean?
Say there is an array with 25 elements
and the user guesses: 44, 33, 5,77,55,22,4,7
What is supposed to happen?
• April 26th, 2012, 11:44 PM
daemonlies
Re: Array Issues
Hey Norm,

From a previous method i have a 'random number' and a 'users guessed numbers' , lets say they users guesses are ... 65, 75, 70, 67, 69, 68 .. which the last element being 68 and the user has guessed the correct random number being the last element 68. I need to be able to implement those guessed number into an arrray but not sorted, so i can find the middle number/s e.g. 70 and 67.
• April 27th, 2012, 06:48 AM
Norm
Re: Array Issues
The middle number would be found by some formula like: (the count of numbers entered+1/2) with a test if there are an even count and then there would be two middle numbers.
An example: with 6 numbers entered: (6+1)/2 = 3 and because there are an even number also 4.
• April 28th, 2012, 08:40 PM
daemonlies
Re: Array Issues
Any help ... I've worked out how to process the middle and median but i cannot work out how to continur to input numbers into the array list, it will one process one number ... I also cant work out how to stop adding numbers to the array if a preset value is entered .e.g randomNumber = 45.

import java.util.Arrays;
import java.util.Scanner;

public class ArrayTest
{
Scanner input = new Scanner( System.in );

int usersGuess;
int randomNumber = 45;

public void enter()
{

usersGuess = input.nextInt();
}

public void testArray()
{

int testArray [] = new int[ 20 ];

for ( int i = 0; i < testArray.length; i++ )
testArray[ i ] = usersGuess ;

System.out.println( "Middle is: " + testArray[ (testArray.length+1)\2] );

System.out.println();

Arrays.sort( testArray );

System.out.println( "Median is: " + testArray[ testArray.length/2] );
System.out.println();

}

}

public class ArrayTestArgs
{
public static void main( String [] args )
{
ArrayTest arrayTest = new ArrayTest();

arrayTest.enter();
arrayTest.testArray();

}

}
• April 28th, 2012, 08:44 PM
Norm
Re: Array Issues
Quote:

how to stop adding numbers to the array if a preset value is entered
Use an if statement to test the value entered against the preset value and if equal, use the break statement to exit the loop.

The order of the statements in the program do not make any sense. Before you write any more code you need to get organized. Make a list of what the code needs to do and the order it needs to do it. Write the list in pseudo code and post it. When the steps make sense then write the code.

• April 28th, 2012, 08:48 PM
daemonlies
Re: Array Issues
Ok .. so what will i do about the input not continuing to add numbers .. if i enter say '5' it doesnt ask for anymore entered numbers it justs calucaltes the median and middle
• April 28th, 2012, 08:49 PM
Norm
Re: Array Issues
• April 28th, 2012, 09:18 PM
daemonlies
Re: Array Issues
Hey Norm .. sorry but you mention about a puesdo list .. can you enlighten me on this please as i have no idea
• April 29th, 2012, 06:52 AM
Norm
Re: Array Issues
Here's a sample of pseudo code for some program:
begin loop