修改结构数组的值

Modifying the values of an array of strucutres

本文关键字:数组 结构 修改      更新时间:2023-10-16

我目前正在尝试用 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;
}

这是因为当您调用函数时,您将创建结构的新实例并修改该新创建的实例,该实例将在函数结束时删除。

将值作为引用传递,如下所示:

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

此外,在 LeapForward 函数中,您无需创建临时双精度变量。您可以使用以下内容:

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

现场演示。