Calculating an average with an array in Java; Homework -
i have homework assignment, , these 5 questions,
- what total of elements in array.
- what average of elements in array.
- what largest , smallest numbers in array. (one loop both questions)
- display odd numbers in array. use modulus function (%) on page 46.
- print every other number in array.
i having trouble finding average, or writing code finds it.
public class javaapplication { /** * @param args command line arguments */ public static void main(string[] args) { // todo code application logic here int nums[] = {33,66,77,88,60,91,87,92,76,90}; int sum = 0; (int = 0; < 10; i++){ sum +=nums[i]; } system.out.println("the sum " + sum); int average = 0; here (int = 0; < nums.length; i++) { | sum = sum + nums[i]; | } system.out.println("average value " + average); , here. int min, max; min = max = nums [0]; for(int i=1; < 10; i++) { if(nums[i] < min) min = nums[i]; if(nums[i] > max) max = nums[i]; } system.out.println("min , max: " + min + " " + max); } } this second week in class, please explain needs added or not , :)
the pseudo code average n elements :
1) add of element values 2) divide result step 1 number of elements in collection
Comments
Post a Comment