Error in my fix in C #

1

I have a program to generate pseudo-random numbers by means of the mixed congruencial method, but it throws me an error in one of the code lines.

  static void Main(string[] args)
    {
        double c, a, m, sem, res = 0, lim = 0, conta = 0, i = 0;
        int j = 0, cont1 = 0, cont2 = 0;
        Console.WriteLine("Ingrese constante aditiva");
        c = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Ingrese Constante Multiplicativa");
        a = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Ingrese modulo");
        m = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Ingrese semilla");
        sem = Convert.ToDouble(Console.ReadLine());
        double[] vector = new double[100];
        double[] v = new double[4];
        double[] ve = new double[5];

        int[] resultado = new int[100];
        //suma = num1 + num2;
        while (res != 1)
        {
            lim = sem;
            res = ((sem * a) + c) % m;
            sem = res;

            //conta++;
            //Console.WriteLine("" + res / m);
           vector[j] = res / m;
          j++;

        }

        //Console.WriteLine("Periodo " + conta);
        for (int k = 0; k < vector.Length; k++)
        {
            Console.WriteLine("" + vector[k]);
        }
        for (int l = 0; l < vector.Length; l++)
        {
            for (int w = 1; w < vector.Length - 1; w++)
            {
                if (vector[l] < vector[w])
                {
                    resultado[l] = 0;


                }
                else if (vector[l] > vector[w])
                {
                    resultado[l] = 1;

                }
            }

        }
        for (int k = 0; k < resultado.Length; k++)
        {
            //if(resultado[k]=resultado[k+1])
            if (resultado[k] == 1)
                if (resultado[k] == 1)
                    if (resultado[k] == 1)
                        if (resultado[k] == 1)
                            if (resultado[k] == 1)
                            {

                                cont1 = 5;
                            }
                            else cont1 = 4;
                        else cont1 = 3;
                    else cont1 = 2;
                else cont1 = 1;
            Console.WriteLine("Vector Resultado" + "[" + resultado[k] + "]" + cont1);

         }
        Console.WriteLine("Press any key to continue . . .");
        Console.ReadLine();
    }

    }
}

The error gives me in the following line of code

   vector[j] = res / m;
          j++;

I get the following error:

  

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

I hope you can help me please

    
asked by Elías Carranza 23.09.2018 в 17:06
source

3 answers

2

What happens to you is that res is never worth 1, or that your vector is too small to support the values your program needs. An error of that type unleashes it as soon as it happens res [100] = something, because it is outside the limits of your vector

    
answered by 23.09.2018 в 21:02
0

Since res never changes its value this loop runs infinitely, so j quickly becomes >=100 which means you're trying to access the position 101 of an array of only% co_of% positions.

while (res != 1)
{
    lim = sem;
    res = ((sem * a) + c) % m;
    sem = res;

    //EN ESTE PUNTO FALLA CUANDO j == 100
   vector[j] = res / m;
  j++;

}
    
answered by 23.09.2018 в 23:14
0

You are using a cycle while and you are not conditioning it with your iterator variable that indicates the index of your arrangement, when exceeding the maximum index of your array, it generates the exception:

while (j < vector.Length)
{
    lim = sem;
    res = ((sem * a) + c) % m;
    sem = res;

    //conta++;
    //Console.WriteLine("" + res / m);
    vector[j] = res / m;
    j++;
}
    
answered by 24.09.2018 в 18:52