Beginner: C# recursive function
Help for a recursive function that prints the average of the elements in a list of integers and the number of elements larger than the average. in c #
Well, what I have up to the moment, is giving System Error.StackOverflowException.
public static Double MediaLista(Double[] v)
{
int i = 0;
Double soma = 0;
if (i < v.Length)
{
for (int j = 0; j < v.Length; j++)
{
soma += v[j];
}
i++;
return soma / MediaLista(v);
}
else return 0;
}
static void Main(string[] args)
{
Double[] v = {1,2,3,3,5};
Double resul = MediaLista(v);
}
Edit : I have increased the functionality of picking up the larger-than-average numbers in the Medialist method itself. In all my method stayed like this.
public static double MediaLista(int[] v, int ultimo)
{
if (ultimo == 0)
{
return v[0];
}
else
{
int qtdNumero = ultimo + 1;
double soma = v[ultimo] + (qtdNumero - 1) * MediaLista(v, ultimo - 1);
if (qtdNumero == v.Length) {
for (int i = 0; i < v.Length; i++)
{
if (v[i] > (soma / qtdNumero))
{
Console.WriteLine($"o numero {v[i]} é maior que a média");
}
}
}
return soma / qtdNumero;
}
}
2 answers
StackOverflowException
occurs when there is a overflow of the Stack memory, usually caused by excess nested functions.
In the case on the line return soma / MediaLista(v);
you did not change the Vector v
at all, causing it to call the method again, and again, and again.....until it causes this exception. :)
I believe that what you are wanting to do is return soma / v.Length;
EDIT
There is another error, this time of logic.
In line soma += v[i];
you are looping with the j
but adding the subscript i
, which is always 0.
EDIT 2.0-Recursion Edition: p
Apparently ok. :)
public static void Main()
{
int[] v = { 1, 2, 9, 10, 25, 0};
double media = Media(v, v.Length - 1);
Console.Write(media);
}
public static double Media(int[] v, int ultimo)
{
if (ultimo == 0)
return v[0];
int qtdNumero = ultimo+1;
double soma = v[ultimo] + (qtdNumero-1) * Media(v, ultimo-1);
return soma/qtdNumero;
}
So I managed to do recursion by taking the for and using if and instead of incrementing I was decrementing for recursion to get to 0 and exit the loop and give me the result:
EDIT: I added the part that picks up which elements are larger than the average
private void MetroButton1_Click(object sender, EventArgs e)
{
double[] v = { 1, 2, 3, 3, 5 };
double resul = MediaLista(v, v.Length, 0);
//Elementos da array maiores do que a média
string maiorMedia = string.Empty;
for (int i = v.GetLowerBound(0); i <= v.GetUpperBound(0); i++)
{
if (v[i] > resul)
{
maiorMedia += v[i] + ",";
}
}
metroLabel3.Text = Convert.ToString(resul);
metroLabel4.Text = maiorMedia;
}
//média dos elementos de uma lista de inteiros
public double MediaLista(double[] v, int tamanho, double media)
{
if (tamanho > 0)
{
media = v[tamanho - 1] + MediaLista(v, tamanho - 1, media);
if (tamanho == v.Length)
{
return media / v.Length;
}
return media;
}
else
return 0;
}
EDIT2:
Just to play a little more the part that takes which elements of the array are larger than average, I made another option in LINQ for those who want to understand this feature that is very used in Entity Framework and other Orm
string maiorQueMedia = string.Empty;
var query =
from num in v
where num > resul
select num;
foreach (double num in query)
maiorQueMedia += num + ",";