Practice

Vector



What is the exact output of the following program?  Show your intermediate work.


 #include <iostream>
 using namespace std;

 struct Vector {
     int x;
     int y;
     void display() const {
         cout << '{' << x << ',' << y << '}' << endl;
     }
 };

 struct Set {
     Vector* v;
     int n;
 public:
     void ctor(int k, int* x, int* y) {
         n = k;
         v = new Vector[n];
         for (int i = 0; i < n; i++) {
             v[i].x = x[i];
             v[i].y = y[i];
         }
     }
     void display() const {
         for (int i = 0; i < n; i++)
             cout << '[' << v[i].x << ',' << v[i].y << "] ";
         cout << endl;
     }
     Vector sum() const {
         Vector c = {0, 0};
         for (int i = 0; i < n; i++) {
             c.x += v[i].x;
             c.y += v[i].y;
         }
         return c;
     }
     void dtor() {
         delete [] v;
         cout << n << endl;
     }
 };

 int main() {
     Set s;
     int x[] = {0, 1, 0, 6};
     int y[] = {2, 1, 2, 2};
     s.ctor(3, y, x);
     s.display();
     s.sum().display();
     s.dtor();
 }

Show your output here:









Show your rough work on a separate page.


   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site