我正在尝试在类中创建一个程序,它将在给定的序列中找到2个最大和最小的整数,然后将它们打印给用户。 示例运行看起来像这样:
Enter the sequence size: 8
Enter the sequence: 5 8 9 12 -6 4 -8 10
The two smallest values are: -8 -6
The two largest values are: 12 10
我不允许使用sort或数组。我整夜都在这里,但我似乎无法弄明白,有人能指出我正确的方向吗?这是我目前所困扰的地方 - 它不会编译,因为'small1'和'large1'变量没有被初始化,但是如果我将它们设置为零,它们在样本运行中保持为零。
int small1, small2, large1, large2, loopcount, sequencevalue;
// Ask the user to enter the first number
cout << "Sequence Size: ";
cin >> loopcount;
// Enter the sequence and start the loop
cout << "\nEnter the Sequence: ";
for (int i = 0; i < loopcount; i++)
{
cin >> sequencevalue;
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
if (sequencevalue > large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Small variables
if (small1 == 0)
{
small1 = sequencevalue;
}
else
{
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
}
// Large variables
if (large1 == 0)
{
large1 = sequencevalue;
}
else
{
if (sequencevalue < large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";
我非常感谢你对此事的任何帮助,谢谢你的时间。
答案 0 :(得分:2)
您的程序有未定义的行为,因为您在初始化之前使用small1
,small2
,large1
,large2
。
初始化small1
和small2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
初始化large1
和large2
的正确方法是:
int large1 = INT_MIN;
int large2 = INT_MIN;
<强>更新强>
如果没有排序数组,正确更新数字的逻辑就不是直截了当的。下面是一个正确计算small1
和small2
的工作程序。我会留给您将其扩展为计算large1
和large2
。
#include <iostream>
#include <sstream>
#include <climits>
#include <algorithm>
int main()
{
std::istringstream str("8 5 8 9 12 -6 4 -8 10");
int small1 = INT_MAX;
int small2 = INT_MAX;
int loopcount;
bool small1_found = false;
str >> loopcount;
for (int i = 0; i < loopcount; ++i )
{
int sequencevalue;
str >> sequencevalue;
if ( sequencevalue < small1 )
{
int temp = small1;
small1 = sequencevalue;
if ( small1_found )
{
small2 = std::max(sequencevalue, temp);
}
small1_found = true;
}
}
std::cout << "Two smallest values: " << small1 << " " << small2 << "\n";
}
输出:
Two smallest values: -8 -6
答案 1 :(得分:0)
您正在寻找<climits>
请注意此程序的输出
#include <climits>
#include <iostream>
int main() {
std::cout << INT_MAX << std::endl;
std::cout << INT_MIN << std::endl;
return 0;
}
答案 2 :(得分:0)
我包括了攀登并更正了您的代码。
代码示例:
#include <iostream>
#include <climits>
using namespace std;
int main(){
int count = 0;
cout << "Sequence Size: ";
cin >> count;
int sequencevalue;
int large1 = INT_MIN;
int large2 = INT_MIN;
int small1 = INT_MAX;
int small2 = INT_MAX;
for (int i = 0; i < count; i++){
cin >> sequencevalue;
if (sequencevalue >= large1) {
large2 = large1;
large1 = sequencevalue;
}
else if (sequencevalue > large2){
large2 = sequencevalue;
}
if (sequencevalue <= small1) {
small2 = small1;
small1 = sequencevalue;
}
else if (sequencevalue < small2){
small2 = sequencevalue;
}
}
cout << "Sequence size is " << count << endl;
if (0 == count) {}
else if (1 == count){
cout << "Smallest value: " << small1 << endl;
cout << "Largest value: " << large1 << endl;
}
else{
cout << "First and second smallest value: " << small1 << " " << small2 << endl;
cout << "First and second largest value: " << large1 << " " << large2 << endl;
}
return 0;
}
上面的代码示例将相同的数字视为唯一元素(可能导致相同的第一个和第二个最小/最大)。
如果您需要忽略相同的nubers作为唯一元素,那么for循环应该如下所示:
for (int i = 0; i < count; i++){
cin >> sequencevalue;
if (sequencevalue > large1) {
large2 = large1;
large1 = sequencevalue;
}
else if (sequencevalue != large1 && sequencevalue > large2){
large2 = sequencevalue;
}
if (sequencevalue < small1) {
small2 = small1;
small1 = sequencevalue;
}
else if (sequencevalue != small1 && sequencevalue < small2){
small2 = sequencevalue;
}
}
答案 3 :(得分:0)
int small1, small2, large1, large2, loopcount, sequencevalue;
// Ask the user to enter the first number
cout << "Sequence Size: ";
cin >> loopcount;
// Enter the sequence and start the loop
cout << "\nEnter the Sequence: ";
for (int i = 0; i < loopcount; i++)
{
cin >> sequencevalue;
if(i==0)
small1=sequencevalue;
small2=sequencevalue;
large2=sequencevalue;
large1=sequencevalue;}
if(i==1)
{if(sequencevalue>small1)small2=sequencevalue;
else{
small2=small1;
small1=sequencevalue;
}
large2=small1;
large1=small2;
}
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
if (sequencevalue > large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";
答案 4 :(得分:0)
你在这里
#include <iostream>
int main()
{
while ( true )
{
// Ask the user to enter the first number
std::cout << "Sequence Size: ";
unsigned int loopcount;
if ( not ( std::cin >> loopcount ) || ( loopcount == 0 ) ) break;
int small1, small2, large1, large2;
// Enter the sequence and start the loop
std::cout << "\nEnter the Sequence: ";
unsigned int i = 0;
for ( ; i < loopcount; i++ )
{
int sequencevalue;
std::cin >> sequencevalue;
if ( i == 0 )
{
small1 = sequencevalue;
large1 = sequencevalue;
}
else
{
if ( large1 < sequencevalue )
{
large2 = large1;
large1 = sequencevalue;
}
else if ( i == 1 || large2 < sequencevalue )
{
large2 = sequencevalue;
}
if ( sequencevalue < small1 )
{
small2 = small1;
small1 = sequencevalue;
}
else if ( i == 1 || sequencevalue < small2 )
{
small2 = sequencevalue;
}
}
}
if ( i == 1 )
{
printf( "There is only one largest value %d\n",
large1 );
printf( "And only one mallest value %d\n",
small1 );
}
else
{
printf( "The first largest value is %d and the second largest value is %d\n",
large1, large2 );
printf( "And the first smallest value is %d and the second smallest value is %d\n",
small1, small2 );
}
}
return 0;
}
程序输出看起来像
Sequence Size: 10
Enter the Sequence: 1 9 8 0 7 6 5 4 3 2
The first largest value is 9 and the second largest value is 8
And the first smallest value is 0 and the second smallest value is 1
Sequence Size: 0