Counter and Media C #

1

I am doing a program in which I request the data of N students such as their name, age, weight and height, I want to know the average weight of the students, how many students have the same height and what is that height.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;

        namespace examen
        {
         class Program
        {
        static void Main(string[] args)
        {
        int cant, i=0, j=0,cont =0;
        double suma = 0,promedio =0;


        Console.Write("Ingrese el numero de alumnos que desea ingresar: ");
        cant = int.Parse(Console.ReadLine());
        Console.WriteLine("");
        Console.WriteLine("");
        string[] nombres = new string[cant];
        int[] edad = new int[cant];
        double[] peso = new double[cant];
        float[] estatura = new float[cant];

        for (i = 0; i < cant; i++)
        {

            Console.WriteLine("");
            Console.WriteLine("");
            Console.Write("Ingrese el nombre para el " + (i+1) + " estudiante: ");
            nombres[i] = Console.ReadLine();
            Console.WriteLine("");
            Console.WriteLine("");
            Console.Write("Ingrese la edad perteneciente al estudiante " + nombres[i] + ": ");
            edad[i] = int.Parse(Console.ReadLine());
            Console.WriteLine("");
            Console.WriteLine("");
            Console.Write("Ingrese el peso perteneciente al estudiante " + nombres[i] + ": ");
            peso[i] = double.Parse(Console.ReadLine());
            suma+=peso[i];
            Console.WriteLine("");
            Console.WriteLine("");
            Console.Write("Ingrese la estatura perteneciente al estudiante " +nombres[i] + ": ");
            estatura[i] = float.Parse(Console.ReadLine());
             if (estatura[i] == estatura[i])
            {
                cont++;
            }

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
        }

        Console.WriteLine("El peso promedio de los estudiantes es: " + suma/cant);
        Console.WriteLine("La cantidad de estudiantes que tienen la misma estatura son: " + cont);
            Console.ReadKey();
    }
    }
    }

at the moment I have this code and I present two problems

1-when calculating the average, the decimal point (dot) is not printed correctly (position where it should be)

2-the variable "cont" is responsible for accumulating the times that a student's height is repeated with another student's and although I have created an "if" condition for it, I always print that the number of students that have the same height is equal to the number of students to enter by the user

Regarding the part of print which stature is the one that is repeated among the students I would like to first solve the problems before posed and then print that stature

    
asked by Sasori1264 24.08.2018 в 01:53
source

1 answer

1

Linq has the Distinct () option that can help you find unique values. Try this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace examen
{
    class Program
    {
        static void Main(string[] args)
        {
            int cant, i = 0, j = 0, cont = 0;
            double suma = 0, promedio = 0;

            Console.Write("Ingrese el numero de alumnos que desea ingresar: ");
            cant = int.Parse(Console.ReadLine());
            Console.WriteLine("");
            Console.WriteLine("");
            string[] nombres = new string[cant];
            int[] edad = new int[cant];
            double[] peso = new double[cant];

            List<double> estaturas = new List<double>();
            for (i = 0; i < cant; i++)
            {

                Console.WriteLine("");
                Console.WriteLine("");
                Console.Write("Ingrese el nombre para el " + (i + 1) + " estudiante: ");
                nombres[i] = Console.ReadLine();
                Console.WriteLine("");
                Console.WriteLine("");
                Console.Write("Ingrese la edad perteneciente al estudiante " + nombres[i] + ": ");
                edad[i] = int.Parse(Console.ReadLine());
                Console.WriteLine("");
                Console.WriteLine("");
                Console.Write("Ingrese el peso perteneciente al estudiante " + nombres[i] + ": ");
                peso[i] = double.Parse(Console.ReadLine());
                suma += peso[i];
                Console.WriteLine("");
                Console.WriteLine("");
                Console.Write("Ingrese la estatura perteneciente al estudiante " + nombres[i] + ": ");
                estaturas.Add(float.Parse(Console.ReadLine()));
                Console.WriteLine("");
                Console.WriteLine("");
                Console.WriteLine("");
            }

            Console.WriteLine("El peso promedio de los estudiantes es: " + suma / cant);
            //Console.WriteLine("La cantidad de estudiantes que tienen la misma estatura son: " + cont);

            var listaEstaturas = estaturas.Distinct().ToArray();
            foreach (var elemento in listaEstaturas)
            {                
                Console.WriteLine(estaturas.Count(e => e == elemento)
                     + " estudiantes poseen las misma estatura  " + elemento);
            }
            Console.ReadKey();
        }
    }
}

A brief summary:

The line var listaEstaturas = estaturas.Distinct().ToArray(); created a variable called listStatures with unique values (remove repeated ones).

Then estaturas.Count(e => e == elemento) counts how many times it repeats. And I print those values with:

       foreach (var elemento in listaEstaturas)
        {                
            Console.WriteLine(estaturas.Count(e => e == elemento)
                 + " estudiantes poseen las misma estatura  " + elemento);
        }
    
answered by 24.08.2018 / 05:37
source