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 3 of 3

Thread: need help with arrays

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Durban South Africa
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default need help with arrays

    hello guys please help

    /*1.1 Write a class to do the following:
    a. Declare an array called arr1 to contain 10 cells of type integer.
    b. Write a method to input 10 numbers into the array
    c. Write a method to find and return the sum of the numbers in the array
    d. Write a method to receive the sum. Find and return the average.
    e. Write a method to receive the sum and average, output these values.
    f. Write the test driver class to call up the methods.
    */
    import java .util.*;
    public class Array1
    {
    	public static void main (String [] a)
    	{
    		int [] arr1 = new int [10];
    	}
     
    	public void inputNumber()
    	{
    		Scanner console=new Scanner (System.in);
    		System.out.println("Enter as number");
    		int arr1=console.nextInt();
    	}
    HOW DO I FIND THE SUM (NUMBER C )
    Last edited by jps; August 7th, 2013 at 01:17 PM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,390
    My Mood
    Mellow
    Thanks
    83
    Thanked 471 Times in 462 Posts

    Default Re: need help with arrays

    Iterate the array (inspect each element) and add each to an integer variable, something like:
    int total;
    for ( int i : arr1 )
    {
        total += i;
    }

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    njabulo ngcobo (August 7th, 2013)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: need help with arrays

    Note: you have declared the array in the main method, therefore the inputNumber method does not have access to it. You need to pass it as a parameter or make it an instance variable and use OO principles.
    Improving the world one idiot at a time!

Similar Threads

  1. Arrays
    By LeeDD in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 9th, 2013, 08:09 PM
  2. Need help with arrays
    By Burnett98 in forum What's Wrong With My Code?
    Replies: 27
    Last Post: January 23rd, 2013, 04:23 PM
  3. Arrays
    By oonagh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 7th, 2012, 09:03 AM
  4. 2D arrays
    By Fluffy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2012, 04:11 PM
  5. Can Anyone Help With Arrays?
    By metaleddie13 in forum Collections and Generics
    Replies: 9
    Last Post: November 15th, 2011, 11:12 PM