Finding the area of a rectangle using pass by reference?

I got a homework assignment a couple of days ago.

We received the code below, and were asked to complete each function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;




int main()
{
   double length=0.0,    // The rectangle's length
          width=0.0,     // The rectangle's width
          area=0.0;      // The rectangle's area
          
   // Get the rectangle's length and rectangle's width..
    getInput(length, width);
    
   
   // Get the rectangle's area.
   area = getArea(length, width);
   
   // Display the rectangle's length, width, and area.
   displayData(length, width, area);
          
   return 0;
}



This is what I've done so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;




int main()
{
   double length=0.0,    
          width=0.0,     
          area=0.0;      
          

    getInput(length, width);
    area = getArea(length, width);
    displayData(length, width, area);
          
   return 0;
}

void getInput (double length, double width)
{
	cout << "Please enter the length and the width of the rectangle. " << endl;





}

double getArea (double length, double width)
{
	area = length * width;
	return area;
}

void displayData (double area)
{
	cout << "The length of the rectangle is " << length << endl;
	cout << "The width of the rectangle is " << width << endl;
	cout << "The area of the rectangle is " << area << end;
}



I'm pretty sure my professor wants us to pass the values by reference in the getInput function, but that subject is a quite unclear to me, and I'm not even sure how to begin setting that up.



Thank you.
you pass by reference by doing data_type& variable_name this in a way transfers the actual variable instead of the value of the variable. so you are moving the actual variable. and if you are doing
1
2
3
4
5
double getArea (double length, double width)
{
	area = length * width;
	return area;
}

the display data should have the last line as cout << "The area of the rectangle is " << getArea() << endl;
Topic archived. No new replies allowed.