c++ - 如何将一系列计算值发送到数组中?

标签 c++ arrays

在我的程序中,我假设要求用户输入一系列 x 值,最小值和最大值 x。我假设在这两个极端中计算了 20 个数字。然后我想在表格中显示信息,然后找到有关这些数字的一些统计信息。我不确定如何将计算值发送到数组中,以便我可以计算统计数据,例如均值和范围。我对数组没有太多经验,所以任何建议都会有所帮助。这是我目前所拥有的

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
   double xmin, xmax;
   const int POINTS = 20;
   const double PI = 3.1416;
   double increments;
   int counter = 0;
   double array[POINTS];

   cout << "Enter in a value for the minimum x value: ";
   cin >> xmin;
   cout << "Enter in a value for the maximum x value: ";
   cin >> xmax;

   increments = (abs(xmin) + xmax) / POINTS;

   double x = xmin + increments * counter;
   double min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
   double max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

   cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
   cout << setw(32) << setfill('-') << " " << endl;
   cout << setfill(' ');

   while (x <= xmax)
   {
      cout << fixed << showpos << setw(15) << setprecision(2) << x <<     setw(15)
     << setprecision(4) << 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) << endl;
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) > max)
         max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) < min)
         min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      counter++;
      x = xmin + increments * counter;

   } // End of while (x <= xmax)

   system("pause");
   return 0;
} // End of function main()

最佳答案

我重新编辑了您的代码以添加一些 vector<> 实例,希望您能理解

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
   double xmin, xmax;
   const int POINTS = 20;
   const double PI = 3.1416;
   double increments;
   int counter = 0;
   double array[POINTS];

   vector<vector<float> > values;

   cout << "Enter in a value for the minimum x value: ";
   cin >> xmin;
   cout << "Enter in a value for the maximum x value: ";
   cin >> xmax;

   increments = (abs(xmin) + xmax) / POINTS;

   double x = xmin + increments * counter;
   double min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
   double max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

   cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
   cout << setw(32) << setfill('-') << " " << endl;
   cout << setfill(' ');
   vector<float> auxiliar;

   while (x <= xmax)
   {
      auxiliar.resize(2);
      auxiliar[0] = x;
      auxiliar[1] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

      values.push_back(auxiliar);
      auxiliar.clear();

      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) > max)
         max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) < min)
         min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      counter++;
      x = xmin + increments * counter;

   }

   for(vector<float> i:values)
     cout << fixed << showpos << setw(15) << setprecision(2) << i[0] <<     setw(15) << setprecision(4) << i[1] << endl;
 }

我使用了一个辅助工具,使用 vector<> 模板中的 push_back() 方法将 2 元素 vector 添加到 vector > 对象。我希望它能帮助你! :D

输出与原来相同(xmin = 2, xmax = 3)

Enter in a value for the minimum x value: 2
Enter in a value for the maximum x value: 3
            x |           f(x)
------------------------------- 
          +2.00        -0.0043
          +2.25        -0.0759
          +2.50        +0.0800
          +2.75        +0.0155
          +3.00        +0.0425

关于c++ - 如何将一系列计算值发送到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249090/

相关文章:

javascript - 如何根据对象属性值更改表格行的颜色?

c++ - C++中的进程管理

c++ - 段错误(核心转储)错误

c++ - 从 vector C++ 中删除最后一个元素时出现段错误

javascript - 如何匹配页面中的所有元素并获取其内容?

php - 重新排列 PHP 数组并返回 3 个最低值

c# - ReferenceType 和 ValueType 的数组是对象数组的无效参数

c++ - 通过分发目标文件加速编译

c++ - 在编译时/模板检查变量的值

java - 如何找到数组的所有连续子数组组合并打印它