Capacity of an array sum 00 for int i 0 i

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: length returns the capacity of an array. capacity of an array. sum = 0.0; for (int i = 0; i < rainfall.length; i++) { rainfall[i] = Double.parseDouble( JOptionPane.showinputDialog(null, "Rainfall for month " + (i+1) ) ); sum += rainfall[i]; } annualAverage = sum / rainfall.length; Page 6 11/2/2008 Array Processing – Sample 2 double rainfall = new double[12]; String monthName = new String[12]; monthName[0] = "January"; monthName[1] = "February"; … double The same pattern The same pattern for the remaining for the remaining ten months. ten months. annualAverage, sum = 0.0; for (int i = 0; i < rainfall.length; i++) { rainfall[i] = Double.parseDouble( JOptionPane.showinputDialog(null, "Rainfall for " + monthName[i] )); sum += rainfall[i]; } annualAverage = sum / rainfall.length; Page 7 The actual month The actual month name instead of aa name instead of number. number. 11/2/2008 Array Processing – Sample 3 • Compute the average rainfall for each quarter. //assume rainfall is declared and initialized properly double quarterAverage = new double[4]; for (int i = 0; i < 4; i++) { sum = 0; for (int j = 0; j < 3; j++) { //compute the sum of sum += rainfall[3*i + j]; //one quarter } quarterAverage[i] = sum / 3.0; //Quarter (i+1) average } Page 8 11/2/2008 Array Initialization • Like other data types, it is possible to declare and initialize an array at the same time. number = { 2, 4, 6, 8 }; int double samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2, 9.00, 3.123, 22.084, 18.08 }; String monthName = { "January", "February", "March&q...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online