Results 1 to 5 of 5

Thread: Minimum Number in Array

  1. #1
    cally0611 is offline C# Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0

    Minimum Number in Array

    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.

    Code:
     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);
            }

  2. #2
    jmcilhinney is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,468
    Rep Power
    118
    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.

  3. #3
    cally0611 is offline C# Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0
    Hi jmcilhinney ,
    Thanks for the reply, I will try debugging the code.

  4. #4
    jmcilhinney is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,468
    Rep Power
    118
    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.

  5. #5
    cally0611 is offline C# Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2017
    Posts
    3
    Rep Power
    0
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •