Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - solve simple array earthquake program
    text
    copied!<p>I have a home work problem like this:</p> <p>The following data represent Richter Scale data for earthquakes. Write a program to calculate and print the average of any valid earthquake data.</p> <p>Store the Richter values in an array of doubles named quakeLevels.</p> <p>Unfortunately, your seismograph is known to sometimes produce unreliable readings (like the value of 10.1 in this example). So you decide to throw out the maximum and minimum readings.</p> <p>Your program should do the following:</p> <p>Declare and initialize the quakeLevels array using the following data. <strong>{ 5.6, 6.2, 4.0, 5.5, 5.7, 6.1,7.4, 8.5, 5.5, 6.3, 6.4, 2.1, 6.9, 4.3, 3.1, 7.0, 10.1 }</strong></p> <p>Determine the maximum and minimum values in the array. Compute the average of the array contents, excluding the maximum and minimum values. Print the values in the array excluding the maximum and minimum values. Print the average.</p> <p>I <em>cannot</em> use the Math class, so that is why everything is written out to print max and min.</p> <p>Here is my code so far: </p> <pre><code> public class ARRAYminAndmax0RichterScale { public static void main(String [] args) { double [] quakeLevels = { 5.6, 6.2, 4.0, 5.5, 5.7, 6.1 ,7.4, 8.5, 5.5, 6.3, 6.4, 2.1, 6.9, 4.3, 3.1, 7.0, 10.1}; double [] quakeLevelsNormalized = new double [(quakeLevels.length - 2)]; int i; int minIndex = 0; // start with 0th element as min for ( i = 1; i &lt; quakeLevels.length; i++) { if (quakeLevels[i] &lt; quakeLevels[minIndex]) { minIndex = i; } } System.out.print("Min: " + quakeLevels[minIndex] + " "); int maxIndex = 0; // start with 0th element as max for ( i = 1; i &lt; quakeLevels.length; i++) { if (quakeLevels[i] &gt; quakeLevels[maxIndex]) { maxIndex = i; } } System.out.println("Max: " + quakeLevels[maxIndex]); System.out.println("The Richter values, excluding the extrema, are as follows: "); //make a new array excluding the max and min for ( i = 1; i &lt; quakeLevels.length - 2; i++ ) { if(quakeLevels[i]!= minIndex &amp;&amp; quakeLevels[i]!= maxIndex){ quakeLevelsNormalized[i] = quakeLevels[i]; System.out.printf("%6s\n", quakeLevelsNormalized[i] ); } } //***THIS LOOP IS HERE TO HELP ME FIGURE OUT THE PROBLEM*** for( i =0; i &lt; quakeLevelsNormalized.length; i++){ System.out.println("quakeLevelsNormalized["+i+"] = " + quakeLevelsNormalized[i]); } //find average of quakeLevelsNormalized double arrayTotal = 0; double average = 0; for (i = 0; i &lt; quakeLevelsNormalized.length; i++) { arrayTotal = arrayTotal + quakeLevelsNormalized[ i ]; } average = arrayTotal / quakeLevelsNormalized.length; //output System.out.println( quakeLevelsNormalized[i-1]); System.out.printf("%s%.1f\n","Average Quake Level = ", average); } } </code></pre> <h2>And I get the following output:</h2> <pre><code>Min: 2.1 Max: 10.1 The Richter values, excluding the extrema, are as follows: 6.2 4.0 5.5 5.7 6.1 7.4 8.5 5.5 6.3 6.4 2.1 6.9 4.3 3.1 quakeLevelsNormalized[0] = 0.0 quakeLevelsNormalized[1] = 6.2 quakeLevelsNormalized[2] = 4.0 quakeLevelsNormalized[3] = 5.5 quakeLevelsNormalized[4] = 5.7 quakeLevelsNormalized[5] = 6.1 quakeLevelsNormalized[6] = 7.4 quakeLevelsNormalized[7] = 8.5 quakeLevelsNormalized[8] = 5.5 quakeLevelsNormalized[9] = 6.3 quakeLevelsNormalized[10] = 6.4 quakeLevelsNormalized[11] = 2.1 quakeLevelsNormalized[12] = 6.9 quakeLevelsNormalized[13] = 4.3 quakeLevelsNormalized[14] = 3.1 3.1 Average Quake Level = 5.2 </code></pre> <h1>Problem</h1> <p>So this is obviously not what it is supposed to look like. Why does it give me the extra 3.1 on the end? And it only has 14 elements, when it is supposed to have [18 minus the two extremes]? I'm a beginner programmer- and I <em>greatly</em> appreciate any and all help!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload