SEMF physical C ++ code

Need help solving one minor issue with this program. I created a SEMF in physics for C ++ to calculate the formula everything is fine, but my BE is a formula with a5 in it.

He says about the error: The identifier "a5" is undefined, and I know what it means, but how can I get the a5 that needs to be taken from the select statement, if I entered even even or even odd or odd values ​​for the AZ values.

#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

int main()
{
    int A, Z;

    // Main body of Semi-Empirical Mass Formula
    cout <<"Enter the mass number A: ";
    cin >> A;
    cout <<"\n";
    cout <<"Enter the atomic number Z: ";
    cin >> Z;
    cout <<"\n";

    // Constants from formula, units in MeV(millions of electron volts)
    double a1 = 15.67;          
    double a2 = 17.23;
    double a3 = 0.75;
    double a4 = 93.2;

    if(Z % 2 == 0 && (A - Z) % 2 == 0)

        double a5 = 12.0;

    else if(Z % 2 != 0 && (A - Z) % 2 != 0)

        double a5 = -12.0;

    else

        double a5 = 0;


    // Formula for to compute the binding energy
    double B =a1 * A - a2 * pow( A, 2/3) - a3 * (pow(Z, 2) / pow(A, 1/3)) - a4 * (pow(A - 2 * Z, 2) / A) + (a5 / pow(A, 1/2));

    // Formula for to compute the binding energy per nucleon
    double B_E = B / A;


    return 0;

}

      

+3


source to share


4 answers


Put definition

double a5 = 0.0;

      



just below the definition of a4 and use it in each of your if cases.

+2


source


a5 is undefined due to scope issue.

Since you are declaring a5 in the clauses of the if-else statement, the declaration is only scoped in the statement that is being declared.



To fix this problem, declare a5 in a location where it is scoped to later statements where you use a5:

double a1 = 15.67;          
double a2 = 17.23;
double a3 = 0.75;
double a4 = 93.2;
double a5 = 0.0;
//^ declare a5 here, it will be in scope when used in subsequent statements past the else clause


if(Z % 2 == 0 && (A - Z) % 2 == 0)

    a5 = 12.0;

else if(Z % 2 != 0 && (A - Z) % 2 != 0)

    a5 = -12.0;


// Formula for to compute the binding energy
double B =a1 * A - a2 * pow( A, 2/3) - a3 * (pow(Z, 2) / pow(A, 1/3)) - a4 * (pow(A - 2 * Z, 2) / A) + (a5 / pow(A, 1/2));

      

+6


source


You need to declare a5

outside of your if statements and then set it in if statements

double a5 = 0;

if(Z % 2 == 0 && (A - Z) % 2 == 0)
    a5 = 12.0;
else if(Z % 2 != 0 && (A - Z) % 2 != 0)
    a5 = -12.0;

      

As you are now, the variable a5

will only exist inside the if statement in which it is declared.

+3


source


Move the ad a bit a5

:

double a5;
if(Z % 2 == 0 && (A - Z) % 2 == 0)
    a5 = 12.0;
else if(Z % 2 != 0 && (A - Z) % 2 != 0)
    a5 = -12.0;
else
    a5 = 0;

      

+2


source







All Articles