Function in a function?

Hey there,

I'm working on a school project and I'm having a little problem calling the functions I need into other functions (at least I think that's what I'm trying to do). Here is the pseudo code I'm trying to replicate:


For EL=1 to ...

1) K(1,1)=.... etc.

2) NP(1)=2*JB-1
NP(2)=2*JB
NP(3)=2*JE-1
NP(4)=2*JE

3) For M=1 to 4
For N=1 to 4
S[NP(M), NP(N)]= S[NP(M), NP(N)] + K(M,N)

Next N
Next M
Next EL

So basically I'm trying to insert the values calculated in the NP(1 to 4) calculations into the S matrix. Is there an easy way of doing this?

Thanks in advance!
You are perfectly fine calling one function from another (this is actually what about 80% of the code you write is doing one way or another anyway) as long as the function you are calling was previously declared or defined some where "above" the call to it.
On this forum you are easier to help if you:

1) ask short and clear questions (so that people can quickly give explanations and links to the documentation most relevant to you), and

2) have some C++ source code to show (so that people can see and correct your mistakes if you have any).

What problems are you having exactly?
If do you not know the C++ language well enough, I suggest you read through the tutorial here:

http://www.cplusplus.com/doc/tutorial/

Otherwise, please simplify and clarify the question, and give your C++ source code which you need help with.
I'm not really explaining myself properly, I'm not necessarily dealing with functions.

I want to know how I can insert the value I get for each NP(1-4) into my S matrix. For example, for the first iteration of EL, the values I get for the NPs are 1, 2, 5, 6. The S matrix should be of size 12x12 when every iteration is complete. The NP values only represent what rows and columns are filled in the S matrix at each iteration by the K(M,N). I have tried referring NP to an "index" variable then calling the index variable in my S matrix but that does not work. Here is the code I have so far.


void member_stiffness(double KL[MAXD][MAXD], double SS[MAXD][MAXD], double L, int& EL, double EM[MAXD], double CP[MAXD], int& NM, double MPRP[MAXD][2], double COORD[MAXD][MAXD], double C, double S)
{
    int index, index2;
    int i=0;
    int j=0;
    int x=0;
    int y=0;
    int JB=0;
    int JE=0;
    double M1[MAXD][MAXD]={0};
    double M2[MAXD][MAXD]={0};
    double NP[MAXD];
    double EAL=0;

    for(EL=1;EL<=NM;EL++)
    {
        JB=MPRP[EL][1];
        JE=MPRP[EL][2];

        L=sqrt((COORD[JE][1]-COORD[JB][1])*(COORD[JE][1]-COORD[JB][1])+(COORD[JE][2]-COORD[JB][2])*(COORD[JE][2]-COORD[JB][2]));
        C=(COORD[JE][1]-COORD[JB][1])/L;
        S=(COORD[JE][2]-COORD[JB][2])/L;

        EAL=EM[1]*CP[1]/L;


        for(int x = 0; x < 4; x++){
            for(int y = 0; y < 4; y++){
                KL[x][y] = 0;
            }
        }


        for(int x = 0; x < 4; x = x + 2){
            for(int y = 0; y < 4 ; y = y + 2){

                if((x == 0 && y == 0) || (x == 2 && y == 2 )){
                    KL[x][y] = C*C*EAL;
                    KL[x+1][y] = C*S*EAL;
                    KL[x][y+1] = C*S*EAL;
                    KL[x+1][y+1] = S*S*EAL;
                }

                else{
                    KL[x][y] = C*C*-EAL;
                    KL[x+1][y] = C*S*-EAL;
                    KL[x][y+1] = C*S*-EAL;
                    KL[x+1][y+1] = S*S*-EAL;
                }

            }


        }


        NP[1]=2*JB-1;
        NP[2]=2*JB;
        NP[3]=2*JE-1;
        NP[4]=2*JE;

        index=NP[x];
        index2=NP[y];

        for(x=1; x<=4; x++)
        {
            for(y=1; y<=4; y++)
            {
                SS[index][index2] = SS[index][index2]+KL[index][index2];
            }
        }


    }

}


For the most part, the rest of the code consists of reading values from a .txt file.
Well, copy-pasting your code which is problematic:

1
2
3
4
5
6
7
8
9
10
        index=NP[x];
        index2=NP[y];

        for(x=1; x<=4; x++)
        {
            for(y=1; y<=4; y++)
            {
                SS[index][index2] = SS[index][index2]+KL[index][index2];
            }
        }


In the code above there are a total of 16 steps due to the nested for() loops, however you never update index and index2.

So basically you keep modifying the SS matrix at SS[index][index2] sixteen times.

Suggestions (not knowing exactly what your code is supposed to do):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        index=NP[x];
        index2=NP[y];

        for(x=1; x<=4; x++)
        {
            for(y=1; y<=4; y++)
            {
                // SS[x][y] = SS[x][y]+KL[x][y]; // or maybe...
                // SS[NP[x]][NP[y]] = SS[NP[x]][NP[y]]+KL[NP[x]][NP[y]];

                // simplification: x=x+y same as x+=y
                // SS[NP[x]][NP[y]] += KL[NP[x]][NP[y]];
            }
        }

Last edited on
I have tried the second line of what you've proposed before, it was the first thing I tried but I got an error when I did.

error: invalid types 'double (*)[100][double]' for array subscript


I get this error on the SS equation line:

SS[NP[x]][NP[y]] = SS[NP[x]][NP[y]] + KL[x][y];
Just a small observation I forgot to make:
Be aware that in C and C++ counting starts from 0 and not 1.
This is the same for all kinds of arrays, including multidimensional ones (matrices).

1
2
3
4
5
6
7
int arr[5];

// available, five elements
a[0] = a[1] = a[2] = a[3] = a[4] = 0;

// not available!
// a[5] 


I have tried the second line of what you've proposed before, it was the first thing I tried but I got an error when I did.

The NP array stores floating point values (of type double) while indexes need to be natural numbers (unsigned integers, preferably of type size_t).

In order to not uglify the code too much, let's use index and index2 like you originally intended:

1
2
3
4
5
6
7
8
9
10
        for(x=1; x<=4; x++)
        {
            for(y=1; y<=4; y++)
            {
                index = NP[x]; // automatic conversion from double to int
                index2 = NP[y]; // same as above

                SS[index][index2] = SS[index][index2] + KL[x][y];
            }
        }
Last edited on
Wait, are you trying to calculate tension on a 2D spring system?
That helps a lot thank you!

Doesn't quite give me the answers that it should but it's a start! Thanks again.

Cheers
Topic archived. No new replies allowed.