I'm having some trouble outputting a 2D vector as a grid in the Terminal.

What I've done is create a class Grid that creates a grid (in this case, a square grid), by creating a set of x and y co-ordinates whose spacing is dx and dy . I can then set the domain, by choosing maximum and minimum values for x and y. I then want to create a value V(x,y) which is a function of x,y[j].

Maybe some code will give a better idea of what I'm trying...


#include <stdlib.h>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

class Grid
{
    // min/max value of square grid
    double xmin,xmax,ymin,ymax;
    public:
        //x,y coordinates
        vector<double> x,y;
        //value V(x,y) = v(xi,yj) = v[i][j]
        vector< vector<double> >v;
        //grid spaces
        double dx,dy;
        // change grid size
        void changeGridDomain(double xmin_,double xmax_,double ymin_,double ymax_)
        {
            xmin=xmin_;
            xmax=xmax_;
            ymin=ymin_;
            ymax=ymax_;
        }
        // change grid storage
        void changeGridStorage(int n,int m)
        {
            x.resize(n);
            y.resize(m);

            for (int k=0;k<=m;k++)
                v.resize(n,vector<double>(k,0));
        }
        //update grid
        void updateGrid()
        {
            dx=(xmax-xmin)/(x.size()-1);
            dy=(ymax-ymin)/(y.size()-1);
            
            for(int i=0; i<x.size();i++)
            {
                x[i]=xmin+i*dx;
            }

            for(int j=0;j<y.size();j++)
            {
                y[j]=ymin+j*dy;
            }
        }
        void GridFunction()
        {
            for(int i=0; i<x.size(); i++)
            for(int j=0; j<y.size(); j++)
            {
                //V(x_i,y_j)
                v[i][j]=x[i]*y[j];
                cout << v[i][j] << "\n";
            }
        }
};

int main() {

    Grid g;
    //create an 11x11 grid
    g.changeGridStorage(11,11);
    //fix the max and min values of x and y to be 0 and 1
    g.changeGridDomain(0,1,0,1);
    g.updateGrid();
    //implemet the function v
    g.GridFunction();

    return (EXIT_SUCCESS);
}

Here, I've simply got my function, V, to be the product of the x and y co-ordinates.

The problem is, the output is just a line of 100 values corresponding to x_i*y_j. What I want is a grid, so that I can maybe do a surface plot.

Any ideas on how I'd do this?

That code doesn't run for me. It crashes on line 58 v[i][j]=x[i]*y[j]; which means either x, y, or v isn't big enough.

You problems are in your fuction changeGridStorage.

You do this

x.resize(n);    // this is fine
y.resize(m);    

for(int k=0;k<=m;k++)  // I REALLY don't think that you need <= BUT this isn't your 
                       //   main problem

  v.resize(n,vector<double>(k,0));    // Ok talk about packing a lot into one line:
                                      // I will discuss it below

Now you have a v.resize() method. The prototypee for that, the simple v.resize(int,T c = T()) were T is the type of the vector.

In your case you are setting c to be vector<double>(k,0); and what you are doing is setting the size of the secondary vector in the pair to be 0,1,2,3,4 .... AND you are constantly resizing vector v.

So write it as

v.resize(n,vector<double>(m,0.0));

Then your code doesn't crash. This is another one of those reasons that I like the boost::multi_array class http://www.boost.org/doc/libs/1_45_0/libs/multi_array/doc/index.html. However, I understand that it is often a long step straight into boost as a beginner.

Other than that, I think that program works. [I can recommend putting the "\n" into the second loop of the GridFunction() and adding a space into the first. It does make it easier to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.