After executing the following program segments what is the r
Solution
Ans(2).
#include <iostream>
using namespace std;
int main()
{
int x=100,y=200,z[10]={1,2,3,4,5,6,7,8,9,10};
int *ip=z; //pointing to the z array i.e very first address or index of z array,hence ip pointing to z[0]
y=*ip; //y=*ip ,hence y also pointing to z[0]
*ip=z[8]; //content of ip=z[8],hence value stored at ip=9
ip=&x; //now ip stored the address of x
z[7]=*ip; //z[7]=content of the address stored at ip i.e value of x,hence z[7]=100
ip=&z[6]; //now ip stored the address of z[6]
*ip=1000; //content of the address stored at ip must be 1000,hence z[6]=1000
y=*(ip+1); //y =content of z[7] hence y=100;
cout<<x<<\" \"<<y<<\" \"<<z[0]<<\" \"<<z[6]<<\" \"<<z[7]<<\" \"<<z[8];
return 0;
}
x=100,y=100,z[0]=9,z[6]=1000,z[7]=100,z[8]=9
****OUTPUT*****
100 100 9 1000 100 9
****OUTPUT*****
**end of answer 2 ****
Ans(3 and 4).
#include <iostream>
#include <cmath>
using namespace std;
class point{
private:
float x;
float y;
public:
point();
//constructor 1
point(float a,float b){ //constructor 2
x=a; //i have difined the constructor
y=b;
}
void print(point p); // print a point //i have added the object as as argument
float midPoint(point p); //compute the midpoint of two points
float Distance(point p); //compute the distance between two points
void Line(point p,float m,float b); //compute the line formed from two points,y=mx+b
} ;
void point::print(point p){ //definition of the method print
cout<<p.x<<\" \"<<p.y<<\"\ \";
}
float point::Distance(point p){ //definition of the method Distance
float x_diff=p.x-0; //computing the distance by asuming the origion as a refrence point
float y_diff=p.y-0; //computing the distance by asuming the origion as a refrence point
return sqrt(x_diff*x_diff+y_diff*y_diff);
}
float point::midPoint(point p) //definition of the method midPoint
{
float midpoint=(p.x+p.y)/2; //computing the midpoint
cout<<\"Midpoint = \"<<midpoint<<\"\ \";
}
void point::Line(point p,float m,float b){ //definition of the method Line
cout<<\"line equation : \"<<p.y<<\"=\"<<m<<\"*\"<<p.x<<\"+\"<<b; //printing the equation of the line
}
int main()
{
point p(2,3);
p.print(p); //printing the point
float distance=p.Distance(p); //calling the distance function
cout<<\"distance = \"<<distance<<\"\ \"; //printing the distance of the point
p.midPoint(p); //printing the midpoint
p.Line(p,1.5,2); //printing the equation of the line
return 0;
}
//end of code
Note:i have defined the required method as well as tested the methods also by calling them from the main method.
*********OUTPUT*************
2 3
distance = 3.60555
Midpoint = 2.5
line equation : 3=1.5*2+2
***********OUTPUT**********
Please let me know in case of any doubt.
![After executing the following program segments, what is the result of cout? - int x = 100, y = 200, z [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Int * ip = z; - After executing the following program segments, what is the result of cout? - int x = 100, y = 200, z [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Int * ip = z; -](/WebImages/16/after-executing-the-following-program-segments-what-is-the-r-1025912-1761531159-0.webp)
![After executing the following program segments, what is the result of cout? - int x = 100, y = 200, z [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Int * ip = z; - After executing the following program segments, what is the result of cout? - int x = 100, y = 200, z [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Int * ip = z; -](/WebImages/16/after-executing-the-following-program-segments-what-is-the-r-1025912-1761531159-1.webp)