Question Minimum Number in Array

cally0611

New member
Joined
Feb 28, 2017
Messages
3
Programming Experience
Beginner
Hi, I am trying to find the minimum number in an array. I am supposed to get 1, but I am getting 3. I would just need someone to point out what am I missing and I will try to take it from there.

C#:
 int[] Array1 = new int[] { 25, 1, 30, 1, 3 };            bool set_condition;
            int num = 0;
            for(int i = 0; i<Array1.Length; i++)
            {
                for(int j = 0; j<Array1.Length; j++)
                {
                    
                    if (i != j)
                    { 
                        if (Array1[i] <= Array1[j])
                        {
                            Console.WriteLine("Array1[{0}] and Array1[{1}],{2},{3} = True ", i, j,Array1[i],Array1[j]);
                            set_condition = true;
                            num = Array1[i];
                        }
                        else
                        {
                            Console.WriteLine("Array1[{0}] and Array1[{1}],{2},{3} = False ", i, j, Array1[i], Array1[j]);
                            set_condition = false;
                            break;
                        }
                    }
                }
            
            }
            Console.WriteLine(num);
        }
 
What you're missing is a proper debugging technique. You've written that code so I would hope that you would understand what it is supposed to be doing at each step. If it doesn't produce the expected result and you can't see anything obvious just by reading the code then your next action is not to post but rather to debug the code. Place a breakpoint at the top of the code and then step through it line by line. At each step, examine the value of each relevant variable and expression to confirm that each is what you expect it to be. If, at any stage, execution steps to a line you don't expect or any variable or expression produces a value you don't expect then you have found a specific issue and you can investigate that specifically, e.g. how did that value get into that variable. If you can't work out the specific cause of the issue or the solution, THEN you would post here and provide us with all the relevant information from your debugging rather than just a block of code.
 
Unfortunately, very few tutorials or even courses talk about debugging but it is a very important skill so, if you feel you need more information, search the web for C# debugging information specifically. VS contains numerous tools to help watch your code and diagnose issues as it executes.
 
Right, thanks, I was outputting the variables to check on the output but debugging using the tools would give a clearer picture. Thank you for pointing it. I googled and I think my technique itself is wrong now, I will work on the code.
 
Back
Top Bottom