Hi guys,

I need to write a program in C++ that will input data for N rectangles (0<N<5). The data for each rectangle consists of coordinates of the lower left and upper right corners, which are positive integers, less than 100.
Then I need it to calculate the area of each rectangle.

Thanks,
Hristian

Hi guys,

I need to write a program in C++ that will input data for N rectangles (0<N<5). The data for each rectangle consists of coordinates of the lower left and upper right corners, which are positive integers, less than 100.
Then I need it to calculate the area of each rectangle.

Thanks,
Hristian

Think of how area is calculated.

In order to obtain the area of a rectangle, you only need to know the length and width of the rectangle. The area is--

length * width

--which is the easy part. Now, the question becomes "how do I obtain the length and width of the rectangle by only knowing the lower left and upper right corners?"

Draw two points and list their coordinates (give them fake ones if you want) then think of how you can come up with a length and width for a rectangle that would fit those coordinates.

commented: I dont know how you put up with him :P +2

Really thanks for the fast reply but I don't actually know anything about this part of the programming. So could you write the program if you can :/

Thanks,
Hristian

Really thanks for the fast reply but I don't actually know anything about this part of the programming. So could you write the program if you can :/

Thanks,
Hristian

Normally I wouldn't mind providing big hints for a project like this, but honestly the answer is right in front of you.

Before even thinking about programming, think of how the solution is obtained.

Also, try to make an attempt to write the code. You might actually get it.

Well the problem is that I perfectly understand what I have to do but I have absolutely no idea how to do it.. And I'm now on my Term Exam so I kind of need it now and I'll understand the way it's done later. It's just that it's my last chance now :/

Cheers,
Hristian

Well the problem is that I perfectly understand what I have to do but I have absolutely no idea how to do it.. And I'm now on my Term Exam so I kind of need it now and I'll understand the way it's done later. It's just that it's my last chance now :/

Cheers,
Hristian

Ok, so how is it done? I can step you through the coding.

I understand you but now I have 20 minutes left and if I don't pass this exam I'll have to do the test in september and I can't do it then. So if you could just write the program and I'll come to understand it later while I'm at home.

Thanks,
Hristian

I understand you but now I have 20 minutes left and if I don't pass this exam I'll have to do the test in september and I can't do it then. So if you could just write the program and I'll come to understand it later while I'm at home.

Thanks,
Hristian

I promised I wouldn't provide coded answers for people anymore. Here's the area in point-form.

(x2 - x1) * (y2 - y1) = area

where x2 is the second coordinates x value, and x1 is the first. The difference makes the width.

y2 is the second coordinates y value and y1 is the first. The difference makes the length.

Multiplying these two components makes the area.

All you have to do is find a way to snag each respectable point and code it. You'll also have to iterate through N rectangles and show their areas.

for(int i = 0; i < N; i ++)
    cout << rectNum[i].getArea()  << endl;

Where rectNum is an array of rectangles and getArea() is a method that calculates the area of the rectangle using the method mentioned above.

You can do this in less than 20 minutes.

I'm really thankful but I have only two values for each rectangle.

I'm really thankful but I have only two values for each rectangle.

I'm guessing each Rectangle is defined by a Point class/struct correct? Or are they just integral/floating types (int, double, unsigned int... etc).

You're not giving much information.

This is where I am now... And I have no idea how to do the other things :/

#include <iostream>
using namespace std;

int main()
{
  array rectNum;
  int N;
  cout << "How many rectangles do you want to input data for?";
  cin >> N;
  
  if(n<0 && n>5) cout <<"error";
  else
  
  for(int i = 0; i < N; i ++)
    cout << rectNum[i].getArea()  << endl;
  
  for(int i = 0; i < N; i ++)
    cout << "Enter the x1 value for rectangle " << i << ".";
    cin >> 
    cout << "Enter the y1 value for rectangle " << i << ".";
}

This is where I am now... And I have no idea how to do the other things :/

#include <iostream>
using namespace std;

int main()
{
  array rectNum; //most likely an error - not sure if there's an array type/object predefined in C/C++
  int N;
  cout << "How many rectangles do you want to input data for?";
  cin >> N;
  
  if(n<0 && n>5) cout <<"error";
  else //error, below code will most likely run no matter what N is, use blocks
  
  for(int i = 0; i < N; i ++)
    cout << rectNum[i].getArea()  << endl;
  
  for(int i = 0; i < N; i ++) 
    cout << "Enter the x1 value for rectangle " << i << ".";
    cin >> //error, nothing to input into
    cout << "Enter the y1 value for rectangle " << i << "."; //error, i doesnt exist, for loop reaches 1st line only
}

You have quite a few errors you'll be marked down for, and you still didn't answer my question.

Because I can't answer it. I posted where I am at so that you can possibly answer it yourself :/

I found some of the errors but I definately don't know what to do now :/

#include <iostream>
using namespace std;

int main()
{
  int rectNum[];
  int rectW[];       // Rectangle first point
  int rectH[];       // Rectangle second point
  int N;
  cout << "How many rectangles do you want to input data for?";
  cin >> N;
  
  if(n<0 && n>5) cout <<"error";
  else
  
  for(int i = 0; i < N; i ++)
    cout << rectNum[i].getArea()  << endl;
  
  for(int i = 0; i < N; i ++)
    cout << "Enter the x1 value for rectangle " << i << ":";
    cin >> rectW[i];
    cout << "Enter the y1 value for rectangle " << i << ":";
    cin >> rectH[i];
}

Because I can't answer it. I posted where I am at so that you can possibly answer it yourself :/

I found some of the errors but I definately don't know what to do now :/

#include <iostream>
using namespace std;

int main()
{
  int rectNum[];
  int rectW[];       // Rectangle first point
  int rectH[];       // Rectangle second point
  int N;
  cout << "How many rectangles do you want to input data for?";
  cin >> N;
  
  if(n<0 && n>5) cout <<"error";
  else
  
  for(int i = 0; i < N; i ++)
    cout << rectNum[i].getArea()  << endl;
  
  for(int i = 0; i < N; i ++)
    cout << "Enter the x1 value for rectangle " << i << ":";
    cin >> rectW[i];
    cout << "Enter the y1 value for rectangle " << i << ":";
    cin >> rectH[i];
}

You clearly don't know how to use a class, so you don't have a Rectangle class (which is what Alex was asking before, I think), so get rid of the .getArea() code. You may want to have a function called:

int GetArea(int x1, int y1, int x2, int y2)

If the code below is to be repeated:

for(int i = 0; i < N; i ++)
    cout << "Enter the x1 value for rectangle " << i << ":";
    cin >> rectW[i];
    cout << "Enter the y1 value for rectangle " << i << ":";
    cin >> rectH[i];

you need to have brackets:

for(int i = 0; i < N; i ++)
  {
    cout << "Enter the x1 value for rectangle " << i << ":";
    cin >> rectW[i];
    cout << "Enter the y1 value for rectangle " << i << ":";
    cin >> rectH[i];
  }

However, this isn't going to do the job. You need the user to input four integers, not two. A point is two integers and you have two points. Here's your original spec. It requires four integers.

I need to write a program in C++ that will input data for N rectangles (0<N<5). The data for each rectangle consists of coordinates of the lower left and upper right corners, which are positive integers, less than 100.
Then I need it to calculate the area of each rectangle.

It looks like you are out of time. You'll need to post earlier next time. It's against the rules on this forum to do the whole program for anyone.

Whoops--

whenever I think of something like a Rectangle or anything with its own characteristics, I immediately think class/struct is the way to go.

My mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.