Chapter6_Arrays

# Capacity of an array sum 00 for int i 0 i

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

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 &lt; rainfall.length; i++) { rainfall[i] = Double.parseDouble( JOptionPane.showinputDialog(null, &quot;Rainfall for month &quot; + (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] = &quot;January&quot;; monthName[1] = &quot;February&quot;; … 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 &lt; rainfall.length; i++) { rainfall[i] = Double.parseDouble( JOptionPane.showinputDialog(null, &quot;Rainfall for &quot; + 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 &lt; 4; i++) { sum = 0; for (int j = 0; j &lt; 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 = { &quot;January&quot;, &quot;February&quot;, &quot;March&q...
View Full Document

## This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online