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

EXERCISES FOR C++ 1) Declare a pointer to an integer. Declare an integer say mon

ID: 3912609 • Letter: E

Question

EXERCISES FOR C++

1) Declare a pointer to an integer. Declare an integer say month. Use the pointer to change month to 1 then output the result. You must use the pointer for all of your code. Also properly initialize the pointer.

2) Use the Point class to declare 2 Point objects (could be of any x,y) and 2 pointers to those objects. Declare a double variable to represent the distance between the two points. Compute the distance using the pointers. Hint: distance = sqrt ( (x2-x1)2 + (y2-y1)2 ). Must use the pointers in your calculation.

3) Assuming the following fragment of code:

int      day = 21;

int      month = 9;

int * p1 = NULL;

int * p2 = NULL;

int * p3 = NULL;

p1 = &day;

p2 = &month;

p3 = p1;

p1 = p2;

p3 = p2;

p1 = NULL;

Draw diagram of day, month, p1, p2, and p3 similar to the diagram shown in section "Pointers to double" in sub-module "Pointer to primitive data types".

The purpose is to show the changes of the pointers after each statement is executed.

Explanation / Answer

(1)

#include<iostream>

using namespace std;

int main(){

int *ptr;

int month=5;

ptr=&month;

*ptr=1;

cout<<"After change value of integer month: "<<month<<" ";

return 0;

}

(2)

#include<iostream>

#include<math.h>

using namespace std;

class point {

public :

int x,y;

};

int main(){

double distance;

point *ob1,*ob2;

ob1=new point;

ob2=new point;

cout<<"enter x and y coordinate of point 1 : ";

cin>>ob1->x>>ob1->y;

cout<<"enter x and y coordinate of point 2 : ";

cin>>ob2->x>>ob2->y;

int value=((ob2->x - ob1->x)*(ob2->x - ob1->x))+((ob2->y - ob1->y)*(ob2->y - ob1->y));

distance=sqrt(value);

cout<<"distance between two points is: "<<distance<<" ";

return 0;

}

(3)

#include<iostream>

using namespace std;

int main(){

int day = 21;

int month = 9;

int * p1 = NULL;

int * p2 = NULL;

int * p3 = NULL;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

p1 = &day;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

p2 = &month;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

p3 = p1;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

p1 = p2;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

p3 = p2;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

p1 = NULL;

cout<<"Day : "<<day<<" Month : "<<month<<" pointer1 : "<<p1<<" pointer2 : "<<p2<<" pointer3 : "<<p3<<" ";

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote