Question

In: Computer Science

write a program that finds the average of all positive, non-zero values found in a given...

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:

  • Does the solution compile?
  • Does the solution have your name in the comments?
  • If the array has the values {2.0,-4.0,-6.0,8.0,11.0} does the program print out:

Average: 7.0

  • If the array has the values {-1.0,5.0,-10.0,2.0,-20.0,0.0} does the program print out:

Average: 3.5

  • If the array has the values {-5.0,-4.0,-3.0,-2.0,-1.0} does the program print out:

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
 * 

Solutions

Expert Solution

************************************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**********************************************


Related Solutions

write a program using the main method where it searches through an array of positive, non-zero...
write a program using the main method where it searches through an array of positive, non-zero integer values and prints out the maximum even and odd values. 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: “Max Even: ”<<max even value>> “Max Odd: ”<<max odd value>> Values denoted in “<< >>” represent variable values,...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Write a python program. Grades are values between zero and 10 (both zero and 10 included),...
Write a python program. Grades are values between zero and 10 (both zero and 10 included), and are always rounded to the nearest half point. To translate grades to the American style, 8.5 to 10 become an “A,” 7.5 and 8 become a “B,” 6.5 and 7 become a “C,” 5.5 and 6 become a “D,” and other grades become an “F.” Implement this translation, whereby you ask the user for a grade, and then give the American translation. If...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Using R: write a program that finds the standard deviation between each column. Find the average...
Using R: write a program that finds the standard deviation between each column. Find the average standard deviation for Set A and B, and then use that average to guess the next (fifth) column. Set A: 2, 5, 7, 8 Set B: 2, 3, 6, 9
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Now say you wish to write a program that, given a natural number input n, finds...
Now say you wish to write a program that, given a natural number input n, finds another program (e.g. in Java or C) which prints out n. The discovered program should have the minimum execution-time-plus-length of all the programs that print n. Execution time is measured by the number of CPU instructions executed, while “length” is the number of characters in the source code. Can this be done? (Hint: Is it possible to tell whether a program halts on a...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT