Help with array

• September 14th, 2013, 03:43 PM
Flying_Tiger
Help with array
can i use an array for two things for example if i want to enter 5 numbers and first thing find the lowest number and second thing to find the sum of even numbers
• September 14th, 2013, 03:49 PM
GregBrannon
Re: Help with array
Of course. You can do as many things with the elements (contents) of the array as you wish. You can even do those same things multiple times. The elements never get used up. It's magic.
• September 14th, 2013, 05:07 PM
Flying_Tiger
Re: Help with array
Greg Thank you for your reply so far i did the first one as find the largest number in the array but i'm not able to do it for the second one get the sum of even numbers

import java.util.Scanner;
public class tiger {

public static void main(String[] args)
{
Scanner Keyboard = new Scanner(System.in);
double[] numbers = new double[5];
int index;
int odd=0;
double max;
System.out.println("Enter 5 numebrs:");
numbers[0] = Keyboard.nextDouble();
max = numbers[0];
for (index = 0; index < 5; index++)
{
numbers[index] = Keyboard.nextDouble();
if (numbers[index] > max)
max = numbers[index];

}
System.out.println("The highest Number is " + max);
}
}
• September 14th, 2013, 06:09 PM
camel-man
Re: Help with array
First off you need to use the for loop before you make max=numbers[0];

Pseudocode

Prompt user to enter 5 numbers
loop getting 5 inputs from the user
calculate
• September 15th, 2013, 10:57 AM
urbatin
Re: Help with array
to get it to search for the second number, the first method I could think of was to have 2 counters, 1 to hold the highest number and the second for the next largest number

loop through the array
first number test should go into the highest number counter
on the second loop it will compare that number to the highest, if its less, it go into the 2nd largest number. else, the first number goes to the 2nd largest counter and the number your testing goes into the highest counter.

once it goes through all the numbers you have it return the second counter
• September 15th, 2013, 11:29 AM
GregBrannon
Re: Help with array
@urbatin: What? Search for what second number? Did you read the original post?

@Flying_Tiger: Please post your code in code tags. You can learn how here.

You can iterate the array multiple times if it makes more sense to you. The first time through, find the largest number, which I think you're doing.

The second time through, find the even numbers and sum them as you go. You can find the even numbers using the modulo/remainder (%) operator.

You can do both with one pass through the for loop. Try whichever approach your comfortable with.

Why the extra Keyboard.nextDouble(); statement before the for loop? This requires the user to enter 6 numbers rather than the desired 5. Oh, I'll bet it's so that you can set the max value. Well, then reduce the number of numbers collected during the for loop by 1. This complicates adding the even number task to the for loop a tiny bit, but you should be able to overcome the complication.

Respect Java naming conventions and capitalize the first letter of your class names. Variables and method names begin with lower-case letters. Keyboard should be 'keyboard'.

Avoid using "magic numbers" to the greatest degree possible. Instead of using '5' as the limit of your for loop, use numbers.length.

It's an accepted best practice to enclose ALL statement clauses in braces, '{}', even when they're only one line long:
Code java:

```if (numbers[index] > max) { max = numbers[index]; }```
You'll find consistently using this best practice will eliminate many sources of errors that become hard to find in a large source file.
• September 16th, 2013, 11:03 PM
Flying_Tiger
Re: Help with array
Ok thank you for you help here what i done so far but still cannot get the sum of the odd numbers so any suggestion;

Code Java:

```import java.util.Scanner; public class lab01 {   public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int[] n = new int[10]; int i=0; int odd=0; int sum = 0; int max =0; System.out.println("Enter 10 numebrs:"); n[0] = keyboard.nextInt(); max = n[0]; for (i = 1; i< n.length; i++) { n[i] = keyboard.nextInt(); if (n[i] > max) max = n[i]; } for (i=0; i <n.length; i++) { if (n[i] %2!= 1); odd ++; sum = odd + n[i]; } System.out.println("The highest Number is " + max); System.out.println ("The sum of odd numbers is " + sum); } }```
• September 17th, 2013, 12:18 AM
Junky
Re: Help with array
The variable odd is counting how many odd numbers there are. Why are you including this count in the sum?

arr = {2,4,5,3} The sum of odd numbers is 5 + 3 = 8