修改结构数组的值
Modifying the values of an array of strucutres
我目前正在尝试用 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;
现场演示。
相关文章:
- 从函数中全局删除并重新实例化数组结构,而无需在编译时知道数组的大小
- 如何使用函数的输出初始化 const 数组结构字段?
- 传递数组结构、ofstream 和 interger 以运行
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 将文本文件读取到数组结构中
- C/C++中数组结构和数组结构的通用接口
- C++ MDC final-在字符类型的数组结构中按字母顺序对记录中的名称进行排序
- C++ 使用数组结构创建平衡的二叉搜索树
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- 使用 vector 在 c++ 中声明 3D 数组结构
- 数组结构无法正确打印
- 如何在C++中访问数组结构内部的数组结构
- CIN进入数组结构似乎什么也没输入
- ifstream将数组结构到txt文件中,然后尽可能将其提取为数组
- wlanapi-将WlanFreeMemory释放其WLAN_INTERFACE_INFO数组结构
- 显示数组结构 c++
- C++ 使用函数访问数组结构的方法是什么?
- 将庞大的数组结构复制到 GPU
- 如何封送包含字符矩阵的数组结构
- C++多维数组结构的对齐