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 ());

}

CodePudding user response:

The
reference 2 floor 姎 jacaranda 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 ());

}

Positive solution,
To understand the functions do, building Lord are simple, a group of the reuse process, can let the program structure looks more clear and organized, and a lot of time to isolates it operating data - even constants, also recommended separately defined it, rather than write directly in the code,
You want to operate an array, the array passed as a parameter, and returns the processing result, rather than write the array into the function to death

CodePudding user response:

 
Private void button1_Click (object sender, EventArgs e)
{
MessageBox. Show (GetAvg (new List {1, 2, 3, 4}) + "");
}

Public double GetAvg (List Value)
{
The return value. The business ();
}

CodePudding user response:

 
Int [] 'arry=new int [] {6};
Int mean='arry. Business ()
  •  Tags:  
  • C#
  • Related