Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with array

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    }
    }

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default 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

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

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

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


    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);
    }
    }

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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
    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
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Return an array of Pairs that have the same length as the input array of Strings.
    By hemla in forum Object Oriented Programming
    Replies: 3
    Last Post: August 8th, 2013, 08:17 AM
  2. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  3. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  4. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM