| RachelAnderson (65) | |||
|
Hi all.What I am trying to do is a program that calculates the area of a polygon. I divided my program into two subcodes.The first part computes the area, and the second calculates which points create the largest area of the polygon,.My problem is that now I got the points, and the calculation mechanism of the area works, but I cant make it so the calculated points go automatically to the function which calculates the area.Because the points are in vector stack and mass, I try to assign the two of them to the vector v, which goes to the function which computes the area...Here is my code so far, I tried several methods, now I am really stuck.I will be very happy if you help me. Thanks Rachel Anderson Code:
| |||
|
Last edited on
|
|||
| RachelAnderson (65) | |
|
In line 174 and 183 it gives 2 errors for each line.The error is : 183 double[int]' for array subscript. Can i change this one- v.push_back(make_pair(x[i], y[i])); with something else to avoid this error? | |
|
|
|
| Browni3141 (466) | |
| You are trying to use x and y as arrays, but they are only doubles. | |
|
|
|
| RachelAnderson (65) | |
| So, what should I do? | |
|
|
|
| RachelAnderson (65) | |
| If I make a new vector and push every x[i] and y[i] in it, and after that use this vector in the function where I want to put them now? | |
|
Last edited on
|
|
| RachelAnderson (65) | |
| Nobody can give me an advice?Come on people.I really need your help... | |
|
|
|
| computerquip (1997) | |
|
Is the polygon regular? Why do you need vectors in the case that the need for dynamically sized memory doesn't seem to be needed? And last but not least, I stopped about 1/5 of the way through your example and said that it's basically unreadable. Either comment and reformat some of the rather strange things you did or you won't be finding much help here. You have well over enough unneeded lines of white space. | |
|
Last edited on
|
|
| RachelAnderson (65) | |||||||
I think now its more understandable.The polygon is not regular.As I said, the code works, but I made it to output the points which are needed to calculate the area of the polygon
, and after that the user should input them again.Now what I want to do is to input them automatically. So I use this:
That vector "v" goes to the function that calculates the area. There is some logical mistake, and I dont know how to fix it. Thanks :) | |||||||
|
Last edited on
|
|||||||
| RachelAnderson (65) | |||
To be more clear, I will post the code which works. Here it is:
Look at the way the pairs are pusshed in v.I want to change it. When the program starts, it asks the user to input the points. The program outputs the points which are needed to be calculated the area.Then the program asks how many are those points.The user inputs the number of the points, and after that He/she should input the points.Then the program computes the area...My question is- can I avoid all of this and make it more simple so the user inputs the points of the polygon, and the program makes the rest? | |||
|
|
|||
| RachelAnderson (65) | |
Is it so complicated?I had a new idea.To use v.push_back(make_pair(stack.x[i], stack.y[i]));instead of v.push_back(make_pair(x[i], y[i]));.I think it should be like this, but it gives an error- 141 'class std::vector<A, std::allocator<A> >' has no member named 'x' 141 'class std::vector<A, std::allocator<A> >' has no member named 'y' Any ideas? | |
|
|
|
| Computergeek01 (2874) | |
|
The way you've named your variables based on nothing in particular, your inconsistent indentations and use of brackets, lack of whitespace where it matters, random declaration style and habit of making new variables as you need them is what's making it complicated. So to answer your question, your task although simple is being made more difficult having to decipher this mass of code. "x" and "y" are NOT array's so they CAN NOT be used as arrays. You fix this by redefining "x" and "y" as arrays. | |
|
|
|
| RachelAnderson (65) | |
| Yes yes, you are right, I just have the habit to write the code, and after its compiling and running properly, i make it more "elegant".As for the x's and y's, yes I made a mistake (omg), instead of v.push_back(make_pair(stack[i].x, stack[i].y)); i wrote v.push_back(make_pair(stack.x[i], stack.y[i]));.I fixed that.Now it compiles without errors, but gives an error in the computations.Now I am trying to fix it... | |
|
|
|
| RachelAnderson (65) | |||
I reduced my code a lot, and now it looks like this:
The problem now is that I cant find a way to assign the points which I need for the calculations.The best result would be to take the points from a .txt file, but I couldnt do it.Now I try to make two vectors, which stores the points, and with two loops to take those points and merge them in vector "v".The code works, but there is a logic error, because the calculations are wrong.. Any ideas what should I do? Thanks :) | |||
|
Last edited on
|
|||
| RachelAnderson (65) | |
| Nobody helps me, not very nice feeling ;)... | |
|
|
|
| Moschops (5981) | |||
How many points did you intend to create there? | |||
|
|
|||
| RachelAnderson (65) | |||
|
Actually two points.I noticed that I can make it without the second loop.I also added a diagnostic messages, to see why I get a wrong answer. I made it like this:
the output is: 56 39 35 68 62 18 7 44 18 30//everything is good so far 1699946596 83//That shouldnt be here ps. With different set of points it gives the same strange output at the end-1699946596 83 ps. After some tests I found out that it comes from here cout<<b[i]<<"\n";...
| |||
|
Last edited on
|
|||
| RachelAnderson (65) | |
I found it when I removed cout<<a[i]<<" ";.But when I changed the places of cout<<a[i]<<" "; and cout<<b[i]<<"\n";, this 1699946596 83 comes with "a"..Any ideas, I really dont know what it means. | |
|
|
|
| Moschops (5981) | |
If a and b are of size 5, and your for loop goes from i=0 to i=5 (which is being done with i<=a.size()), you're going to read a[0], a[1], a[2], a[3], a[4], a[5].What is a[5]? It is off the end of your vector and will be some garbage value. You need to stop reading values at a[4]. | |
|
|
|
| RachelAnderson (65) | |
| If I understand you correctly, I should make it: for (int i=1...), but when I make it like this, the garbage still appears, but there is missing one pair of the points. | |
|
|
|
| Moschops (5981) | |
|
A vector named a of size 5 has the following elements: a[0], a[1], a[2], a[3], a[4]. If you start at i=i and end at i=5, you will read the following elements: a[1], a[2], a[3], a[4], a[5]. If you do this, you will have missed out a[0] (and, of course, in your code b[0] as well) and you'll be reading the garbage value a[5], which is exactly what you're doing. You need to read a[0], a[1], a[2], a[3], a[4]. You need to start at i=0 and finished at i=4. | |
|
|
|