I'm confused about data structs and classes

In my class, we went over this stuff briefly and now that it's time to apply it, I'm completely confused. The book hasn't helped much either.

1
2
3
4
5
6
7
8
9
10
11
MassData getMassData(T x[], T y[], T m[], int numDataPoints);

MassData getMassData(T x[], T y[], T m[], int numDataPoints){
  
  MassData massData;
  massData.cenMassX = 0;
  massData.cenMassY = 0;
  massData.totMass  = 0;
  
  return massData;
}


Here's the portion of my code that is relevant. Not only am I confused about finding the center of mass of the 13-sided irregular shape (given its coordinates and side lengths), but how I would output this is my main function.

How would I output something like:
Total Mass:
Center of Mass:


Obviously those statements use cout, but I don't know how to output the calculated values.
closed account (3TXyhbRD)
You want to output the members of a struct? Is that your question? If yes you could do it this way (C):
1
2
3
4
5
6
7
8
9
#include <cstdio>

int main()
{
    MassData massData;
    // initialization and calculus...
    printf("%d %d\n", massData.cenMassX, massData.cenMassY); // assuming those two members are int
    return 0;
}


Or the C++ way:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
    MassData massData;
    // initialization and calculus...
    cout<<massData.cenMassX<<" "<<massData.cenMassY<<endl;
    return 0;
}
Thanks. Now I have to figure out how to calculate the center of mass coordinates.
Does this look correct for finding the total mass?
Here's my output code:
cout << "Total Mass: " << massData.totMass << endl;

  int mass = 0;
  
  MassData massData;
  massData.cenMassX = 0;
  massData.cenMassY = 0;
  
  for(int i = 0; i <= numDataPoints; ++i){
    mass = m[i];
    massData.totMass += mass;
  }
  
  return massData;
}
closed account (3TXyhbRD)
Yep, looks good.
Topic archived. No new replies allowed.