0
#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; } } 

I know how to get the maximum value, but don't know how to get the position of the max value "without using algorithm or vector". There is one rule, if there are two same maximum value, we take the former one's position.

For example,

 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 
3
  • There's an algorithm for that. Commented Nov 9, 2017 at 18:30
  • 2
    I must mention that array indexes in C++ start from 0, not from 1. Commented Nov 9, 2017 at 18:31
  • maxindex = i; ? Commented Nov 9, 2017 at 18:55

2 Answers 2

1

how to find a max value in an array. Psuedo code

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

now max has max value.

Sign up to request clarification or add additional context in comments.

2 Comments

"How to find a max value in a positive array." then. 0 is not special in generic context.
since he is calculating triangle sizes i assume that there are no negative values
0

The correct solution is to use std::max_element() and std::distance(), but since you are not allowed to use them, you will have to find the position of the max value manually. You already have the right idea for how to do that, but you didn't implement it correctly. You are not using the correct array indexes in your loops, and you are not assigning maxindex correctly.

Try something more like this instead:

#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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.