c++ assignment, answer the following questions 1) Create a struct Point2D that r
ID: 3835492 • Letter: C
Question
c++ assignment, answer the following questions
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) Create a function named dist which takes two Point2D points as parameters and returns the distance between the two points.
4).Write a function named moveTo that takes two Point2D points and moves the first to the position of the second. The function returns nothing. Call your function from the main and verify by calling printLocation on the first point that it has indeed moved.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct Point2D
{
double x;
double y;
};
struct Student
{
string name;
string school;
int sid;
char grade;
};
double dist(struct Point2D p1, struct Point2D p2)
{
double dist = (p1.x -p2.x)*(p1.x -p2.x) + (p1.y -p2.y)*(p1.y -p2.y);
return sqrt(dist);
}
void moveTo(struct Point2D *p1, struct Point2D p2)
{
p1->x = p2.x;
p1->y = p2.y;
}
int main()
{
struct Point2D p1;
p1.x = 4; p1.y = 6;
struct Point2D p2;
p2.x = 0; p2.y = 0;
struct Point2D p3;
p3.x = -4; p3.y = -6;
cout << "Point 1 at location : x = " << p1.x << " y = " << p1.y << endl;
cout << "Point 2 at location : x = " << p2.x << " y = " << p2.y << endl;
cout << "Point 3 at location : x = " << p3.x << " y = " << p3.y << endl;
struct Student s1, s2;
s1.name ="name1";
s1.school = "school1";
s1.sid = 1;
s1.grade = '-';
s2.name ="name2";
s2.school = "school1";
s2.sid = 2;
s2.grade = '-';
cout << "Stduent sid = " << s1.sid << ", school = " << s1.school << ", name = " << s1.name << ", grade = " << s1.grade << endl;
cout << "Stduent sid = " << s2.sid << ", school = " << s2.school << ", name = " << s2.name << ", grade = " << s2.grade << endl;
s1.grade = 'A';
s2.grade = 'A';
cout << "Stduent sid = " << s1.sid << ", school = " << s1.school << ", name = " << s1.name << ", grade = " << s1.grade << endl;
cout << "Stduent sid = " << s2.sid << ", school = " << s2.school << ", name = " << s2.name << ", grade = " << s2.grade << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.