Hello,

I'm trying to convert some code from BASIC to C++.. But, I'm stuck on a particular part..

REM CREATE A MATRIX C WITH (COS(DN) , -SIN(DN)) :
DIM C(NE, NS, 2)
FOR N = 0 TO NE / 2
 FOR E = 0 TO NE - 1
  DC = E / FE
  FEN = N * FE / NE
  DN = 2 * PY * FEN * DC
  C(E, N, 1) = COS(DN)
  C(E, N, 2) = -SIN(DN)
 NEXT E
NEXT N

Basically, I don't get the:

DIM C(NE, NS, 2)

Is this a vector with the size of NE, NS (Which is inputted)?

I also don't get this part:

 C(E, N, 1) = COS(DN)
 C(E, N, 2) = -SIN(DN)

Since if C was a vector, I wouldn't be able to push back to different data sets into the same vector.. I don't get the 1/2 differenciation. Also, if cos and sin are in C++, is there a -sin() in the math library?

Here is my code so far:

#include <iostream>
#include <math.h>
#include <vector>

using namespace std;
int main(int argc, char *argv[]) {

    const double PY = 3.14159265;
    double FE = 0;
    double NE = 0;

    cout << "Sampling rate" << endl;
    cin >> FE;

    cout << "Amount of Measures" << endl;
    cin >> NE;

    double NS = NE / 2 + 1; // Amount of Sunusoids 

    vector<double> C (FE*NE);

    for(int n=0; (n < NE/2); n++)
    {
        for(int e=0; (e < NE-1); e++)
        {
            double DC = e / FE;
            double FEN = n*FE/NE;
            double DN = 2*PY*FEN*DC;
            //C.push_back(cos(DN));
        }
    }

}

Hoping someone can help me :)

C is a 2-dimensional vector. In c++ you can represent it like this:
vector<vector<NS>> C;
C.resize(NE);

There are other ways to do it as well, such as this boost library

Or you could just use a simple array such as int C[NE][NS]; assuming NE and NS are constants, or you are using a compiler that supports the new c++11 standards.

NS and NE can be either input using cin, read from a file, or just int constants.

Also, if cos and sin are in C++, is there a -sin() in the math library?

Yes, sin() and cos() are part of the standard math library. Look in <cmath> or math.h for others.

Heyy thanks for your help, it worked! :)

Another part though:

FOR N = 0 TO NE / 2
 R(N, 1) = 0
 R(N, 2) = 0
 FOR E = 0 TO NE - 1
  A = A(E) * B(N) * C(E, N, 1)
  B = A(E) * B(N) * C(E, N, 2)
  R(N, 1) = R(N, 1) + A
  R(N, 2) = R(N, 2) + B
 NEXT E
NEXT N

-- Converted so far in C++

 vector<matrix>R(NS);
 vector<matrix>A(NE);
    for(int N=0; (N < NE/2); N++)
        {
            R[0][N] = 0;
            R[1][N] = 0;

            for(int e=0; (e < N1-1); e++)
            {

            }
        }

I don't get this bit:

A = A(E) * B(N) * C(E, N, 1)
B = A(E) * B(N) * C(E, N, 2)

Do I just push_back into A? So for example:

A.push_back(A(E) * B(N) * C(E, N, 1));
B.push_back(A(E) * B(N) * C(E, N, 2));

Thanks for your help again :)

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.