In: Computer Science
Write a complete program (possible to compile) that:
1) declares two local integers x and y
2) defines a global structure containing two pointers (xptr,yptr)
and an integer (z)
3) Declares a variable (mst) by the type of previous
structure
4) requests the values of x and y from the user using only one
scanf statement
5) sets the first pointer in the struct to point to x
6) sets the second pointer in the struct to point to y
7) uses the structure to print out the values in x and y (do not
print x and y values directly)
8) Declare a variable (mstptr) that is a pointer to the structure and make it point to the structure you already declared (mst)
9) Using the pointer to the structure set the integer z in thestructure to the number 5.
10) Using the pointer to the structure display the value of the integer and the values pointed to by the two pointers (must use -> operator)
C++ Coding Please and if you could put comments down that would be amazing!
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
#include<iostream>
using namespace std;
struct Point{
int *xptr;
int *yptr;
int z;
};
int main()
{
//declare x and y
int x,y;
//object of structure
struct Point mst;
//ask for the input
printf("Enter value of x and y : ");
scanf("%d%d",&x,&y);
//set x and y to pointer
mst.xptr = &x;
mst.yptr = &y;
//print x and y using mst pointer
printf("Printing the value using mst
pointer\n");
printf(" x = %d\n",*mst.xptr);
printf(" y = %d\n",*mst.yptr);
//define pointer to structure
struct Point *mstptr=&mst;
//set value to z
mstptr->z=5;
printf("\nPrinting the value using structure
pointer\n");
//print x and y and z using structure point
printf(" x = %d\n",*mstptr->xptr);
printf(" y = %d\n",*mstptr->yptr);
printf(" z = %d\n",mstptr->z);
return 0;
}