首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >民,最大,射程

民,最大,射程
EN

Stack Overflow用户
提问于 2016-02-11 01:48:48
回答 1查看 2.6K关注 0票数 0

我很难处理这个问题。它说,当用户输入一个最小范围和一个最大范围时,我已经让程序找到了最小、最大和范围。

这里是我的代码:

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#define pi 3.1416
#define POINTS 20


using namespace std;

int main()
{

    int Xmin, Xmax;
    double step;

    cout << "Enter a value for xMin and xMax:\n";
    cin >> Xmin >> Xmax;


    step = (double)(Xmax - Xmin) / (double)POINTS;

    cout << "X-VALUES " << "" << "| " << "" << "Y-VALUES" << endl;
    cout << "_________" << "" << "|_" << "" << "_________" << endl;



    for (int i = 0; i < POINTS; ++i)
    {

        double x = Xmin + (step * i);

        double y = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);


        cout << x << "\t     " << setprecision(2) << y << endl;
    }

    cout << "____________________" << endl;


    return 0;
}

我这里是我的程序的输出:

代码语言:javascript
复制
X-Value   |   Y-Value
__________|__________
 -2           -0.0043
 -1.8         -0.0982
 -1.6         0.0378
 -1.4         0.0438
 -1.2         0.0099
 -1           0.0618
 -0.8         -0.1118
 -0.6         -0.0198
 -0.4         -0.0047
 -0.2         -0.0184
 0            0.1257
 0.2          -0.0184
 0.4          -0.0047
 0.6          -0.0198
 0.8          -0.1118
 1            0.0618
 1.2          0.0099
 1.4          0.0438
 1.6          0.0738
 1.8          -0.0982
 2            -0.0043
─────────────────────

基本上,这个程序都准备好了,它有一个公式,根据Xmin和Xmax的用户输入来计算数字并列出它们。我应该让程序找到最小值,最大值,并从上表的Y值算出它的范围。

这是我找到min和max的代码。

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <limits>
#define pi 3.1416
#define POINTS 20


using namespace std;

int main()
{

    int Xmin, Xmax;
    double step;
    int max = numeric_limits<int> :: min();
    int min = numeric_limits<int> :: max();
    int num = 0;

    cout << "Enter a value for xMin and xMax:\n";
    cin >> Xmin >> Xmax;


    step = (double)(Xmax - Xmin) / (double)POINTS;

    cout << "X-VALUES " << "" << "| " << "" << "Y-VALUES" << endl;
    cout << "_________" << "" << "|_" << "" << "_________" << endl;



    for (int i = 0; i < POINTS; ++i)
    {

        double x = Xmin + (step * i);

        double y = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);

        //printf(" %f\t%f\n ", x, y);
        cout << x << "\t     " << setprecision(2) << y << endl;
    }

    cout << "____________________" << endl;

    while (cout << "Enter a value for xMin and xMax:\n" &&
        cin >> Xmin >> Xmax)
    {
        if (num > max) max = num;
        if (num < min) min = num;
    }

    cout << "max is: " << max << '\n'
        << "min is: " << min << '\n';


    return 0;
}

它运行,但它没有打印出最小或最大。它只是在“输入Xmin和Xmax”上重复程序。但当我进入任何一个杠杆,它打印出最小和最大。帮帮我。我很困惑。

EN

回答 1

Stack Overflow用户

发布于 2016-02-11 01:58:31

局部变量num从未在函数中分配值。在while循环中使用它,但不输入它。

当您声明一个变量时,应该始终初始化它。

编辑1: Min与Max

您可以为任何变量保持运行的最小值和最大值。将最大变量设置为最低值,将最小值设置为最大值。

计算y变量后,执行以下操作:

代码语言:javascript
复制
if ( y > max_y) max_y = y;
if ( y < min_y) min_y = y;

这将保持运行的最小和最大。

我建议对sum变量执行此操作。你需要一个和变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35329409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档