// In this code, show using 'structs' how to group related variables together #include using namespace std; int main() { //Suppose we are writing a program involving particles, and that for each particle, //we will are interested in its mass and momentum // we could create a variable for each of these quantities, like this: // double mass_1; // double mass_2; // double mass_3; // double momentum_1; // double momentum_2; // double momentum_3; // But this can get quite tediuos/complex //-------------------------------------------------------------------------------------- //Solution (in C++) -- struct -- a data structure that can link together related variables //Define a struct which contains two variables: mass and momentum struct particle { double mass; double p; }; // note the semicolon after the closing brace of the struct here // this is important! -- if omitted, it can cause compilation issues // now that we have defined this 'particle' struct, we can use it just like the basic data types // you learned about earlier like (int, double)! // create an object which is an instance of this struct type: particle p1; // Can assign the values of the variables for this object, using the syntax: // object.variable = value; p1.mass = 0.938; //GeV (proton mass) p1.p = 0.0; // anytime we want to access the variables within the struct, // we do it by writing object.variable cout << "Particle 1: mass = " << p1.mass; cout << "; momentum = " << p1.p <