So I've been trying to figure out dynamic arrays for a Quantum Mechanics project I've been working on and am currently having the issue that when I try to have a value called f_theta computed by my program stored into the array I get an output that is not the value computed.
In order to try and diagnose the problem I commented out various parts of the code that deals with the array and I have determined that the problem comes from line 76. When that piece (the actual part that is supposed to store the f_theta into the array) is commented out the array output are all 0 which is what I would expect since I zeroed all the elements in the array prior to that. but when that line of code is in the program I get the output:
"The array stored: -2147483648"
This is clearly not correct as each value that is supposed to be stored in the array is on the order of 10^24.
#include <iostream>
#include <ctime>
#include<cmath>
#include <new>
usingnamespace std;
#define m1 10 //wimp mass in GeV/c^2
#define m2 73 // germanium nucleus mass in GeV/c^2
#define u 91 // z boson mass in GeV/c^2 this is the mediating particle
#define g 2.00976e-22 // coupling constant in GeV*m
#define h 6.58211899e-25 // plancks constant h-bar in GeV*s
int main ()
{
//number of iterations for algorithim or "data points" in the simulated experiment
//n_iter is user defined, i and k are indexes for the computer
longint n_iter, i, k;
//p_initial is momentum of neutralino
float p_initial;
//q is momentum transfer to germanium nucleus
//E_observed is a calculated quantity for the "observed" phonon energy in a germanium detector
// which will be taken to be related to the magnitude of q by E= q^2/2m where m is the mass of germanium nucleus
//scale is the random float from 0..1 that defines the observed energy. note |q|<= |p_initial|
//f_theta is the calculated scattering amplitude
double scale,E_observed,q,f_theta,E_neutralino;
//Here we will declare f_array which will contain a list of scattering amplitudes (uses pointer?)
longint *f_array;
//user sets number of calculations of scattering amplitude
cout<<"How many data points would you like to use to calculate the scattering amplitude? (n<=1000) \n";
cin>>n_iter;
//user defines an incident momentum
cout << "What is the incident momentum of the neutralino? \n";
cin>> p_initial;
//resize array to match user data set input for n_iter (number of data points that the program will compute)
f_array= new () longint[n_iter];
//zeroes all elements in the array
for(k=0;k<n_iter;k++)
f_array[k]=0;
//seed random number generator
srand(time(NULL));
for(i=0; i<n_iter; i++)
{
//creates a random float between 0..1 for every trail
scale=float(rand())/float(RAND_MAX);
cout<<"scale is: " << scale << "\n";
//calculates energy of neutralino based on user input for momentum
E_neutralino=((p_initial*p_initial)/(2*m1));
//creates an observed phonon energy based on the energy of the neutralino, Note E_observed<=E_neutralino
E_observed=(scale*(E_neutralino));
cout<<"The observed energy: " << E_observed<< " GeV/c^2 \n";
//calculate momentum transfer to Germanium
q=sqrt((2*m2*E_observed));
cout<<"q is: " << q << " GeV/c \n";
//calculate scattering amplitude formula derived on poster or in paper
f_theta=((2*m1*g)/((h*h)*(((u*u)+(q*q)))));
cout << "The scattering amplitude was calculated to be:" << f_theta<<" \n";
//stores array values for each computed f_theta
f_array[i]=f_theta;
cout<<"The array stored:" << f_array[i]<<"\n";
}
//calculate the differential cross section
}
Hi i had a look at your code, whats happening is that you are trying to assign f_theta (which is a floating point number) to f_array[INDEX] which is an integer. if you change the type of f_array to a float or a double this should work fine
ok, so even though the index is an int type i still have to declare float type for the array? I did what you suggested and it works (thank you!) but I want to know for future reference.
actually if you want to use dynamic arrays that you can resize on the fly consider using <vector>.
e.g. vector<double> f_array;
there's plenty of info on their usage on the net.