Matrix problem c # select couples

0

First of all explain that the "exercise" that I try to do is a small set of pairs, in which you enter some coordinates and you are shown the value stored in that coordinate. At the moment that two of the values of the coordinates that you have entered are equal (for example, I put (0,1) and I get 3, then I put (2,4) and I get 3. If I left 4 I would ask again coordinates for the two values) should exit from the [...] while. I have the following problem and that no matter how much I think about it, I can not solve:

When selecting a number from within the matrix, I am saved and I can not change the value that I have to get from the matrix the second time I enter the numbers. He prints only the first number, the second never, he copies it from the first one.

The variable "sig" is not in use to check that the numbers that are chosen from the matrix are different.

    static void Main(string[] args)
    {
        int[,] matriz2 = new int[5, 5];           
        Random ale = new Random();
        bool acierto = false;
        int guardo = 0, guardo2 = 16;
        int intentos = 0;
        string sig = "?";
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                matriz2[i, j] = ale.Next(0, 11);
                Console.Write("\t{0}", matriz2[i, j]);
            }
            Console.WriteLine();
        }         
        do
        {
            Console.WriteLine("Introduzca las coordenadas.");
            int numero = int.Parse(Console.ReadLine());
            int numero2 = int.Parse(Console.ReadLine());
            Console.Write("Mostramos el valor: ");
            Console.WriteLine(funcion1(matriz2, numero, numero2));
            guardo = matriz2[numero, numero2];  
            if (guardo == guardo2)
                acierto = true;
            guardo2 = guardo;
            intentos++;
        } while (acierto != true);
        Console.Read();

    }
    static int funcion1(int[,] ar, int num1, int num2)
    {
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                ar[i, j] = ar[num1, num2];
            }
        }
        return ar[num1, num2];
    }

  }
}
    
asked by cRyZaStAyLo 22.09.2017 в 13:43
source

2 answers

0

The problem is in the funcion1 function.

I do not know what you intend to do exactly in that function but what you are really doing is assigning the value of the selected element ( ar[num1, num2] ) to all the positions of the array. With what the array remains with all the same numbers. That's why he returns the same number again and again.

Actually you could just return the value directly from the array. As I said: I do not know what you're trying to do exactly with that function:

static void Main(string[] args)
{

    int[,] matriz2 = new int[5, 5];
    Random ale = new Random();
    bool acierto = false;
    int guardo = 0, guardo2 = 16;
    int intentos = 0;
    string sig = "?";
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            matriz2[i, j] = ale.Next(0, 11);
            Console.Write("\t{0}", matriz2[i, j]);
        }
        Console.WriteLine();
    }
    do
    {
        Console.WriteLine("Introduzca las coordenadas.");
        int numero = int.Parse(Console.ReadLine());
        int numero2 = int.Parse(Console.ReadLine());
        Console.Write("Mostramos el valor: ");
        Console.WriteLine(matriz2[ numero, numero2]);
        guardo = matriz2[numero, numero2];
        if (guardo == guardo2)
            acierto = true;
        guardo2 = guardo;
        intentos++;
    } while (acierto != true);
    Console.Read();

}
    
answered by 22.09.2017 / 15:10
source
0

First I do not know why you start the variable to 16, try to add as I added an else statement, so that you do not always do the same assignment, it should work like that

static void Main(string[] args)
    {
        int[,] matriz2 = new int[5, 5];           
        Random ale = new Random();
        bool acierto = false;
        int guardo = 0;
        int guardo2 = 0;
        int intentos = 0;
        string sig = "?";
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                matriz2[i, j] = ale.Next(0, 11);
                Console.Write("\t{0}", matriz2[i, j]);
            }
            Console.WriteLine();
        }         
        do
        {
            Console.WriteLine("Introduzca las coordenadas.");
            int numero = int.Parse(Console.ReadLine());
            int numero2 = int.Parse(Console.ReadLine());
            Console.Write("Mostramos el valor: ");
            Console.WriteLine(funcion1(matriz2, numero, numero2));
            guardo = matriz2[numero, numero2];  
            if (guardo == guardo2){
                acierto = true;
            }else{
            guardo2 = guardo;
            intentos++;
            }
        } while (acierto != true);
        Console.Read();

    }
    static int funcion1(int[,] ar, int num1, int num2)
    {
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                ar[i, j] = ar[num1, num2];
            }
        }
        return ar[num1, num2];
    }

  }
}
    
answered by 22.09.2017 в 15:00