For my assignment, I have to

"Write a program that reads integers, one per line, and displays their sum. Also, display all the numbers read, each with an annotation giving its percentage contribution to the sum. Use a method that takes the entire array as one argument and returns the sum of the numbers in the array.


I have the array down, as well as the sum, but i cant begin to guess where to start with the part where you have to give the percentage contribution to the sum, and also not so sure what he means by wanting us to put it in a method, like not sure how one passes an array in a method i guess?

any hints? not looking for people to code for me, looking for a point in the right direction...well maybe a strong point xD i hate programming


heres my code

import java.util.Scanner;
public class array 
{
	public void compute()
	{
		//nonsense to be entered here
	}

	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		int size, sum = 0;

		System.out.println("How many numbers are you going to enter?");
		size = keyboard.nextInt();
		
		int[] anArray = new int[size];
		
		System.out.println("Enter " + size + " numbers, one per line");
		for (int i = size-1; i >= 0; i--)
		{
			anArray[i] = keyboard.nextInt();
			sum = sum + anArray[i];
		}
		
		for (int i = size-1; i >= 0; i--)
		{
			System.out.println("Numbers entered were:");
			System.out.println(anArray[i]);
		}
		System.out.println("Sum of the numbers is: " + sum);
	}
}

thanks

In order to run a method that takes an array of integers and returns their sum, your method will need the following signature:

public static int sumArray(int[] anArray)

Then, instead of having the line sum = sum + anArray; in your main method, after your loop you can call your method like so:

sum = sumArray(anArray);

I'll leave it to you to figure out exactly what needs to go in the sumArray method, but it is fairly simple so don't over-complicate it.

i understand that part...but how exactly would i figure out the annotation part...like if the user entered
2
1
1
2


how would i get it to say this part:

2, which is 33.3333% of the sum.
1, which is 16.6666% of the sum.
1, which is 16.6666% of the sum.
2, which is 33.3333% of the sum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.