ArraysExample

ArraysExample - if(midtermScores[i == SENTINEL break...

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

View Full Document Right Arrow Icon
/* * ArraysExample.java * ----------------- * This program shows an example of using arrays. */ import acm.program.*; public class ArraysExample extends ConsoleProgram { private static final int SENTINEL = -1; public void run() { setFont("Courier New-24"); int maxLength = readInt("Max size of array: "); int[] midtermScores = new int[maxLength]; int numActualScores = 0; for (int i = 0; i < maxLength; i++) { midtermScores[i] = readInt("Next score: ");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (midtermScores[i] == SENTINEL) break; numActualScores++; } println("Average score: " + computeAverage(midtermScores, numActualScores)); } private double computeAverage(int arr, int numActualScores) { double average = 0; for(int i = 0; i < numActualScores; i++) { average += arr[i]; } average = average / numActualScores; return average; } }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Ask a homework question - tutors are online