Area of Rectangles

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (4) was titled “Making Decisions”.

I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.

4.4: Area of Rectangles: The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same.

My solution:

/* Chapter 4 Challenge 4
   Written by Kirk Hingsberger
   October 2, 2007
   Comparing rectangle areas
*/

#include <iostream>
using namespace std;

int main()
{
  float width1, width2, length1, length2, rect1, rect2;

  cout << "Enter inches width of first rectangle: ";
  cin >> width1;
  cout << "Enter inches length of first rectangle: ";
  cin >> length1;
  cout << "Enter inches width of second rectangle: ";
  cin >> width2;
  cout << "Enter inches length of second rectangle: ";
  cin >> length2;

  rect1 = width1 * length1;
  rect2 = width2 * length2;

  if (rect1 > rect2)
    {
      cout << "Rectangle 1 (" << rect1 << ") is " << rect1 - rect2
	   << " area inches larger than rectangle 2 ("
	   << rect2 << ")." << endl;
    }
  else if (rect1 < rect2)
    {
      cout << "Rectangle 2 (" << rect2 << ") is " << rect2 - rect1
	   << " area inches larger than rectangle 1 ("
	   << rect1 << ")." << endl;
    }
  else if (rect1 == rect2)
    {
      cout << "Rectangles 1 and 2 are equal at "
	   << rect1 << " area inches." << endl;
    }

  return 0;
}

Here is the capture script I turned in for grading:

Script started on Tue 02 Oct 2007 10:49:54 AM MDT
$ cat chap4chlg4.cc
/* Chapter 4 Challenge 4
   Written by Kirk Hingsberger
   October 2, 2007
   Comparing rectangle areas
*/

#include <iostream>
using namespace std;

int main()
{
  float width1, width2, length1, length2, rect1, rect2;

  cout << "Enter inches width of first rectangle: ";
  cin >> width1;
  cout << "Enter inches length of first rectangle: ";
  cin >> length1;
  cout << "Enter inches width of second rectangle: ";
  cin >> width2;
  cout << "Enter inches length of second rectangle: ";
  cin >> length2;

  rect1 = width1 * length1;
  rect2 = width2 * length2;

  if (rect1 > rect2)
    {
      cout << "Rectangle 1 (" << rect1 << ") is " << rect1 - rect2
	   << " area inches larger than rectangle 2 ("
	   << rect2 << ")." << endl;
    }
  else if (rect1 < rect2)
    {
      cout << "Rectangle 2 (" << rect2 << ") is " << rect2 - rect1
	   << " area inches larger than rectangle 1 ("
	   << rect1 << ")." << endl;
    }
  else if (rect1 == rect2)
    {
      cout << "Rectangles 1 and 2 are equal at "
	   << rect1 << " area inches." << endl;
    }

  return 0;
}
$ g++ -o chap4chlg4 chapr4chlg4.cc -s
$ chap4chlg4
Enter inches width of first rectangle: 2
Enter inches length of first rectangle: 3
Enter inches width of second rectangle: 3
Enter inches length of second rectangle: 4
Rectangle 2 (12) is 6 area inches larger than rectangle 1 (6).
$ chap4chlg4
Enter inches width of first rectangle: 4
Enter inches length of first rectangle: 3
Enter inches width of second rectangle: 3
Enter inches length of second rectangle: 2
Rectangle 1 (12) is 6 area inches larger than rectangle 2 (6).
$ chap4chlg4
Enter inches width of first rectangle: 2
Enter inches length of first rectangle: 3
Enter inches width of second rectangle: 2
Enter inches length of second rectangle: 3
Rectangles 1 and 2 are equal at 6 area inches.
$ exit
exit

Script done on Tue 02 Oct 2007 10:50:40 AM MDT
  • Share/Bookmark




Leave a Reply