如何在C ++中显示一系列整数的最大值和最小值? [英] How do I display the maximum and minumum from a series of integers in C++?

查看:119
本文介绍了如何在C ++中显示一系列整数的最大值和最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是使用C ++编程的初学者。我需要编写一个程序,我应该显示输入的一系列整数的最大值和最小值,但我仍然坚持如何显示最小值。我得到它显示最大值,但不是最小值。有人可以帮助。这就是我所拥有的,但不知道如何解决它。



Hi,

I'm a beginner in programming using C++. I need to write a program where I should display the max and min of a series of integers entered, but I'm stuck on how to display the min. I got it to display the max, but not the min. Can someone pls help. This is what I have, but don't know how to fix it.

#include <iostream>
using namespace std;

int main()
{
	// Declare Variables
	int num;
	int min = 0;
	int max = 0;

	do
	{
		// Ask user to enter a series of positive integers
		cout << "Please enter a series of positive integers."
			<< endl << "Input as many as you want."
			<< endl << "When you are finished, enter -99"
			<< endl << "Integer: ";
		cin >> num;
		while (num != -99 && num < 0)
		{
			cout << "Error! That was not a positive integer."
				<< endl << "Please continue to enter a series of" 
						<< " poisitive integers."
				<< endl << "When you are finished, enter -99"
				<< endl << "Integer: ";
			cin >> num;
		}
		if (num != -99)
		{
			if (num > max)
			{
				max = num;
			}
			if (num < min)
			{
				min = num;
			}		

		}
	}while (num != -99);

	// Display the max and min values
	cout << "The maximum value is: " << max << endl;
	cout << "The minumum value is: " << min << endl;

	return 0;
}

推荐答案

int min = 0;





以非常高的价格开始分钟;不是零。



Start min at something very high; not zero.


我已经更改了你的代码。我用矢量做了你最小的最大值。请参考此链接。

http://www.mochima.com/tutorials/vectors.html [ ^ ]



仔细阅读并理解它的内容..

希望这有帮助...



建议代码
I have changed your code. I did your minimum maximum thing using vector. Here refer this link.
http://www.mochima.com/tutorials/vectors.html[^]

Read it carefully and understand what it says..
hope this helps...

suggested code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	// Declare Variables
	int num;
	int min = 0;
	int max = 0;
	vector<int>value;

	do
	{
		// Ask user to enter a series of positive integers
		cout << "Please enter a series of positive integers."
			<< endl << "Input as many as you want."
			<< endl << "When you are finished, enter -99"
			<< endl << "Integer: ";
		cin >> num;
		while (num != -99 && num < 0)
		{
			cout << "Error! That was not a positive integer."
				<< endl << "Please continue to enter a series of"
						<< " positive integers."
				<< endl << "When you are finished, enter -99"
				<< endl << "Integer: ";
			cin >> num;
		}


		if (num != -99)
		{
		    value.push_back(num);


		}
	}while (num != -99);

// to find the minimum and maximum value ..
   const int highest = *max_element (value.begin(), value.end());
   const int lowest = *min_element (value.begin(), value.end());

	// Display the max and min values
	cout << "The maximum value is: " << highest << endl;
	cout << "The minumum value is: " << lowest << endl;

	return 0;
}


如果是这样,PIEBALDconsult说的是对的。但是为了避免在第一轮输入-99并且只输入一个值,声明一个整数,最后做一些决策如下



If so what PIEBALDconsult said is right. But in order to avoid entering -99 at the first round and entering only one value, declare an integer and at the end do some decision making as follows

#include <iostream>
using namespace std;

int main()
{
	// Declare Variables
	int num;
	int min = 100;
	int max = 0;
	int i=0;

	do
	{
		// Ask user to enter a series of positive integers
		       cout << "Please enter a series of positive integers."
			<< endl << "Input as many as you want."
			<< endl << "When you are finished, enter -99"
			<< endl << "Integer: ";
		cin >> num;
		while (num != -99 && num < 0)
		{
			cout << "Error! That was not a positive integer."
				<< endl << "Please continue to enter a series of"
						<< " poisitive integers."
				<< endl << "When you are finished, enter -99"
				<< endl << "Integer: ";
			cin >> num;
		}
		if (num != -99)
		{
			if (num > max)
			{
				max = num;
			}
			if (num < min)
			{
				min = num;
			}

		}

		i++;
	}while (num != -99);

	// Display the max and min values
	if (i==1)
	{
	    	    cout<<"You did not enter any values...."<<endl;


	}
	else if(i==2)
	{
         cout << "The maximum value is: " << max << endl;
         cout <<  "The minumum value is: " << 0 << endl;



	}
	else
	{
	    cout << "The maximum value is: " << max << endl;
        cout <<  "The minumum value is: " << min << endl;

	}

	return 0;
}


这篇关于如何在C ++中显示一系列整数的最大值和最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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