Computer science array programing question You are interested in computing the a
ID: 3696349 • Letter: C
Question
Computer science array programing question
You are interested in computing the average acid level of coffee served by local coffee shops. You visit many coffee shops and dip your pH meter into samples of coffee. Unfortunately, your pH meter sometimes produces false readings. So you decide to disregard the single reading that is most distant from the average. Write a program that reads the data into an array. Use a negative value to signal the end of data. Compute the average of all the data by summing all the values and dividing by the number of values. If there are two or more values, scan through the array to find the value that is farthest (in either direction) from that average and compute an average that does not include this value by subtracting this value from the previously computed sum. (If the most distant value occurs in the data more than once, that is OK. Just subtract it from the sum once.) If all the values are the same the average will not change, but do the above step anyway. Print the new average. If there is only one value, skip the above step (the average will be just the single value entered.) If there are no values (if the first value entered was negative) exit with an error message and without printing an average. Allow up to 100 pH values. The array will be an array of doubles; use double precision computation. Here is a run of the program: What to submit: Submit a source file for the program. Include documentation at the top of the program that lists its author and date and a brief summary of what the program does.Explanation / Answer
package org.students;
import java.util.Scanner;
public class PhValuesAvg {
public static void main(String[] args) {
// Declaring the double array and initialize the size to 100
double arr[] = new double[100];
// declaring the variables
int i = 0;
double sum = 0, avg = 0, count = 0, num1, newavg = 0;
// Scanner object is used to get the inputs from the user.
Scanner sc = new Scanner(System.in);
// this loop will executes until user enters -1.when the user enters -1
// for the first time the program terminates.
// if the user enters -1 after the first time then calculates the sum of
// the numbers till entered and calculates its average.
while (true) {
System.out.print("Sample " + (i + 1) + " :");
num1 = sc.nextDouble();
// If the user enter -1 this block will execute.
if (num1 == -1) {
if (sum != 0) {
// Calculate the average
avg = sum / count;
// Displaying the average
System.out.println("Average:" + avg);
double farth = arr[0];
// Finding the farthest value in the array.
for (int j = 0; j < count; j++) {
if (farth > arr[j])
farth = arr[j];
}
sum = sum - farth;
// calculating new average
newavg = sum / (count - 1);
// displaying the farthest value and new average
System.out.println("Most Distant Value:" + farth);
System.out.println("New Average:" + newavg);
}
System.out.println(":: Program Exit ::");
System.exit(1);
} else {
arr[i] = num1;
sum = sum + arr[i];
i++;
count++;
}
}
}
}
_______________________________________________________________________________________
output:
Sample 1 :5.6
Sample 2 :6.2
Sample 3 :6.0
Sample 4 :5.5
Sample 5 :5.7
Sample 6 :6.1
Sample 7 :7.4
Sample 8 :5.5
Sample 9 :5.5
Sample 10 :6.3
Sample 11 :6.4
Sample 12 :4.0
Sample 13 :6.9
Sample 14 :-1
Average:5.930769230769231
Most Distant Value:4.0
New Average:6.091666666666668
:: Program Exit ::
_______________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.