Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ 1) Create a struct Point2D that represents a point in a 2 dimensional plane.

ID: 3857383 • Letter: C

Question

C++

1) Create a struct Point2D that represents a point in a 2 dimensional plane. It should have two fields x and y.
Create a few points at different locations. Then print their locations.

2) Create a struct Student that represents a student. It should have a name, a school, a student id and a grade field. Create a few different students all with grade '-'. Then give them all 'A's.

3) Explain briefly and clearly how using structs is better than just using regular primitive variables to represent the data like we have until this point.

4) Create a function named dist which takes two Point2D points as parameters and returns the distance between the two points.

Explanation / Answer

1
struct Point2D {
   double x;
   double y;
};

void fun() {
   Point2D p1 = new Point2D;
   p1->x = 1;
   p1->y = 2;
   Point2D p2 = new Point2D;
   p2->x = 2;
   p2->y = 2;
   Point2D p3 = new Point2D;
   p3->x = 4;
   p3->y = 4;

   cout << "x= " << p1->x << "y= " << p1->y << endl;
   cout << "x= " << p2->x << "y= " << p2->y << endl;
   cout << "x= " << p3->x << "y= " << p3->y << endl;
}


2
struct Student {
   string name;
   string school;
   string studentid;
   char grade;
}


void fun() {
   Student s1 = new Student;
   s1-> name = "xyz";
   s1-> school = "abc";
   s1-> studentid = "21-11-0578";
   s1-> grade = '-';

   Student s2 = new Student;
   s2-> name = "pqr";
   s2-> school = "alpha";
   s2-> studentid = "516";
   s2-> grade = '-';


   s1-> grade = 'A';
   s2-> grade = 'A';
  
  
}


3.
If we didn't use structure, we haf to maintian two different data item x and y. If we had to maintain more than 1 points maintaing different pair of x and y becomes tough for developer.

4.
double dist(Point2D a, Point2D b) {
   double d = sqrt( pow (a->x - b->x, 2) + pow (a->y - b->y, 2))
   return d;
}

For any query, please post in the comment section