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:
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.
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.
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];
}
}