到目前为止,跟踪哪个值最小,哪个值最大

麦可

您能帮我解决简单的问题吗?我对C ++感到非常新鲜,并从《 Bjarne Stroustrup的编程:使用C ++的原理和实践》一书中学习。我以前从未学习过C ++,所以我对许多有用的功能并不熟悉。演习说:

“ 6.现在,更改循环的主体,以使它每次读到的都是两倍。定义两个变量以跟踪到目前为止所看到的最小和最大值。每次通过循环写入减去输入的值。如果到目前为止是最小的,则在数字之后写下最小值。如果到目前为止最大的,在数字后写入迄今为止最大值。”

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

#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. 编写一个由while循环组成的程序(每次循环)读取两个int,然后打印它们。当终止符“ |”时退出程序 输入。
    1. 更改程序以写出较小的值:后跟较小的数字,较大的值是:后接较大的值。
    2. 增强程序,以便在数字相等的情况下(仅)将数字相等的行写入行中。
    3. 更改程序,使其使用双精度而不是整数。
    4. 更改程序,使其写出的数字几乎相等,如果两个数字相差小于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();
}
谢尔盖(Sergey Kalinichenko)

我不知道如何在不使用向量的情况下正确执行此操作。

您不需要向量。描述正确地指出,两个变量就足够了:

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何计算到目前为止在序列中看到的最大值或最小值及其关联的ID?

到目前为止,数据框列的最大值

向左清除,但到目前为止

OffsetDateTime 到目前为止 Android

我试图在用户输入税金时获得g_total。到目前为止,它没有返回任何值。到目前为止我已经尝试过这件事

JS到目前为止的最小回溯算法错误

重新采样到目前为止的时间序列

到目前为止是否有任何恶意 PPA?

到目前为止的Django Datetime字段

Orchard的Context数据-到目前为止还很近

加上到目前为止的月份,分数

在SDL2,C ++中,TTF_SizeText()没有为“ i”,“ j”和“ 1”(到目前为止发现)返回正确的值

我需要能够将文本框中的值插入到Access数据库中,但是到目前为止它不起作用

到目前为止,每15分钟计算一次费用

到目前为止,是否有任何软件可以显示ssd使用情况?

到目前为止,如何在联合中转换空列?

到目前为止,基于COUNT个DISTINCT`foreign_key`的限制

到目前为止我还无法理解的scala slick方法

到目前为止,无法在Android上实现无间隙音频循环

奖励广告是否可以检查用户到目前为止获得的奖励?

到目前为止,Apache Jena的所有发行说明

到目前为止,如何获取经过的毫秒数

在Python中使用(到目前为止)Lambda函数中的未定义函数

查看到目前为止加载的所有 Spring 配置文件

我可以看到本月到目前为止的当前帐单吗?(火力爆发计划)

如果取消Subversion UPDATE,那么到目前为止下载的文件会怎样?

到目前为止,32位Windows安装有多普遍?

用到目前为止已出现的次数替换每次出现的字符串

如何获取到目前为止的总分钟数?