简体   繁体   English

用C ++打印一个简单的数组

[英]Printing a simple array in C++

I'm a greenhorn when in comes to C++, but basically I'm trying to print an array with the following code: 当我使用C ++时,我是一个新手,但是基本上我正在尝试使用以下代码打印数组:

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <numeric>
#include <math.h>
#include <iterator>
#include "rect_plate.h"
#include "numeric_constants.h"

using namespace std;
using std::vector;

// function prototypes
vector<double> generateRange(double a, double b, double c);

double lengthX;
double lengthY;
double resX;
double resY;

double Nx = lengthX / resX;
double Ny = lengthY / resY;

double deltaX = lengthX / Nx;
double deltaY = lengthY / Ny;


// function to calculate array
vector<double> generateRange(double a, double b, double c) {
    vector<double> array;
    while (a <= c) {
        array.push_back(a);
        a += b;         // could recode to better handle rounding errors
    }
    return array;
}

vector<double> xSpace = generateRange(deltaX / 2, deltaX, lengthX);
vector<double> ySpace = generateRange(deltaY / 2, deltaY, lengthY);

int main()
{
    // print array
    for (int i = 0; i < Nx - 1; i++)
    {
        cout << "Test" << xSpace[i] << endl;
    }
    system("pause");
    return 0;
}

but am not getting any output in the console when I run the executable file. 但是当我运行可执行文件时,控制台中没有任何输出。 All I get is 'press a key to continue' and then it exits the console. 我得到的只是“按一个键继续”,然后退出控制台。

I'm in the process of converting some of my procedural MATLAB into OO-C++. 我正在将一些过程MATLAB转换为OO-C ++。 I had a class setup, with objects, but removed those for now to check if my variables were being initialized properly. 我有一个带有对象的类设置,但是现在删除了那些对象以检查我的变量是否正确初始化。 I know in C#, there's a way to step through the code (F10 - I'm using VS2017), and when you hover the mouse over a given variable, it shows you the value, but haven't found anything similar for C++, so I'm just printing (or at least trying to print) everything for now. 我知道在C#中,有一种方法可以遍历代码(F10-我正在使用VS2017),当您将鼠标悬停在给定变量上时,它会向您显示值,但没有发现与C ++类似的东西,所以我现在只是打印(或至少尝试打印)所有内容。

OK let's take it step by step 好,让我们逐步进行

double lengthX;
double lengthY;
double resX;
double resY;

What's the value of these variable? 这些变量的值是多少? Rules of C++ says they're all 0.0 (because they're uninitialised globals , and globals get zero-initialised first). C ++规则表示它们全为0.0 (因为它们是未初始化的globals ,并且globals首先要零初始化)。

double Nx = lengthX / resX;
double Ny = lengthY / resY;

What's the value of these variables? 这些变量的价值是什么? From the previous section its 0.0/0.0 . 从上一节开始,它是0.0/0.0 By the almost universally used IEEE standard that is a special number called NaN , NaN stands for 'not a number'. 根据几乎普遍使用的IEEE标准(一个特殊的数字,称为NaN ,NaN代表“非数字”。

double deltaX = lengthX / Nx;
double deltaY = lengthY / Ny;

What's the value of these variables? 这些变量的价值是什么? lengthX is zero but Nx is NaN . lengthX为零,但NxNaN Again by IEEE standards any operation on NaN resuls in another NaN , so these variables are NaN also. 同样,根据IEEE标准,在另一个NaNNaN resul进行的任何操作,因此这些变量也是NaN

So you end up calling generateRange with NaN for the first argument. 因此,您最后使用NaN调用了generateRange作为第一个参数。 Inside generateRange what's the value of this expression? generateRange内部,此表达式的值是什么? Remember a is NaN . 记住aNaN

while (a <= c) {

IEEE standard say that comparing NaN with anything is always false . IEEE标准说,将NaN与任何事物进行比较始终是false So your while loop is never entered and nothing gets added to your array. 因此,您的while循环永远不会输入,并且什么也不会添加到您的数组中。

Obviously the general problem with your code is that you never gave any values to your initial variables. 显然,代码的一般问题是您从未给初始变量提供任何值。 You should assign some numbers to those variables. 您应该为这些变量分配一些数字。 Then it would be traditional to call generateRange from inside your main function after you have set all the variables up (maybe with input from the user). 然后, 设置完所有变量之后(可能是来自用户的输入),从main函数内部调用generateRange是传统的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM