Area of a rectangle

Hi. I am trying to calculate the area of a rectangle with x and y vertices. The program should finish either if the second vertices x is on the left side of the first vertices x or its y is smaller than the vertice 1s y.
#include <iostream>

using namespace std;

int main()
{
int vertice1x,vertice1y,vertice2x,vertice2y;
int n=1;
int area[n];
do{
cin>>vertice1x>>vertice1y>>vertice2x>>vertice2y;
area[n]=(vertice2x-vertice1x)*(vertice2y-vertice1y);
n++;
}while(vertice2x>vertice1x || vertice2y>vertice1y);
for(int x=1;x<n-1;x++){
cout<<area[x]<<endl;
}
return 0;
}
All seems good with this programm. But actually it takes a lot of time to execute. I think the problem is with the array. So, I want to delete something to get less execution time
arrays must be constant sized in standard c++, so n is not legal. many compilers support it anyway, but it isnt safe to rely on.
on top of which, making an array of size 1 is pointless.

arrays do NOT grow in c++. you are out of bounds and causing all kinds of hidden problems with this code (it likely damages nearby variables, which in turn could indeed cause it to hang or run forever or anything else).

you want a vector: those can resize on demand.
vector<int> area;
and to assign it in your loop, instead of area[n] = use
area.push_back((vertice2x-vertice1x)*(vertice2y-vertice1y));
and to print it you can use area.size() in a for loop like your n based loop, or you can use a ranged based for loop here and skip that extra typing.
look in the reference section here on vectors for more info... this is just the starting point.

c++ is zero based. so array[0] is the first item in an array or vector, not [1]

there may be other issues, but those are the most immediate ones.
more info: C++ is a mixed level language, meaning it is capable of low level things, and arrays in c++ are low level things. An array is effectively the starting point into a location in memory with a built in byte-size so it can move from one complete item to another (that is, for a 4 byte integer, it moves 4 bytes for you when you use [0] or [1], [1] is automatically the location+4 in that example). That is all it can do. You can't assign arrays to each other, it does not know how big itself is, it can't grow to hold more stuff, its just a raw block of memory in bytes (block meaning its all sequential bytes of memory). Most c++ coders and coders coming from purely high level languages use a vectors which can do these things (resize, assignment, know own size, more). Arrays are, take your pick: { a left over from C, an experts-only feature, a feature used in the 90s, dangerous and not used much anymore}. All those are true from one point of view or another :). I am older, and I fall back to arrays at times when I should not in quick home-code, and you will see examples of arrays everywhere in code, even when its not the best tool. Old habits die hard.

Roughly, then:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include<vector>

int main()
{
	int vertice1x,vertice1y,vertice2x,vertice2y;	
	vector<int> area;
	do
	{
		cin>>vertice1x>>vertice1y>>vertice2x>>vertice2y;
		area.push_back((vertice2x-vertice1x)*(vertice2y-vertice1y));		
	}while(vertice2x>vertice1x || vertice2y>vertice1y);
	for(int x=0; x<area.size(); x++)
	{
	  cout<<area[x]<<endl;
	}
  return 0;
}
Last edited on
Try this, there weren't a lot of things to tinker with.
You also might like to note the following:

1. Use whitespace (spacebar) for clarity.
2. x1, y2 etc are clearer names for the coordinates of the left and right corners?
3. Arrays are OK, vectors are OK too but the 'challenge' you had was to use (C-style) arrays which underpin a lot(all?) of C++ data structures, so they are important.
4. '\n' is a better choice than endl
5. PS I haven't thoroughly tested the while loop condition
:)

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
#include <iostream>

using namespace std;

int main()
{
    int x1{0}, y1{0}, x2{0}, y2{0};
    const int size = 100;
    
    double* area = new double[size];
    
    int n = 0;
    do{
        cout << "Enter corners (btm left, then top right): ";
        cin >> x1 >> y1 >> x2 >> y2;
        area[n] = (x2 - x1) * (y2 - y1);
        n++;
    }while((x2 > x1) || (y2 > y1));
    
    for(int x = 0; x < n - 1; x++)
    {
        cout << area[x] << endl;
    }

    return 0;
}
Thankyou so much for your help.
I still have a question...
I changed my program using a vector. The max memory I can use is 4096 KiB.
So, by checking my new program I get a MLE (Memory-Limit-Exception) on the online judge.
My code is the following:
#include <iostream>
#include<vector>

int main()
{
int vertice1x,vertice1y,vertice2x,vertice2y;
std::vector<int> area;
do
{
std::cin>>vertice1x>>vertice1y>>vertice2x>>vertice2y;
area.push_back((vertice2x-vertice1x)*(vertice2y-vertice1y));
}while(vertice2x>vertice1x || vertice2y>vertice1y);
for(int x=0; x<area.size()-1; x++)
{
std::cout<<area[x]<<std::endl;
}
return 0;
}
what was the problem? Do you need to store the values, or can you just write them out, one in, one out style?
How big are the inputs, do you need int, would char do? Short? 32bit? how big is int on your target? etc?
we don't know anything here, so its hard to tell you where to cut the memory.
a vector uses a *tiny* bit more memory than C-code would. Talking a few 10s of bytes.
Memory Limit Exceeded (ML): Your program tried to use more memory than the judge default settings. If you are sure that such problem needs more memory, please contact us.

We have no fix for that. You’ll have to do what they say and contact them
Use an algorithm which requires less memory.

Currently, your solution has linear memory complexity, because it stores every single vertex (the singular form of vertices is vertex). There is no need to do this: compute one result and write it out. Throw away the intermediate data.

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
  int x1, x2, y1, y2;  
  while ((std::cin >> x1 >> y1 >> x2 >> y2) &&
         (x2 >= x1 || y2 >= y1))
    std::cout << (x2 - x1) * (y2 - y1) << '\n';
} 
Last edited on
Thanks mbozzi, actually the problem is, that I have to cout all the outputs when the program finishes and not for each input...
and how many bits do you need to store the answers? What is the biggest area, and how many areas do you have?
start by figuring that out and seeing if the data will fit. If it won't fit, you need a scheme to compress it or represent it differently.
if it will fit but you get this error, preallocate your vector to the precise max size so it does not over-grow.
Last edited on
Please post the exact text of the assignment.

As stated the problem is not solvable in general because there is no way to avoid storing O(n) data in the worst case.

This means that the instructions have been misinterpreted, the assignment is nonsense, or there is information missing from the problem description.
Last edited on
Or ... OP just has ask the judge for more memory, now that he/she has had ample guidance from all of us to resolve this and we can all move on with a green tick.
Just solved the problem:)
Thanks for your help
#include <iostream>
using namespace std;
int main()
{ int x1,y1,x2,y2;
while(cin>>x1>>y1>>x2>>y2 && y2 >= y1 && x2 >= x1){
cout<<(x2-x1)*(y2-y1)<<endl;
}
return 0;
}
Topic archived. No new replies allowed.