获取数组的最大值
Get the biggest value of an array
我有一个文本框来写数组的位置,还有一个文本框来写那个位置的值。每次我想添加一个值和一个位置时,我单击按钮 btnStoreValue
我为另一个练习创建了一个函数 (CompareTwoNumbers),该函数比较两个数字 returns 最大的
使用该函数并避免使用 > 和 < 之类的比较字符我应该得到数组的最大值
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();
}
我尝试了多种方法,使用多个变量,我尝试将其写在纸上以更好地理解事物,但我被卡住了。我不知道如何使用我在上一个练习中创建的函数找到该值(假设我创建的函数是正确的)
这使用元组通过相同的方法为您提供最大索引和最大值:
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);
}
所以,你的问题的核心部分是你想知道如何使用你的辅助函数 CompareTwoNumbers
找到数组中最大的数字,然后找出最大数字的值和位置是。
根据我的上述理解,您的框架几乎已正确设置。
首先,CompareTwoNumbers 应该更新为 return 布尔值。这样做可以让您有条件地更新持有最大数值和位置的变量。
private int CompareTwoNumbers(int i, int j)
{
if (i < j)
{
return true;
}
return false;
}
要知道(未排序的)数组中的最大值是多少,您需要遍历每个值。这样做时,您需要跟踪最大值的值和位置,只有在找到更大的值时才更新它。
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();
}
我有一个文本框来写数组的位置,还有一个文本框来写那个位置的值。每次我想添加一个值和一个位置时,我单击按钮 btnStoreValue
我为另一个练习创建了一个函数 (CompareTwoNumbers),该函数比较两个数字 returns 最大的
使用该函数并避免使用 > 和 < 之类的比较字符我应该得到数组的最大值
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();
}
我尝试了多种方法,使用多个变量,我尝试将其写在纸上以更好地理解事物,但我被卡住了。我不知道如何使用我在上一个练习中创建的函数找到该值(假设我创建的函数是正确的)
这使用元组通过相同的方法为您提供最大索引和最大值:
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);
}
所以,你的问题的核心部分是你想知道如何使用你的辅助函数 CompareTwoNumbers
找到数组中最大的数字,然后找出最大数字的值和位置是。
根据我的上述理解,您的框架几乎已正确设置。
首先,CompareTwoNumbers 应该更新为 return 布尔值。这样做可以让您有条件地更新持有最大数值和位置的变量。
private int CompareTwoNumbers(int i, int j)
{
if (i < j)
{
return true;
}
return false;
}
要知道(未排序的)数组中的最大值是多少,您需要遍历每个值。这样做时,您需要跟踪最大值的值和位置,只有在找到更大的值时才更新它。
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();
}