0

I'm trying to put together a java program to do the following:

  1. Prompt for and read in a number of integers to read
  2. Create an array that can hold that many integers
  3. Use a loop to read in integer values to fill the array
  4. Calculate the average value in the array (as an integer)

This is what I have so far (although I'm pretty sure this is wrong):

public static void Average (Scanner keyboard)
{
    System.out.println("Please insert number of integers to read in: ");
    keyboard = new Scanner(System.in);
    int f = keyboard.nextInt();
    int value[]= new int[f];
    //I don't know if I should use a while loop here or what the arguments should be
}

What should the conditions be, in order to set up the loop?

0

5 Answers 5

3

Let's look at what you need to calculate an average and what you have right now.

What you need

  • The total number of values
  • The values
  • Somewhere to keep the sum of values

What you have

  • The total number of values
  • A source from which to get new values

Now, from your code, you don't seem to have a place to add all your numbers. That's easy to fix; you know how to declare a new variable.

You also don't have the values, but you do have somewhere you can get them from. Since you also know how many numbers you need to sum up, you can use a loop to get that many numbers from your source.

All in all, you'll want your loop to run f times. In that loop, you'll want to get new a new number and add it to the rest. At the end, you should be able to derive the average from all that.

Sign up to request clarification or add additional context in comments.

Comments

0

The better idea would be to prompt the user to enter all of the values at once, separated by spaces. IE

2 4 1 1 6 4 2 1

You can then call the split() function for Strings to split this into an array of Strings, then use the Integer.parseInt() function to turn this array of Strings into an array of ints.

Once you have your array of ints, it's a simple for loop to add all of the values together and divide by that array's length.

1 Comment

While a reasonable solution, it doesn't meet the requirements of the OPs assignment
0

You can put a while loop or a for loop to input the numbers. Along with the input, keep taking the sum of the numbers. Since you have total number of values: Average= (sum of numbers)/ total numbers.

I will write pseudo code so that it will force you to search more:

//Pseudo code starts after your array declaration

for loop from 0 to f
  store it in values Array
  save sum of numbers: sum= sum+values[i]
loop ends
calculate Average

Comments

0
public static void Average (Scanner keyboard)
{
    System.out.println("Please insert number of integers to read in: ");
    keyboard = new Scanner(System.in);
    int f = keyboard.nextInt();
    int value[]= new int[f];

    double avg = 0;
    for (int i = 0; i < f; i++)
    { 
       value[i] = keyboard.nextInt();
       avg += value[i];   
    }

    avg /= f;
    System.out.println("Average is " + avg);
}

I dont see a point of having array value. Or do you want some other kind of average ?

Comments

0

I wrote(with a friend) a code that calculates the average number:

package dingen;
import java.util.Scanner;


public class Gemiddelde {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);

    float maxCount = 0;
    float avgCount = 0;

    System.out.println("How many numbers do you want");

    int n = sc.nextInt();

    for(int i = 0; i < n; i++) {
        System.out.println("Number: ");
        float number = sc.nextInt();
        maxCount = maxCount + number;

    }

    avgCount = maxCount / n;
    System.out.println("maxCount = " + maxCount);
    System.out.println("avgCount = " + avgCount);
}

}

the only thing you have to do is replace your class and package.

you wil get the message: How many numbers do you want?:

and then it wil ask you the amount of numbers you inserted.

example:

How many numbers do you want?:6

Number:6

Number:7

Number:8

Number:9

Number:93

Number:94

maxCount = 217.0

avgCount = 36.166668

I have hoped I helped you with your problem :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.