在这种情况下如何找到数组中最大值的位置?

时间:2017-11-09 18:29:25

标签: c++

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

class Triangle {
private:
    double area;
    double side1, side2, side3;

public:
    Triangle() {
        side1, side2, side3 = 0.0;
    }
    Triangle(double a, double b, double c) {
        side1 = a, side2 = b, side3 = c;
    }
    void setsides(double a, double b, double c) {
        side1 = a, side2 = b, side3 = c;
    }
    void calarea() {
        double s = (side1 + side2 + side3) / 2;
        area = sqrt(s*(s - side1)*(s - side2)*(s - side3));
    }
    double getarea() {
        return area;
    }

};

void main() {
    double s1, s2, s3;
    int n;
    int i;
    double max = 0;
    int maxindex = 0;
    Triangle arr[10];

    cout << "Enter the number of triangles in range 1:10: ";
    cin >> n;

    for (i = 1; i < n + 1; i++) {
        cout << "\nEnter the sides triangle " << i << ": \n";
        cin >> s1 >> s2 >> s3;
        arr[i].setsides(s1, s2, s3);
        arr[i].calarea();
        arr[i].getarea();
    }

    for (i = 1; i < n + 1; i++) {
        cout << "Area of triangle " << i << " is: " << setprecision(2) << fixed << arr[i].getarea() << "\n";
    }

    for (i = 1; i < n + 1; i++) {
        if (arr[i].getarea() > max) {
            max = arr[i].getarea();
            maxindex++;
        }
        cout << "\nThe largest area " << max << " is of triangle " << maxindex << endl;
    }
}

我知道如何获取最大值,但不知道如何在不使用algorithmvector的情况下获取最大值的位置。有一条规则,如果有两个相同的最大值,我们采取前一个的位置。

例如,

Area of triangle 1 is: 1.98
Area of triangle 2 is: 6.00
Area of triangle 3 is: 6.00
Area of triangle 4 is: 2.83
The largest area 6.00 is of triangle 2

2 个答案:

答案 0 :(得分:1)

如何在数组中查找最大值。 Psuedo代码

max = 0
foreach element
   if element > max then max = element

现在max有最大值。

答案 1 :(得分:0)

正确的解决方案是使用std::max_element()std::distance(),但由于您不允许使用它们,因此您必须手动找到最大值的位置。你已经有了正确的想法,但是你没有正确地实现它。您没有在循环中使用正确的数组索引,并且未正确分配maxindex

尝试更像这样的东西:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

class Triangle {
private:
    double side1, side2, side3, area;

    void calcarea() {
        double s = (side1 + side2 + side3) / 2;
        area = sqrt(s*(s - side1)*(s - side2)*(s - side3));
    }    

public:
    Triangle() {
        side1 = side2 = side3 = area = 0.0;
    }

    Triangle(double a, double b, double c) {
        side1 = a; side2 = b; side3 = c; calcarea();
    }

    void setsides(double a, double b, double c) {
        side1 = a; side2 = b; side3 = c; calcarea();
    }

    double getarea() {
        return area;
    }    
};

void main() {
    double s1, s2, s3, area;
    int i, n;
    double max = 0;
    int maxindex = -1;
    Triangle arr[10];

    cout << "Enter the number of triangles in range 1:10: ";
    cin >> n;

    for (i = 0; (i < n) && (i < 10); i++) {
        cout << "\nEnter the sides triangle " << i+1 << ": \n";
        cin >> s1 >> s2 >> s3;
        arr[i].setsides(s1, s2, s3);
    }

    for (i = 0; i < n; i++) {
        cout << "Area of triangle " << i+1 << " is: " << setprecision(2) << fixed << arr[i].getarea() << "\n";
    }

    for (i = 0; i < n; i++) {
        area = arr[i].getarea();
        if (area > max) {
            max = area;
            maxindex = i;
        }
    }

    /* alternatively:

    if (n > 0)
    {
        max = arr[0].getarea();
        maxindex = 0;
        for (i = 1; i < n; i++) {
            area = arr[i].getarea();
            if (area > max) {
                max = area;
                maxindex = i;
            }
        }
    }
    */

    cout << "\nThe largest area " << setprecision(2) << fixed << max << " is of triangle " << maxindex+1 << endl;
}