Home > Net > Define a method GetAvg (), calculating the average of an integer array, incoming parameters for an a
Define a method GetAvg (), calculating the average of an integer array, incoming parameters for an a
Time:09-21
Using System;
The namespace wefe { The class MainClass { Public static void GetAvg () { Int [] intArr={1, 2, 3, 4, 5, 6, 77, 88}; int sum=0; Int avg=0; for (int i=1; I & lt;=intArr. Length; I++) { The sum +=intArr [I]; } Avg=sum/intArr. Length; }
Public static void Main (string [] args) { Int ruselt=GetAvg ();//what should I write here } } }
CodePudding user response:
GetAvg () into a
Public int void GetAvg () { Int [] intArr={1, 2, 3, 4, 5, 6, 77, 88}; int sum=0; Int avg=0; for (int i=1; I & lt;=intArr. Length; I++) { The sum +=intArr [I]; } Return (int) (sum/intArr. Length); }
CodePudding user response:
public static float GetAvg (int [] intArr) {
Float sum=0; Float avg=0; for (int i=0; I & lt; IntArr. Length; I++) { The sum +=intArr [I]; } Avg=sum/intArr. Length; return avg; } Public static void Main () { Int [] intArr={1, 2, 3, 4, 5, 6, 77, 88}; The Console. Write (GetAvg (intArr). The ToString ());