Read a list of non-negative integers. The list can be of any length from 1 to 25
ID: 3633504 • Letter: R
Question
Read a list of non-negative integers. The list can be of any length from 1 to 25. The user will signify end of the list with the sentinel value -1. Your program will print:1) The number of data values (don’t count the sentinel value)
2) The average of the data values
3) The highest of the data values
4) All of the data values listed in reverse order from the way they were
given by the user.
Use the following int variables named maxVal & total
Use the following resources:
import java.util.Scanner;
Students will provide one file, Arrays7.java.
Explanation / Answer
import java.util.Scanner; public class Arrays7 { public static void main(String[] args) { int i,n=0;int[] array = new int[5]; int maxVal,total=0; do { System.out.println("enter a number(enter -1 to terminate)"); Scanner sc = new Scanner(System.in); i = sc.nextInt();array[n]=i;n++; }while(i!= -1); System.out.println("The number of data value=" +(n-1)); for(i=0;array[i]!=-1;i++) total=total+array[i]; System.out.println("The average of data value=" +total/(n-1)); maxVal=array[0]; for(i=1;array[i]!=-1;i++) { if(array[i]>maxVal)maxVal=array[i]; } System.out.println("The highest of data value=" +maxVal); System.out.println("The data values in reverse order is:"); for(i=n-2;i>=0;i--) System.out.print(" "+array[i] ); }}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.