Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my coding class we learning to print out the average of a set of numbers from an array list. I know how to print the average of a single array but I am having trouble with printing out the average of a double array. The context of my problem is to ask the user for height and width of the array, then ask for a minimum and maximum number. Where it will randomly find numbers and print them out however many times the height and width was given. Now I have to calculate and display the rainfall for the entire array. The whole code works except when I try to do this. Can anyone help me. I have a problem showing I know how to print out the average of a specific set of numbers, but I can't seem to figure out how to do it in my homework. Should I be using the .size() method in my for loops to figure out the average? Thanks

What I have tried:

From the problem that works:
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListAverage {
   public static void main (String [] args) {
      final int NUM_ELEMENTS = 8;
      Scanner scnr = new Scanner(System.in);
      ArrayList<Double> userNums = new ArrayList<Double>(); // User numbers
      Double sumVal = 0.0;
      Double averageVal = 0.0; // Computed average
      int i = 0; // Loop index

      System.out.println("Enter " + NUM_ELEMENTS + " numbers...");
      for (i = 0; i < NUM_ELEMENTS; ++i) {
         System.out.print("Number " + (i + 1) + ": ");
         userNums.add(new Double(scnr.nextDouble()));
      }

      // Determine average value
      sumVal = 0.0;
      for (i = 0; i < NUM_ELEMENTS; ++i) {
         sumVal = sumVal + userNums.get(i); // Calculate sum of all numbers
      }

      averageVal = sumVal / NUM_ELEMENTS; // Calculate average 
      
      System.out.println("Average: " + averageVal);

      return;
   }
}


The code that doesn't work:


import java.util.Scanner;
import java.util.Random;
public class App {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int userHeight;
        int userWidth;
        int userMin;
        int userMax;
        
        
        System.out.println("Please enter in a height and width of array.");
        System.out.print("Height: ");
        userHeight = scan.nextInt();
        System.out.print("Width: ");
        userWidth = scan.nextInt();
        
        System.out.println("Please enter in the minimum and maximum rainfall value.");
        System.out.print("Minimum rainfall: ");
        userMin = scan.nextInt();
        System.out.print("Maximum rainfall: ");
        userMax = scan.nextInt();
        
        int [][] rainMap = new int [userHeight][userWidth];
        
        for (int i = 0; i < userHeight; i++)
        {
            for (int j = 0; j < userWidth; j++)
            {
                Random rand = new Random();
                int randomNum = rand.nextInt((userMax - userMin) + 1) + userMin;
                rainMap[i][j] = randomNum;
            }
        }
        
        for (int i = 0; i < userHeight; i++)
        {
            for (int j = 0; j < userWidth; j++)
            {
                System.out.print(rainMap[i][j]);
            }
            System.out.println("");
        }
       
        //finding average
        double total = 0;
        double sumValI = 0;
        //double sumValJ = 0;
        double average = 0.0;
       for (int i = 0; i < userHeight; i++)
       {
           for (int j = 0; j < userWidth; j++)
           {
               sumValI = sumValI + rainMap.get((i)(j));
           }
       }
       total = userHeight * userWidth; 
        average = sumValI / total;
        System.out.println("Average rainfall: " + average);
    }
    
}
Posted
Updated 19-Oct-17 22:37pm
v2

Quote:
I have a problem showing I know how to print out the average of a specific set of numbers, but I can't seem to figure out how to do it in my homework.


There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
You cannot access the array values in the way you have coded - the compiler error message should have given you a clue.
Java
sumValI = sumValI + rainMap.get((i)(j));

See Array (Java Platform SE 7 )[^].


You just need to do it the same way as in the loop that prints the values:
Java
sumValI = sumValI + rainMap[i][j];

Incidentally, why not do this summing as you print each item, rather than in a separate loop?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900