-1

I'm a greenhorn when in comes to C++, but basically I'm trying to print an array with the following code:

#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++. 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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

1 Answers1

3

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).

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

What's the value of these variables? From the previous section its 0.0/0.0. By the almost universally used IEEE standard that is a special number called NaN, NaN stands for 'not a number'.

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

What's the value of these variables? lengthX is zero but Nx is NaN. Again by IEEE standards any operation on NaN resuls in another NaN, so these variables are NaN also.

So you end up calling generateRange with NaN for the first argument. Inside generateRange what's the value of this expression? Remember a is NaN.

while (a <= c) {

IEEE standard say that comparing NaN with anything is always false. So your while loop is never entered and nothing gets added to your array.

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).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
john
  • 85,011
  • 4
  • 57
  • 81