Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > C++ Programming with NR

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2009, 08:37 AM
mazman2011 mazman2011 is offline
Registered User
 
Join Date: Jan 2009
Location: Newcastle upon Tyne
Posts: 1
Fill vector (Vec_IO_DP) with data

Hello,
i'm having a simple question which i couldn't answer with google or other web forums. I'm trying to use C++ with Numerical Recipes (2nd Ed.). I would like to implement the quicksort algorithm (NR::sort) which takes an Vector 'Vec_IO_DP &arr'. The question is:

How do i fill a vector with data and pass it to that function?

Vec_IO_DP arr() creates me an array but how do i fill it with data e.g. {2.3,45.,245.12,1.25,12.0}? I do not really understand this format.

Another problem i'm having is that nrutil_nr.h won't compile properly as i get an "expected ')' before '=' token" error in many lines of template <class T>.
Reply With Quote
  #2  
Old 01-16-2009, 10:38 AM
davekw7x davekw7x is offline
Registered User
 
Join Date: Jan 2008
Posts: 453
Quote:
Originally Posted by mazman2011 View Post
...e.g. {2.3,45.,245.12,1.25,12.0}? I do not really understand this format.
I suggest that you get a book or find an on-line C or C++ tutorial that describes arrays and their initialization. For example: Tutorial on Arrays

Quote:
Originally Posted by mazman2011
...How do i fill a vector with data and pass it to that function?
In C and C++ you can initialize an array by giving a comma-separated list of constants enclosed by braces. For example to declare an array of five doubles and to specify initial values:
Code:
/* Any C or C++ program */
int main()
{
    double v[5] = {3.2, 8.2, 5.5, 2.1, 3.0};/* Creates and initialized an array of five doubles */
.
.
.
You can even let the compiler count the number of values in your initializer list. The following has the exact same effect:
Code:
/* Any C or C++ program */V
int main()
{
    double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};/* Creates and initialized an array of five doubles */
.
.
.
Note that this notation is only permissible when declaring the array. In C and C++ you can never (that's never) copy all of the elements of an entire array with an assignment statement. So the following would not work.
Code:
    double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
    double v2[5];
    v2 = v; // Won't work. Never has worked. Never will work. Period.
The Numerical Recipes Vec_DP class is somewhat similar to the C++ std::vector class. (It's kind of like a vector of doubles with some helpful new stuff thrown in and with some other stuff thrown out.)

It is not possible to create a constructor for an NR::Vec_DP object (or for a std::vector) that uses the {} array initialization syntax. (That's not possible. Really. The language simply doesn't provide for such things.)

However, there is a constructor that allows a Vec_DP to be initialized by copying the contents of an array.

For example, if you want to declare a Vec_DP that has size equal to five and give it the values from the array, then you can do something like:

Code:
 Vec_DP vdp(v, 5); // You have to tell it how many values to use from the array
You can not copy an array into a Vec_DP (or any other C++ vector object) with an assignment statement. (I am trying to make a point here: In General, in C and C++, you can not copy the entire contents of an array to or from anything with an assignment statement that just uses the name of the array.)

So the following would not work:
Code:
    double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
    Vec_DP vdp2;
    vdp2 = v; // This will not work. Try it if you want to, but it won't work. Period.
You can, however copy one Numerical Recipes Vec_DP to another by a simple assignment statement. The size of the target is automatically adjusted to be the size of the source, and the elements are copied.

For example:
Code:
//
// Demonstration of initialization and copying of
// Numerical Vec_DP objects
//
// davekw7x
//
#include "nrtypes.h"

void printVecDP(Vec_I_DP & v);

int main()
{
    DP v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
    Vec_DP vdp(v, 5); // You have to tell it how to use from the array
    Vec_DP vdp2;      // No initializer, so it is "empty"
    cout << "Initially:" << endl;
    cout << "  vdp.size()  = " << vdp.size() << endl;
    cout << "  vdp2.size() = " << vdp2.size() << endl;
    cout << "    Here's vdp: " << endl;
    printVecDP(vdp);
    cout << "   Here's vdp2:" << endl;
    printVecDP(vdp2);

    vdp2 = vdp;
    cout << "After assignment:" << endl;
    cout << "  vdp.size()  = " << vdp.size() << endl;
    cout << "  vdp2.size() = " << vdp2.size() << endl;
    cout << "    Here's vdp: " << endl;
    printVecDP(vdp);
    cout << "    Here's vdp2:" << endl;
    printVecDP(vdp2);
    

    return 0;
}

void printVecDP (Vec_I_DP & v)
{
    if (v.size() == 0) {
        cout << "        In printVecDP: The vector is empty." << endl;
    }
    else {
        for (int i = 0; i < v.size(); i++) {
            cout << "        v[" << i << "] = " << v[i] << endl;
        }
    }
    cout << endl;
}
Output:

Code:
Initially:
  vdp.size()  = 5
  vdp2.size() = 0
    Here's vdp: 
        v[0] = 3.2
        v[1] = 8.2
        v[2] = 5.5
        v[3] = 2.1
        v[4] = 3

   Here's vdp2:
        In printVecDP: The vector is empty.

After assignment:
  vdp.size()  = 5
  vdp2.size() = 5
    Here's vdp: 
        v[0] = 3.2
        v[1] = 8.2
        v[2] = 5.5
        v[3] = 2.1
        v[4] = 3

    Here's vdp2:
        v[0] = 3.2
        v[1] = 8.2
        v[2] = 5.5
        v[3] = 2.1
        v[4] = 3

Quote:
Originally Posted by mazman2011
Another problem i'm having is that nrutil_nr.h won't compile ...
Tell us what compiler you are using and what version.

Tell us how you are compiling. (Command line? Integrated Development Environment? What?)

Regards,

Dave

Footnote: If you want to learn how to implement quicksort, you can write it yourself. If you just want to use quicksort, on Vec_DP objects, then it's already supplied:
Code:
//
// Demo of Vec_DP initialization and use of nr::sort
//
// davekw7x
#include "nr.h"

void printVecDP(Vec_I_DP & v);

int main()
{
    DP v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
    Vec_DP vdp(v, 5);
    cout << "Initially:" << endl;
    cout << "    vdp: " << endl;
    printVecDP(vdp);
    
    NR::sort(vdp);

    cout << "After NR::sort:" << endl;
    cout << "    vdp: " << endl;
    printVecDP(vdp);
    

    return 0;
}
void printVecDP (Vec_I_DP & v)
{
    if (v.size() == 0) {
        cout << "        In printVecDP: The vector is empty." << endl;
    }
    else {
        for (int i = 0; i < v.size(); i++) {
            cout << "        v[" << i << "] = " << v[i] << endl;
        }
    }
    cout << endl;
}
Output
Code:
Initially:
    vdp: 
        v[0] = 3.2
        v[1] = 8.2
        v[2] = 5.5
        v[3] = 2.1
        v[4] = 3

After NR::sort:
    vdp: 
        v[0] = 2.1
        v[1] = 3
        v[2] = 3.2
        v[3] = 5.5
        v[4] = 8.2

Last edited by davekw7x; 01-17-2009 at 09:53 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:02 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.