当我尝试将我的函数用于最大值时,它会一直返回错误的值。
#include <iostream>
#include <iomanip>
using namespace std;
int largest(int[], int);
main()
{
const int size = 3;
int a[size];
for (int i = 0; i < size; i++)
{ cout << "Enter an array element ";
cin >> a[i];
}
int large = largest(a,size);
cout << "Largest is " << largest;
return 0;
}
int largest (int a[], int size )
{
int largest; largest = a[0];
for (int i = 1; i < size; i++)
{
if(a[i] > largest)
{
largest = a[i];
return largest; }
}
}
答案 0 :(得分:0)
返回语句应该在for循环之后才能找到最大值。
int largest (int a[], int size )
{
int largest; largest = a[0];
for (int i = 1; i < size; i++)
{
if(a[i] > largest)
largest = a[i];
}
return largest;
}
答案 1 :(得分:0)
你有两个问题:a)你第一次回来是&gt; a [0],b)您在<RelativePanel>
<Button Name="Button1"
Content="Button 1"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.AlignVerticalCenterWithPanel="True"/>
<Button Name="Button2"
Content="Button 2"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.AlignVerticalCenterWithPanel="True"
RelativePanel.Below="Button1"/>
<Button Name="Button3"
Content="Button 3"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.AlignVerticalCenterWithPanel="True"
RelativePanel.Below="Button2"/>
</RelativePanel>
中捕获返回值,但是您打印large
,这将打印该函数的地址。
largest