检测数组中的最大值和最小值 [英] Detect maximum and minimum value in array

查看:120
本文介绍了检测数组中的最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码中的问题是什么?

What is the problem in this code?

#include
using namespace std;

int main ()

{	int const arraysize=10;
	int a[arraysize];
	int maximum=0;
	int minimum=a[0];
	int i;
	
	for ( i= 0 , i > (arraysize) , i++ )
	{
		cin>>a[i];
		
		if (a[i]>=maximum)
			maximum=a[i];

		if(a[i]<=minimum)
			minimum=a[i];
		
	}
	
		cout<<"the maximum of array a is : "<<maximum<<endl;
		cout<<"the minimum of array a is : "<<minimum<<endl;
 
		
return 0;

}

推荐答案

joek1991写道:
joek1991 wrote:

int maximum = 0; int minimum = a [0];

int maximum=0; int minimum=a[0];


更改为


change to

int maximum = INT_MIN;
int minimum = INT_MAX;







joek1991写道:
joek1991 wrote:

用于(i = 0,i>(arraysize),i ++)

for ( i= 0 , i > (arraysize) , i++ )


更改为


change to

for ( i = 0 ; i < arraysize ; i++ )




(恕我直言,好的C/C++教程会有所帮助)
:)




(IMHO a good C/C++ tutorial would help)
:)


IMO,通过使用调试器并单步执行程序,您将在1分钟内查明此问题.
学习如何使用调试器是值得的.
IMO, you would have pinpointed this problem in 1 minute, by using the debugger and single-step your program.
Learning how to use the debugger is well worth the time spent.


在将值赋给a [0]之前,您要声明最小值为a [0].
You are declaring minimum as a value a[0] before assigning a value to a[0].


这篇关于检测数组中的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆