2014-11-22 94 views
-2

我目前正在嘗試在C++中建模彈性碰撞。我有一個叫做粒子的結構,我用它來描述對象。修改一個結構數組的值

//Defining the particle structure 
struct particle { 
    double x; // Position 
    double p; // Momentum 
    double im; // Inverse Mass 
    double v; // Velocity 
    double T; // Kinetic Energy 
    double a; // Radius of a Particle 
}; 

我使用這些結構創建了一個數組。

particle AP[n+2]; 

我的程序的一部分需要使用時間變量和它們的速度增加每個結構的位置。

我使用這個代碼

void LeapForward(struct particle AP[],double tfc,int n) 
{ 
    for(int cycle=0; cycle<n+2; cycle++) 
    { 
     double second; 
     second=AP[cycle].x+tfc*AP[cycle].v; 
     AP[cycle].x=second; 
    } 
} 

然而,看來正在修改的陣列心不是而是重新創建。我想知道是否有人知道這是爲什麼,如果是的話如何解決這個問題。

我的完整代碼可以在下面找到。任何幫助將不勝感激。

//This program will be about particle collsions 
#include <iostream> 
#include <cmath> 
#include <fstream> 
using namespace std; 

//Function for collision of particles 
void Collision(struct particle AP[],int chosen); 
void Assignment(struct particle AP[],int n); 
void LeapForward(struct particle AP[], double tfc, int n); 
void TimeForCollision(struct particle AP[],double tfc, int chosen, int n); 

//Defining the particle structure 
struct particle { 
    double x; // Position 
    double p; // Momentum 
    double im; // Inverse Mass 
    double v; // Velocity 
    double T; // Kinetic Energy 
    double a; // Radius of a Particle 
}; 

//Variable Declaration 
int n; //Number of particles 
double tfc; //This is going to be the time to leap forward 
int chosen; //Which particles are to collide 

    //Main program 

    int main() 
    { 
    cout<<"How many particle do you want to model: "; 
    cin>>n; 
    particle AP[n+2];  

    //Assigning value to Particles 
    Assignment(AP,n);  

    cout<<endl<<"Two walls have been placed at x=0 and 20"<<endl; 
    //Assignment of Wall Data. 
    AP[0].im=0.000001,AP[n+1].im=0.000001; 
    AP[0].x=0,AP[n+1].v=0; 
    AP[0].x=0,AP[n+1].x=20; 
    AP[0].a=0,AP[n+1].a=0; 

    ofstream Port; 
    Port.open ("Positions.txt"); 
    for(int model=0; model<2;model++) 
     { 
    //Time for next collision 
    TimeForCollision(AP,tfc,chosen,n); 
    LeapForward(AP,tfc,n); 
    Collision(AP,chosen); 
    for(int plot=0; plot<n+2;plot++) 
     { 
     Port<<AP[plot].x<<"\t"; 
      } 
    Port<<endl; 
     } 
    } 


void Collision(struct particle AP[],int chosen) 
{ 
    double combinemass=1/AP[chosen].im+1/AP[chosen+1].im; 
    double result1=(AP[chosen].v*(1/(AP[chosen].im)-1/(AP[chosen+1].im))+(2*AP[chosen+1].v*1/(AP[chosen+1].im)))/combinemass; 
    double result2=(AP[chosen+1].v*(1/(AP[chosen+1].im)-1/(AP[chosen].im))+(2*AP[chosen].v*1/(AP[chosen].im)))/combinemass; 
    AP[chosen].v=result1; 
    AP[chosen+1].v=result2; 
} 

void Assignment(struct particle AP[],int n) 
{ 
for(int cycle=1;cycle<=n;cycle++) 
    { 
    cout<<"Data Input for particle number "<<cycle<<endl; 
    double mass; 
    cout<<"What is the position of the particle : "; 
    cin>>AP[cycle].x; 
    cout<<"What is the mass of the particle : "; 
    cin>>mass; 
    AP[cycle].im=1/mass; 
    cout<<"What is the velocity of the particle : "; 
    cin>>AP[cycle].v; 
    cout<<"What is the radius of the particle : "; 
    cin>>AP[cycle].a; 
    //Data calculation 
    AP[cycle].p=AP[cycle].v*mass; 
    AP[cycle].T=0.5*AP[cycle].v*mass*mass; 
    } 
} 

void LeapForward(struct particle AP[],double tfc,int n) 
{ 
    for(int cycle=0; cycle<n+2; cycle++) 
    { 
     double second; 
     second=AP[cycle].x+tfc*AP[cycle].v; 
     AP[cycle].x=second; 
    } 
} 

void TimeForCollision(struct particle AP[],double tfc, int chosen, int n) 
{ 
double balance=0; 
tfc=0; 
    for(int cycle=0;cycle<n+1;cycle++) 
    { 
     double placeh=abs(((AP[cycle+1].x-AP[cycle+1].a)-(AP[cycle].x+AP[cycle].a)))/(AP[cycle].v-AP[cycle+1].v); 
     if(placeh>tfc && placeh>0) 
     { 
     tfc=placeh; 
     chosen=cycle; 
     } 
    } 
    cout<<tfc<<endl; 
} 
+2

請不要將您的問題中的代碼更改爲基於您收到的答案而工作的版本。它使您的整個問題無效。 – 2014-11-22 18:35:59

+0

@CaptainObvlious你能證實我的回答是正確的嗎?我認爲這就是爲什麼他的功能沒有修改這些值! – Quest 2014-11-22 18:43:42

+0

對不起,我是新來的網站,並沒有意識到。我很抱歉。 – SiberianSloth 2014-11-22 18:55:36

回答

0

這是因爲當你打電話的功能,您將創建結構的新實例,並修改新創建的實例,這將在你的函數結束時被刪除。

傳遞值作爲這樣一個參考:

void LeapForward(struct particle *AP, double tfc, int n); 

另外,在你LeapForward功能,您不需要創建臨時的雙變量。 您可以使用類似:

AP[cycle].x += tfc * AP[cycle].v; 

Live demo

+0

@SiberianSloth Ups抱歉。我更新了我的代碼。它現在有效 – Quest 2014-11-22 18:32:39