I was doing an assignment which was related to arrays. The question is:
"Complete the code segment to help Raj , find the highest mark and average mark secured by him in "s" number of subjects."
and here is the code segment which needs to be completed:
import java.util.Scanner;
public class Exercise1_5{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double mark_avg;
int result;
int i;
int s;
//define size of array
s = input.nextInt();
//The array is defined "arr" and inserted marks into it.
int[] arr = new int[s];
for(i=0;i<arr.length;i++)
{
arr[i]=input.nextInt();
}
//hints given:
//Initialize maximum element as first element of the array.
//Traverse array elements to get the current max.
//Store the highest mark in the variable result.
//Store average mark in avgMarks.
//my Attempt:
/*
for (i = 1; i < arr.length; i++)
if (arr[i] > s){
s = arr[i];}
result = s;
System.out.println(result);
double sum= 0;
for (i = 0; i < arr.length; i++)
sum += arr[i];
double avgMarks=sum/i;
System.out.print(avgMarks);*/
}
}
question from:
https://stackoverflow.com/questions/65948431/traversing-finding-the-max-value-and-finding-average-of-arrays-in-java 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…