In: Computer Science
write a program that finds the average of all positive, non-zero values found in a given array of doubles and prints that to the console. Any negative or zero values do not contribute to the average. The program should use the array provided in the code, and you may assume that it has already been populated with values. The results should be printed out to the console in the following format:
“Average: ”<<average value>>
Values denoted in “<< >>” represent variable values, and strings in quotations denote literal values (make sure to follow spelling, capitalization, punctuation, and spacing exactly). Also, if there are either no non-zero or positive in the array then print out the average value as “0.0”.
Solution Tests:
Average: 7.0
Average: 3.5
Average: 0.0
PROVIDED CODE:
* Provided code. Do not alter the code that says "Do not alter" * Make sure this at least compiles (no syntax errors) * You may write additional methods to help */ //Do not alter----------------------------------------------------------------------- import java.util.Scanner; public class Question04 { public static double[] array;//The array to be used in the problem public static void main(String[] args) { int number;//Used for the stairs if(args == null || args.length == 0) { //----------------------------------------------------------------------------------- double[] tempArray ={2.0,-4.0,-6.0,8.0,11.0};//You may change these values to test your solution //Do not alter----------------------------------------------------------------------- array = tempArray; } else { } //----------------------------------------------------------------------------------- //Write your solution here }//Do not alter this //Space for other methods if necessary----------------------------------------------- //Write those here if necessary //----------------------------------------------------------------------------------- }//Do not alter this /*Solution Description *
************************************Summary*******************************************
The program for above question is given below with comments and output..
And the program does compile...
However just wanted to let you know that in java we cannot print the variable value in <<average value>> format...
>> operator is used to print variavle value in c++ language not java
but we can print it in
System.out.println("Average : "+avg); //String in quotation denotes the literal values and variables values are printed without qutotation
or
System.out.printf("Average : %.2f ",avg); //%.2f denotes that a float value is to be printed upto 2 decimal points
I hope it works for you!!!!!!!!!!!!!!!!!
******************************************Program******************************************
//Do not alter-----------------------------------------------------------------------
import java.util.Scanner;
public class Question04{
public static double[] array;//The array to be used in the problem
public static void main(String[] args) {
if(args == null || args.length == 0)
{
//-----------------------------------------------------------------------------------
double[] tempArray ={-1.0,5.0,-10.0,2.0,-20.0,0.0};//You may change these values to test your solution
//Do not alter-----------------------------------------------------------------------
array = tempArray;
}
else
{
}
//-----------------------------------------------------------------------------------
//Write your solution here
int number;//Used for the stairs
int count=0; //to count number of non zero positive values in array
double avg=0; //to store average
for(number=0;number<array.length;number++) //iterating through the elements of array
{
if(!(array[number]<=0)) //if array elemt is not zero and not negative
{
avg=avg+array[number]; //add the elements in the avg variable
count++; //increment the count(number of non zero psitive values)
}
}
//untill here only the sum of numbers is stored in avg so....to find the average ..divide the sum by number of elements
if(count!=0) //if theres was at least 1 non zero or positive number in array..then calculate the average
{avg=avg/count;}
System.out.println("Average : "+avg); //printing the average on console
}//Do not alter this
//Space for other methods if necessary-----------------------------------------------
//Write those here if necessary
//-----------------------------------------------------------------------------------
}//Do not alter this
*********************************************Output with code**********************************************