midtermkey - CSI 201 KEY KEY KEY Midterm KEY KEY KEY 1 a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSI 201 KEY KEY KEY Midterm KEY KEY KEY October 17, 2008 1 v 2.00 1. a) Write a while loop that will print out the values from 0 to 100, inclusive, and then b) rewrite it as a for loop that will do the same thing. (15 points) int index = 0; while (index <= 100) { System.out.println(index); index = index + 1; } for (int index = 0; index <= 100; index = index + 1) { System.out.println(index); } 2. Assume you have the following code: int[] numberArray = {2, 2, 6, 42, 1, 14, 0, 42, 23}; int sum = 0; double average = 0.0; // The code for your answer should go here System.out.println("The average is " + average); a) Write a loop (either while or for) to insert into this code that will add up the values in the array and the result in the variable sum. b) Divide the sum by the number of elements in the array to create the average. Remember the average should be a floating point quantity. (20 points) int index = 0; while (index < numberArray.length) { sum = sum + numberArray[index]; index = index + 1; } average = sum/numberArray.length;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

Page1 / 3

midtermkey - CSI 201 KEY KEY KEY Midterm KEY KEY KEY 1 a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online