Código
Miren los datos los saco de un xml y necesito sacar la mediana de las notas , osea la nota que esta al medio del arraylist y no he podido sacarlo , quien me da algunas idea porfa , se que el codigo es medio largo y enredado xD , pero recuerden que estoy recien aprendiendo... bueno espero que alguien me heche una mano , el case 4 es la mediana que no he podido sacar , saludos.
using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Collections; namespace ConsoleApplication1 { class Program { public static void PrintValues(IEnumerable myList) { Alumno e; System.Collections.IEnumerator enumerar = myList.GetEnumerator(); while (enumerar.MoveNext()) { e = (Alumno)enumerar.Current; Console.WriteLine("Nombre :" + e.nombre_); Console.WriteLine("Nota :" + e.nota_); //onsole.WriteLine("Moda :" + enumerar.Current); Console.WriteLine(); } } public static void imprimirNotas(IEnumerable myList) { Alumno f; System.Collections.IEnumerator enumerar10 = myList.GetEnumerator(); while (enumerar10.MoveNext()) { f = (Alumno)enumerar10.Current; //Console.WriteLine("Nombre :" + e.nombre_); Console.WriteLine("Nota :" + f.nota_); //onsole.WriteLine("Moda :" + enumerar.Current); Console.WriteLine(); } } static void Main(string[] args) { ArrayList lista = new ArrayList(); // SortedList notas = new SortedList(); XmlDocument doc = new XmlDocument(); Alumno a; String nombre; int opcion = 0; decimal nota=0; doc.Load("..\\..\\alumnos.xml"); // ACA RECORRERE LOS NODOS DE LA LISTA DEL ARCHIVO //XML foreach (XmlNode nodo in doc.ChildNodes) { foreach (XmlNode nodo2 in nodo.ChildNodes) { // GUARDAMOS LOS DATOS EN CADA UNA DE LAS VARIABLES CREADAS nombre = nodo2.SelectSingleNode("nombre").InnerText; nota = decimal.Parse(nodo2.SelectSingleNode("nota").InnerText); Console.WriteLine("Nombre :" + nombre); Console.WriteLine("Nota :" + nota); a = new Alumno(nombre,nota); lista.Add(a); //notas.Add(notas,a); } } do { Console.WriteLine("1.- Mostrar Alumnos de la lista"); Console.WriteLine("2.- Mostrar nota del alumno segun el nombre"); Console.WriteLine("3.- Promedio de notas del curso"); Console.WriteLine("4.- Mediana de las notas"); Console.WriteLine("5.- Tabla de frecuencia de las notas del curso"); Console.WriteLine("6.- Mostrar la mejor nota del curso"); Console.WriteLine("7.- Mostrar las peores notas del curso"); Console.WriteLine("8 .- Salir"); Console.WriteLine("Ingrese la opcion que desea "); opcion = int.Parse(Console.ReadLine()); switch (opcion) { case 1: PrintValues(lista); break; case 2: Console.WriteLine("Ingrese el nombre"); nombre = Console.ReadLine(); Alumno b; System.Collections.IEnumerator enumerar2 = lista.GetEnumerator(); while (enumerar2.MoveNext()) { b= (Alumno)enumerar2.Current; if (b.nombre_.ToLower().Equals(nombre.ToLower())) { Console.WriteLine("Nota :" + b.nota_); break; } } // Console.WriteLine("Nota : " + b.nota_); break; case 3: Alumno EM; Decimal RESULTADO = 0; Decimal promedio=0; System.Collections.IEnumerator ENUMERA = lista.GetEnumerator(); while (ENUMERA.MoveNext()) { EM = (Alumno)ENUMERA.Current; nota = EM.nota_; String name = EM.nombre_; RESULTADO = RESULTADO + nota/ lista.Count; // APROXIMO EL PROMEDIO PARA QUE NO SALGAN TANTOS DECIMALES promedio = Math.Round(RESULTADO); //break; } Console.WriteLine("El promedio es :" + promedio); break; case 4: /* int total = lista.Count; double otro = total / 2; double result = Math.Round(otro, 0);*/ double[] notas; SortedList calificaciones; calificaciones = new SortedList(); notas = new double[50]; double laMediana; laMediana = 0; int i=0; System.Collections.IEnumerator ENUMERAT = lista.GetEnumerator(); while (ENUMERAT.MoveNext()) { Alumno EM2 = (Alumno)ENUMERAT.Current; nota = EM2.nota_; i++; nota = (decimal)notas[(int)(lista.Count / 2)]; // String name = EM.nombre_; // RESULTADO = RESULTADO + nota / lista.Count; //notas.Add(nota); // lista.IndexOf(2); if (lista.Count % 2 == 0) { // N=4; // N/2 = 2 => 2-1 laMediana = ((Double)notas[(int)(lista.Count / 2)] + (Double)notas[(int)((lista.Count / 2) - 1)]) / 2; //Console.Write("La Mediana del Curso es : " + laMediana); } else { // N=5 // N-1=4 => 4/2 => 2 laMediana =(double) notas[(int)((lista.Count - 1) / 2)]; // Console.Write("La Mediana del Curso es : " + laMediana); } //break; } // PrintValues(lista); // Console.WriteLine("La media es : " + result); // Console.Write("La Mediana del Curso es : " + laMediana); break; case 5: // imprimirNotas(notas); int uno, dos, tres, cuatro, cinco, seis, siete; uno = 0; dos = 0; tres = 0; cuatro = 0; cinco = 0; seis = 0; siete = 0; siete = 0; Alumno es = new Alumno(); System.Collections.IEnumerator enumerar30 = lista.GetEnumerator(); while (enumerar30.MoveNext()) { es = (Alumno)enumerar30.Current; if ((es.nota_ >=1 ) & (es.nota_ < 2)) { uno++; } if ((es.nota_ >= 2) & (es.nota_ < 3)) { dos++; } if ((es.nota_ >= 3) & (es.nota_ < 4)) { tres++; } if ((es.nota_ >= 4)& (es.nota_ < 5)) { cuatro++; } if ((es.nota_ >= 5) & (es.nota_ < 6)) { cinco++; } if ((es.nota_ >=6) & (es.nota_ < 7)) { seis++; } if (es.nota_ == 7) { //siete = 0; siete++; //Console.WriteLine("Nota 7 :" + siete); } } Console.WriteLine("**** TABLA DE FRECUENCIAS *****"); Console.WriteLine("Nota sobre 1 :" + uno); Console.WriteLine("Nota sobre 2 : " + dos); Console.WriteLine("Nota sobre 3 :" + tres); Console.WriteLine("Nota sobre 4 :" + cuatro); Console.WriteLine("Nota sobre 5:" + cinco); Console.WriteLine("Nota sobre 6 :" + seis); Console.WriteLine("Nota sobre 7 :" + siete); break; case 6: String names = null; decimal notas2 = 0; Alumno dedicado; System.Collections.IEnumerator enumerar3 = lista.GetEnumerator(); while (enumerar3.MoveNext()) { dedicado = (Alumno)enumerar3.Current; if (notas2 < dedicado.nota_) { notas2 = dedicado.nota_; names = dedicado.nombre_; Console.WriteLine("La mejor nota es : " + names); //break; } } //Console.WriteLine("La mejor nota es : " + names); break; case 7: String name2 = null; decimal NOTA = 7; Alumno emple2; System.Collections.IEnumerator enumerar4 = lista.GetEnumerator(); while (enumerar4.MoveNext()) { emple2 = (Alumno)enumerar4.Current; if (emple2.nota_ < NOTA) { NOTA = emple2.nota_; name2 = emple2.nombre_; Console.WriteLine("La persona con peor nota es : " + name2); } } break; } } while (opcion!=8); } } }
Gracias de ante mano.
BadDevil