-2

first post here so I hope this is how I should ask questions here, So, I have a function used in different classes (override), and I need to display the MaxValue of this function across different data. (don't know if I said it correcty).

Tried this first but it kept giving me the same answer even though it wasn't the max value:

class Program
    {
        static void Main(string[] args)
        {
            Trapezoid[] Shape = new Trapezoid[5];

            Shape[0] = new Trapezoid(4,3.5,2);
            Shape[1] = new Rectangle(9,2);
            Shape[2] = new Square(5);
            Shape[3] = new Trapezoid(2,6.8,10);
            Shape[4] = new Square(8);
            
            Trapezoid maximumArea = null;
            
            foreach (Trapezoid trapezoid1 in Shape)
            {
                double area1 = trapezoid1.Area();
                
                foreach (Trapezoid trapezoid2 in Shape)
                {
                    double area2 = trapezoid1.Area();

                    if (area1 >= area2)
                    {
                        maximumArea = trapezoid1;
                    }
                    else
                    {
                        maximumArea = trapezoid2;
                    }
                }
            Console.WriteLine(maximumArea.ToString());
            Console.ReadLine();
        }
    }

the second time I tried this but couldn't make it work:

    class Program
    {
        static void Main(string[] args)
        {
            Trapezoid[] Shape = new Trapezoid[5];

            Shape[0] = new Trapezoid(4,3.5,2);
            Shape[1] = new Rectangle(9,2);
            Shape[2] = new Square(5);
            Shape[3] = new Trapezoid(2,6.8,10);
            Shape[4] = new Square(8);
            
            Trapezoid maximumArea = null;

            foreach (Trapezoid trapezoid1 in Shape)
            {
                double area1 = trapezoid1.Area();

                foreach (Trapezoid trapezoid2 in Shape)
                {
                    double area2 = trapezoid2.Area();

                    maximumArea = Math.Max (area1 , area2);
                }
            }

            Console.WriteLine(maximumArea.ToString());
            Console.ReadLine();

        }
    }

the function is Area, if you need to see it:

class Trapezoid
    {
        protected double a,b,h;

        public Trapezoid(double a, double b, double h)
        {
            this.a = a;
            this.b = b;
            this.h = h;
        }

        public virtual double Area()
        {            
            return ((a + b) * h) / 2;
        }
        public override string ToString()
        {
            return "The area of this Trapezoid is " + Area() + ".";
        }
    }

did the same thing in rectangle and square classes by overriding (inherited classes).

I hope you can help me, thank you. :)

Cheatah
  • 1,007
  • 1
  • 6
  • 17
ismaiaka
  • 7
  • 2
  • 2
    What do you mean by "did the same thing"? You are not showing that code so there is no way for us to be sure about that or whether you understand inheritance. – Cheatah Feb 23 '21 at 00:23
  • See duplicates. Just substitute the call to `Area()` for the property in those examples. – Peter Duniho Feb 23 '21 at 00:33

2 Answers2

3

You need to use maximumArea to store the max of the list, not the max of the current pair.

Trapezoid maximumArea = new Square(0);  //Smallest possible trapezoid

foreach (Trapezoid t in Shape)
{
    if (t.Area() > maximumArea.Area()) maximumArea = t;
}
Console.WriteLine(maximumArea);

You can also shorten this considerably using LINQ:

var maximumArea = shape.OrderByDescending( x => x.Area() ).First();

And if there's a chance that some trapezoids will tie, maybe you should get a list:

var max = shape.Max( x => x.Area() );
var maximumAreas = shape.Where( x => x.Area() == max).ToList();
John Wu
  • 44,075
  • 6
  • 37
  • 69
2

Use the power of Linq:

using System.Linq;

then

var trapezoidWithTheLargestArea = Shape.OrderByDescending(s => s.Area()).First()
nvoigt
  • 61,531
  • 23
  • 73
  • 116