Insert Design Layout References Mailings Review View Help VIEW Be careful files
ID: 3744377 • Letter: I
Question
Insert Design Layout References Mailings Review View Help VIEW Be careful files from the Internet can contain viruses. Unless you need to edit, it's safer to st finchhde cioetream frinclede cemats uing namespace atd This program reads a width and height of a rectangle. It computes and prints the area, perimeter and leagth of the diagonal of the rectangle Written by: Your name here Date written: Current date here void compute rectangle (domble width, double height); int main) double width, l width of the rectangle entered by the user height, Il height of the rectangle entered by the user area, llarea of the given rectangle perimeter, Il perimeter of the given rectangle diagonal;ll diagonal of the given rectangle cout > width; cout height; compute rectangle (width, height); coutExplanation / Answer
Solution for
[1] Since the values of height and width are passed by value to the function compute_rectangle, the scope of variables are not present in it. And thus, the values get deleted inside the function itself. To avoid this, pass the values by reference.
[2] On running the program as per the changes requested, the results obtained are not what were expected. That is because of the fact that while assigning multiple values, only the last value gets assigned and rest all the values are ignored.
[3] If the area, perimeter and diagonal variables are added as parameter to the function with pass by reference properties, it should look like this:
void compute_rectangle(double &width,
double &height,
double &area,
double &perimeter,
double &diagonal)
{
area = width + height;
perimeter = width * 2 + height * 2;
diagonal = sqrt(width * width - height * height);
}
This will calculate all the properties.
[4] The parameter height and width are not required to be passed as references, rather can be passed as normal values. That is because they do not require any kind of updation.
[5] Even if the name of the parameter inside a method is changed, it still has the same reference that was being passed to it from the caller function. Thus, naming of parameter doesn't affect the logic of the program.