Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: javierx0246 en 15 de Noviembre 2022, 15:57

Título: Duda sobre un ejercicio C#
Publicado por: javierx0246 en 15 de Noviembre 2022, 15:57
Hola queria comentar que estoy haciendo un ejercicio sobre la frecuencia que lo tengo casi pero no me sale



Mi codigo









using System.Text;
internal class Program
{
public static int[] Arra()
{
int notasRecogidas = 0;
int notasnumero = 0;


int[] notas = new int[5];


for (int i = 0; i < notas.Length; i++)
{

do
{
Console.Write("Ingrese nota (entre 0-10): ");
notasRecogidas = int.Parse(Console.ReadLine());
notas = notasRecogidas;
if (notas < 0 || notas > 10)
{
notasnumero++;

Console.WriteLine($"Nota fuera de rango {notasnumero}");
}

Console.WriteLine($" {notas} ");


} while (notas < 0 || notas > 10);










}
return notas;
}

static void FrecuenciaNotas(int[] Array)
{
int[] FrecuenciaNota = new int[11];
int i, j;
for (i = 0; i < 11; i++)
{
for (j = 0; j < Array.Length; j++)
{
if (i ==FrecuenciaNota[j])
{
FrecuenciaNota++;
}

}
Console.WriteLine($"Nota {i}, {FrecuenciaNota[j]} veces.");
}
}


private static void Main(string[] args)
{
int[] notas = Arra();
FrecuenciaNotas(notas);


Console.ReadLine();
}

}

Gracias
Título: Re: Duda sobre un ejercicio
Publicado por: Kabuto en 16 de Noviembre 2022, 11:31
Hola.
No lo he ejecutado, pero así a ojo, creo que hay un problema en este método.
Por argumentos recibes un array que es quien contiene las notas (lo marco en azul).
Pero luego, no lo usas para comparar las notas, si no que usas el array FrecuenciaNota (marco en rojo), quien en realidad lo que tiene que hacer es contar la frecuencia de cada nota

Citar
static void FrecuenciaNotas(int[] Array)
{
int[] FrecuenciaNota = new int[11];
int i, j;
for (i = 0; i < 11; i++)
{
for (j = 0; j < Array.Length; j++)
{
if (i ==FrecuenciaNota[j])
{
FrecuenciaNota[ i ]++;
}

}
Console.WriteLine($"Nota {i}, {FrecuenciaNota[j]} veces.");
}
}

Entiendo que en realidad deberías estar usando el array Array.

Citar
static void FrecuenciaNotas(int[] Array)
{
int[] FrecuenciaNota = new int[11];
int i, j;
for (i = 0; i < 11; i++)
{
for (j = 0; j < Array.Length; j++)
{
if (i ==Array[j])
{
FrecuenciaNota[ i ]++;
}

}
Console.WriteLine($"Nota {i}, {FrecuenciaNota[j]} veces.");
}
}


Comprueba si así te funciona.
Es que yo ahora estoy escribiendo desde un smartphone y no puedo probar  :P