Question

Find the errors in the following program. You must use pass by reference. #include <iostream> using...

Find the errors in the following program. You must use pass by reference.

#include <iostream>

using namespace std;

void area2(int area, int length, int width = 0);

int main()
{
int length, width, area;

// for rectangle use two arguments
cout << "Enter length and width of a rectangle" << endl;
cin >> length >> width;

cout << "The area is " << area2(area, length, width) << endl;

// for squares use one argument
cout << "Enter side of a square" << endl;
cin >> length;

cout << "The area is " << area2(area, length) << endl;

return 0;
}

void area2(int area, int length, int width)
{
if ( width == 0 )
area = length * length;
else
area = length * width;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

void area2(int &area, int length, int width = 0);

int main()
{
int length, width, area;

// for rectangle use two arguments
cout << "Enter length and width of a rectangle" << endl;
cin >> length >> width;
area2(area, length, width);
cout << "The area is " << area << endl;

// for squares use one argument
cout << "Enter side of a square" << endl;
cin >> length;
area2(area, length);
cout << "The area is " << area << endl;

return 0;
}

void area2(int &area, int length, int width)
{
if ( width == 0 )
area = length * length;
else
area = length * width;
}

Add a comment
Know the answer?
Add Answer to:
Find the errors in the following program. You must use pass by reference. #include <iostream> using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length,...

    Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...

  • Find the five problems with this program. Do not change the functionality or rewrite the program....

    Find the five problems with this program. Do not change the functionality or rewrite the program. #include <iostream> void multiply(int x = 1, int y); int main() { int length, width, area; cout << "Enter length and width of a rectangle "; cin >> length, width; area = multiply(length, width); cout << "The area is " << area << endl; area2 = multiply(width); cout << "The second area is " << area2 << endl; return 0; } void multiply(int x=...

  • Make the function take in the variables by reference #include <iostream> using namespace std; //Make the...

    Make the function take in the variables by reference #include <iostream> using namespace std; //Make the function take in the variables by reference void add_to_first_and_reset(int total, int to_add){    total += to_add; to_add = 0; } int main(){ int sum = 0, temp = 0;    for(int i = 0; i < 10; i++){ cin >> temp; add_to_first_and_reset(sum,temp); cout << sum << " " << temp << endl; }    }

  • #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

    #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...

  • Fix the code to print the "exact" result of the division of a/b (Pass by reference)...

    Fix the code to print the "exact" result of the division of a/b (Pass by reference) #include <iostream> using namespace std; void division (int& a, int& b, int& result) {    result = a/b;    } int main () { float a, b, result;    cin >> a; cin >> b;    division(a, b, result); cout << "result = " << result << endl; return 0; }

  • C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream>...

    C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream> using namespace std; int main() { //Part A int numA numA = 10; cout << numA << end; /* Part B */*/ int numB = numA; cin >> usrInput; int usrInput = 0; numB = numB + usrInput; cout << numB <"/n";    /* Part C */ int numC = 10000000000; cout << numC << endl; return 0; }

  • Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) {...

    Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: ";    cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2;    return 0; }

  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • // This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int...

    // This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int main() {    Rectangle rectangle1;    Rectangle rectangle2;    rectangle1.setLength(10.0);    rectangle1.setWidth(5.0);    rectangle2.setLength(7.0);    rectangle2.setWidth(3.0);    cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;    cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;    cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;    cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT