Question

The input should be n - the number of triangles (1 <= n <= 20) and afterwards n rows of three doubles each (corresponding to each of the triangles' sides). The output should be the "n" which has the maximum triangle area.

#include <iostream>
#include <math.h>
using namespace std;

const int MAX_SIZE = 20;

int main()
{
    int n, s, p;
    double max = 0;
    cin >> n;
    int x[MAX_SIZE];
    for (int i = 0; i < n; i++)
    {
        double y[2];
        for (int j = 0; j < 3; j++)
            cin >> y[j];
        p = (y[0] + y[1] + y[2]) / 2;
        s = sqrt(p * (p - y[0]) * (p - y[1]) * (p - y[3]));
        if (s >= max) max = s;
    }
    cout << max;
    return 0;
}

That's what I've done so far. "p" stands for semiparameter by the way.. - I'm using Heron's formula. I haven't even got it to "cout" the n in which the area is max but rather the maximum area itself, yet it doesn't work but gives me a massive error instead. Any ideas?

Était-ce utile?

La solution

You've got a few problems:

  • You need to change s and p from ints to doubles (otherwise you'll get unwanted truncation of your results).
  • You need to change double y[2]; to double y[3]; (since you need three side lengths, not two).
  • Change s = sqrt(p * (p - y[0]) * (p - y[1]) * (p - y[3])); to s = sqrt(p * (p - y[0]) * (p - y[1]) * (p - y[2])); (since y[3] is out of bounds of your array).

Note also that you can get rid of your array x, since you don't seem to actually use it anywhere.

Autres conseils

You are allocation only 2 doubles. You need 3, try double y[3].

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top