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
Printable View
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
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.
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);
}
}
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
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
@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.
Other comments:
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:
You'll find consistently using this best practice will eliminate many sources of errors that become hard to find in a large source file.Code java:
if (numbers[index] > max) { max = numbers[index]; }
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); } }
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
Your algorithm:
2 is even, ignore
4 is even, ignore
5 is odd: increment odd to 1, sum = 1 + 5 (6)
3 is odd: increment odd to 2, sum = 2 + 3 (5)
Clearly 5 is not correct answer