Home > Blockchain >  Get the biggest value of an array
Get the biggest value of an array

Time:03-02

I have a textbox to write the position of an array and a textbox to write the value of that position. Every time I want to add a value and a position I click the button btnStoreValue

I created a function (CompareTwoNumbers) for another exercise that compares two numbers and returns the biggest

Using that function and avoiding the use of comparison characters like > and < I'm supposed to get the biggest value of the array


public partial class Form1 : ExerciseArray
    {
        int[] numbers = new int[10];


private int CompareTwoNumbers(int i, int j)
        {
            if (i < j)
            {
                return j;
            }
            return i;
        }

private void btnBiggestValue_Click(object sender, EventArgs e)
        {   
            //int n=1;
            int counter = 0;
            int highestPosition = CompareTwoNumbers(0, 1);

            for(int i=0; i<10; i  ){
                
                 //int j = CompareTwoNumbers(numbers[i], numbers[i 1])
                //n = CompareTwoNumbers(numbers[n], numbers[i 1]

                  counter = CompareTwoNumbers(highestPosition, i);
        }

        txtBiggestValuePosition.Text= n.ToString();
        txtBiggestValue.Text=numbers[n].ToString();
    }

I've tried multiple things, using multiple variables, I tried to write it on paper to try to understand things better and I'm stuck. I don't know how is it possible to find that value using the function I created on the previous exercise (assuming the function I created is correct)

CodePudding user response:

So, the core part of your question is that you want to know how to find the biggest number in an array using your helper function CompareTwoNumbers and then figure out what the value and position of the biggest number is.

Based on my understanding above, you have the framework almost set up correctly.

First off, CompareTwoNumbers should be updated to return a bool. Doing this will let you conditionally update your variables holding the biggest number value and position.

private int CompareTwoNumbers(int i, int j)
{
    if (i < j)
    {
        return true;
    }
    return false;
}

To know what the largest value in an (unsorted) array is, you will need to iterate through every value. While doing so, you need to keep track of the value and position of the biggest value, only updating it when a bigger value is found.

private void btnBiggestValue_Click(object sender, EventArgs e)
{   
    // Store the bigget number's index and value
    // We start with the first index and corresponding
    // value to give us a starting point.
    int biggestNumberIndex = 0;
    int biggestNumber = numbers[0];
    
    // Iterate through the array of numbers to find 
    // the biggest number and its index
    for(int i=0; i<10; i  )
    {
        // If the current number is larger than the 
        // currently stored biggest number...
        if(CompareTwoNumbers(biggestNumber, numbers[i])
        {
            // ...then update the value and index with
            // the new biggest number.
            biggestNumber = number[i];
            biggestNumberIndex = i;
        }
    }
    
    // Finally, update the text fields with
    // the correct biggest value and biggest 
    // value position.
    txtBiggestValuePosition.Text= biggestNumberIndex.ToString();
    txtBiggestValue.Text=numbers[biggestNumberIndex].ToString();
}

CodePudding user response:

This uses a Tuple to give you both the max index and max value from the same method:

public (int, int) FindMaxValue(int[] items)
{
    int maxValue = items[0];
    int maxIndex = 0;
    for(int i=1;i<items.Length;i  )
    {
        if (items[i] > maxValue)
        {
            maxValue = items[i];
            maxIndex = i;
        }
    }
    return (maxIndex, maxValue);
}
  • Related