0

I would like to have a class this:

class block {
public:
int NX,NY;

int A[][]; // I want the the dimension of A is from 1 to NX and 1 to NY, how do I do that?
};

actually, I already have the corresponding FORTRAN code, I want to have it in c++, I believe with proper use of constructor, I will be able to easily to create and copy object, instead of copy each components of the object like FORTRAN does. Please teach me how to do this! thanks!

module LinearSolution
    type LAE
        integer::N
        double precision,dimension(:,:),allocatable::A
        double precision,dimension(  :),allocatable::B
    contains
        procedure,nopass::RowReduction
    end type LAE
contains
    subroutine RowReduction
        double precision::C
        do k=1,N
            do i=k+1,N
                if(A(k,k)/=0) then
                    C=A(i,k)/A(k,k)
                    B(i)=B(i)-B(k)*C       !error: Statement Function is recursive
                    do j=k+1,N
                        A(i,j)=A(i,j)-A(k,j)*C   !error: Statement Function is recursive
                    end do
                end if
            end do
        end do

        do k=N,1,-1
            do i=k-1,1,-1
                if(A(k,k)/=0) then
                    C=A(i,k)/A(k,k)
                    B(i)=B(i)-B(k)*C  !error: Statement Function is recursive
                end if
            end do
        end do

        do k=1,N
            if(A(k,k)/=0) then
                B(k)=B(k)/A(k,k)  !error: Statement Function is recursive
            end if
        end do
    end subroutine RowReduction
end module LinearSolution

program TestLAE
    use LinearSolution  !fatal error: cant open module file LinearSolution.mod for reading
    type(LAE)::LAE1
    LAE1%N=3
    allocate(LAE1%B(1:N))
    allocate(LAE1%A(1:N,1:N))
    LAE1%B=(/1,1,1/)
    LAE1%A=(/2,0,0,0,2,0,0,0,2/)
    call LAE1%RowReduction
    print*, LAE1%B(1),LAE1%B(2),LAE1%B(3)
end program
northfly
  • 113
  • 3
  • 11
  • What does it mean for the dimension of A to be "from 1 to NX and 1 to NY"?? – juanchopanza May 02 '13 at 13:50
  • usually c++ allows index from 0 to the end, while I want to be able to define the index range myself, can I do that? – northfly May 07 '13 at 03:04
  • I think you're deviating from the original question, and as such, you should [post a separate question](http://stackoverflow.com/questions/ask). – Jesse May 07 '13 at 03:34

2 Answers2

1

Is constructor what you want?

class block {
public:
    int NX, NY;
    int **A;
    block(int nx, int ny){
        NX = nx;
        NY = ny;
        A = new int*[NX + 1];
        for(int x = 1; x <= NX; x++)
            A[x] = new int[NY + 1];
    }
    ~block(){
        for(int x = 1; x <= NX; x++)
            delete A[x];
        delete A;
    }
};
johnchen902
  • 9,531
  • 1
  • 27
  • 69
  • I think they downvoted you for dynamic allocation. I don't see why they should here, though. Just mention that he should delete[] it in destructor. – stardust May 02 '13 at 13:51
  • Well, you can't allocate 2D arrays like that. The second dimension would have to be known at compile time. – juanchopanza May 02 '13 at 13:57
  • @juanchopanza Sorry for my carelessness. Java makes me lazy. I changed the code and compiled it this time. – johnchen902 May 02 '13 at 14:04
0

What you ask for is not possible with standard C++. In C++ the size of any array should be indexed with a constant. Even using the following syntax will give you an error.

const int NX=10;
const int NY=10;
int A[NX][NY];

One possibility is,

#define NX 10
#define NY 10
int A[NX][NY]; 
Deepu
  • 7,592
  • 4
  • 25
  • 47
  • Then how could I do this? use vector? How to do that? using vector will be too clumsy? and inefficient? – northfly May 07 '13 at 02:59