+1 (315) 557-6473 

Program To Expand Pointer in C Assignment Solution.


Instructions

Objective
Write a program to expand pointer in C language.

Requirements and Specifications

pointer related problems in C

pointer related problems in C 1

pointer related problems in C 2
pointer related problems in C 3
pointer related problems in C 4
pointer related problems in C 5
pointer related problems in C 6
pointer related problems in C 7

Source Code

#include

#include

#include

#include

using namespace std;

class Point {

 friend istream& operator>>(istream& is, Point& p) {

  is >> p._x;

  is >> p._y;

  return is;

 }

 friend ostream& operator<<(ostream &out, const Point& p) {

  out << p.getX() << " " << p.getY();

  return out;

 }

 public:

  Point() {

   _x = 0;

   _y = 0;

  }

  Point(int x, int y) {

   _x = x;

   _y = y;

  }

  ~Point() {}

  int getX() const {

   return _x;

  }

  int getY() const {

   return _y;

  }

 private:

  int _x;

  int _y;

};

class Set {

 public:

  void loadSet(string filename) {

   ifstream f(filename);

   string line;

   while(getline(f, line)) {

    Point p;

    stringstream s(line);

    s >> p;

    v.push_back(p);

   }

   f.close();

  }

  void showSet() {

   for (Point p : v) {

    cout << p << endl;

   }

  }

  Point centerOfMass() {

   int xSum = 0;

   int ySum = 0;

   for (Point p : v) {

    xSum += p.getX();

    ySum += p.getY();

   }

   Point p(xSum/v.size(), ySum/v.size());

   return p;

  }

 private:

  vector v;

};

int main() {

 Set set;

 set.loadSet("exam-data.txt");

 set.showSet();

 cout << "Center of mass:" << endl;

 Point com = set.centerOfMass();

 cout << com << endl;

 return 0;

}