gpt4 book ai didi

c++ - 跟踪循环中的最小值和最大值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:31 26 4
gpt4 key购买 nike

你能帮我解决一个简单的问题吗?我对 C++ 非常陌生,并从 Bjarne Stroustrup 的“编程:使用 C++ 的原理和实践”一书学习。我以前从未学过 C++,所以我不熟悉许多有用的功能。演习说:

"6. Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it’s the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number"

我不知道如何在不使用 vector 的情况下正确执行此操作。这是我的代码:

#include "C:/std_lib_facilities.h"

int main()
{
double a, b,differ=0;
char c=' ';
cout << "Enter two values: \n";
while (c != '|' && cin >> a >> b )
{
if (a > b)
{
cout << "The smaller value is: "<< b << " and the larger value is: " << a << "\n \n";
differ = a - b;
if (differ < 1.0 / 100)
cout << "Numbers are almost equal\n\n";
}
else if (a < b)
{
cout << "The smaller value is: " << a << " and the larger value is: " << b << "\n \n";
differ = b - a;
if (differ < 1.0 / 100)
cout << "Numbers are almost equal\n\n";
}
else
{
cout << "These values are equal!\n";
}
cout << "Enter a character | to break loop: \n";
cin >> c;
}
cout << "You have exited the loop.\n";
keep_window_open();
}

这里是前面的步骤,这些我已经用上面的代码解决了:

  1. Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered.
    1. Change the program to write out the smaller value is: followed by the smaller of the numbers and the larger value is: followed by the larger value.
    2. Augment the program so that it writes the line the numbers are equal (only) if they are equal.
    3. Change the program so that it uses doubles instead of ints.
    4. Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100.

你能给我一些提示,告诉我如何进行第 6 步吗?我有一些想法,但没有一个奏效..

这是新代码:

#include "C:/std_lib_facilities.h"

int main()
{
double smallestSoFar = std::numeric_limits<double>::max();
double largestSoFar = std::numeric_limits<double>::min();
double a,differ=0;
char c=' ';
cout << "Enter value: \n";

while (c != '|' && cin >> a)
{
if (a > largestSoFar)
{
largestSoFar = a;
cout <<"Largest so far is: "<< largestSoFar << endl;
}
else if (a < smallestSoFar)
{
smallestSoFar = a;
cout <<"Smallest so far is: "<< smallestSoFar << endl;
}
else if(smallestSoFar >= a && a<=largestSoFar)
cout << a << endl;
cout << "Enter a character | to break loop: \n";
cin >> c;
}
cout << "You have exited the loop.\n";
keep_window_open();
}

最佳答案

I do not know how to do this correctly without using vector.

为此您不需要 vector 。描述正确地说两个变量就足够了:

// Declare these variables before the loop
double smallestSoFar = std::numeric_limits<double>::max();
double largestSoFar = std::numeric_limits<double>::min();

修改您的循环以读入a,而不是同时读入ab。根据 smallestSoFarlargestSoFar 检查新输入的值,进行打印,并根据需要重新分配最小值和最大值。请注意,第一次您应该会看到两个打印输出 - 迄今为止最大的和目前最小的。

关于c++ - 跟踪循环中的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37236764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com