Because when I want to delete the value of a variable in c #, does it mark me an error?

0

The problem is that every time I enter an if created I must delete the floating variable where I store information, only that information is every 3 months and I get information every 12 months, which requires is that when I enter the if it means that it is a quarter, for which I must do the quarterly calculation and before leaving delete the variable so that when I re-enter it does not contain information from previous months.

All right up there, because if you do that procedure for about 6 or 7 iterations, but then it goes bankrupt and tells me error.

    private string[] naciones;
    private float[] media;
    private float[,] mediaT;
    private float[,] mensual;
    private string linea;
    private int cantidad;
    private float mediaM=0.0f;
    private float mediaTr = 0.0f;
    private int z = 0;

public void cargar()
{
    Console.WriteLine("Ingrese la cantidad de paises");
    linea = Console.ReadLine();
    cantidad = int.Parse(linea);
    naciones=new string[cantidad];
    mensual=new float[cantidad,12];
    media=new float[cantidad];
    mediaT = new float[cantidad,4];

    for (int x = 0; x < naciones.Length;x++ )
    {
        Console.WriteLine("Ingrese el nombre del pais");
        naciones[x] = Console.ReadLine();
        for (int y = 0; y < mensual.GetLength(1);y++ )
        {
            Console.WriteLine("Ingrese La temperatura");
            linea = Console.ReadLine();
            mensual[x, y] = float.Parse(linea);
            mediaM=mediaM+float.Parse(linea);
            mediaTr = mediaTr + float.Parse(linea);

            if (y==2 || y==5 || y==8 || y==11) {
                Console.WriteLine("ENTRO");
                z = z + 1;
                    mediaTr = mediaTr / 3;
                    mediaT[x, z] = mediaTr;
                    mediaTr = 0.0f; ----> ERROR


            }

        }
        media[x] = mediaM / 12;
    }
}
    
asked by David 23.09.2016 в 21:23
source

1 answer

2

The error is in this portion of code when loading the 4th quarter:

z = z + 1

Note that the arrays ( arrays ) in C # are Base Zero, for example your array mediaT you initialize it as mediaT = new float[cantidad,4] but you start filling your array mediaT from the position 1 and when you reach the 4th quarter z is worth 5.

The solution would be to increase the variable after the assignment, that is, to do it this way:

 mediaTr = mediaTr / 3;
 mediaT[x, z] = mediaTr;
 mediaTr = 0.0f;
 z = z + 1;

I hope I have been clear, anything do not hesitate to comment!

    
answered by 23.09.2016 / 22:08
source