Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

C++ template example to find max area and perimeter of rectangles

#include <iostream>
#include <functional>
#include <stdio.h>      
#include <math.h>       
#include <string.h>
#include <vector>
#include <stdlib.h>     
using namespace std;

// Generic findMax, with a function object, C++ style.
// Precondition: a.size( ) > 0.
template <typename Object, typename Comparator>
const Object & findMax( const vector<Object> & arr, Comparator isLessThan )
{
    int maxIndex = 0;

    for( int i = 1; i < arr.size( ); ++i )
        if( isLessThan( arr[ maxIndex ], arr[ i]))
            maxIndex = i;
    return arr[ maxIndex ];
}

// Generic findMax, using default ordering.
template <typename Object>
const Object & findMax( const vector<Object> & arr )
{
    return findMax( arr, less<Object>{ } );
}

class Rectangle 
{
    /* 
        going to assume I can just make a rectangle in a 
        0,y2--------x2,y2
          |            |
        0,0--------x2,0 style.
        using distance forumla and stuff could also be used
        but since rectangles are 90 angels all around
        I'll will do it with the assumption that it is a nice rectangle
        and is not rotated in some form, since this would be a general answer
        if there is rotation a small change would work
    */
    private:
        int x1, y1, x2, y2; 
        int getLength (int start, int end)
            {return abs(start-end); }
        int getWidth (int start, int end)
            {return abs(start-end);}
        int Length = getLength(x1,x2);
        int Width = getWidth(y1,y2);
    public:
        void set_cords (int cord1, int cord2, int cord3, int cord4) 
        {
            x1 = cord1, 
            y1 = cord2, 
            x2 = cord3, 
            y2 = cord4;
        }
        void printStuff () 
        {
            printf ("%d \n", x1);
            printf ("%d \n", y1);
            printf ("%d \n", x2);
            printf ("%d \n \n", y2);
            printf ("%d \n", getLength(x1,x2));
            printf ("%d \n \n", getWidth(y1,y2));
            printf ("%d \n", Area());
            printf ("%d \n \n", Perimeter());
            
        }
        int Area() {return getLength(x1,x2)*getWidth(y1,y2);}
        int Perimeter() {return getLength(x1,x2)*2 + getWidth(y1,y2)*2;}
        /* 
        For some reason Length and Width give tons of problems not sure how to fix
        it so just going to use the step use to get the Length and Width  
        int Area(){return Length*Width;}
        int Perimeter() {return Length*2 + Width*2;}
        
        Must be something with PBR and PBV but not sure.
        */
        
        /*  no idea what this is for 
        bool operator( )( const int & lhs, const int & rhs ) const
        { return strcasecmp( lhs.c_str( ), rhs.c_str( ) ) < 0; }
        */       
};

int main () {
    int myints1[] = {0,0,5,3};
    int myints2[] = {0,0,3,9};
    int myints3[] = {0,0,5,25};
    int myints4[] = {0,0,10,13};
    Rectangle Rect1;
    Rectangle Rect2;
    Rectangle Rect3;
    Rectangle Rect4;
    Rect1.set_cords(myints1[0], myints1[1], myints1[2], myints1[3] );
    Rect2.set_cords(myints2[0], myints2[1], myints2[2], myints2[3] );
    Rect3.set_cords(myints3[0], myints3[1], myints3[2], myints3[3] );
    Rect4.set_cords(myints4[0], myints4[1], myints4[2], myints4[3] );
    /*  Can be use to check stuff in more detail if needed  
        Rect1.printStuff();
        Rect2.printStuff();
        Rect3.printStuff();
        Rect4.printStuff();
    */
    int myAreas[] = {Rect1.Area(), Rect2.Area(), Rect3.Area(), Rect4.Area()};
    int myPerimeters[] = {Rect1.Perimeter(), Rect2.Perimeter(), Rect3.Perimeter(), Rect4.Perimeter()};
  
    vector<int> Areas (myAreas, myAreas + sizeof(myAreas) / sizeof(int) );
    vector<int> Perimeters (myPerimeters, myPerimeters + sizeof(myPerimeters) / sizeof(int) );
    /*
    figure 1.25 use a lot of vectors for the max instead of using 
    the arrays.  The question ask for usage of array, but is kind of
    the same thing so, I just did it like so (?)
    Also no idea whatsoever on what 
    template <typename Object, typename Comparator>
    const Object & findMax( const vector<Object> & arr, Comparator isLessThan )
    stuff is about can't understand almost anything of that code portion.
    */
    printf ("%s %d \n", "the max area in these rectangels is: ", findMax(Areas));
    printf ("%s %d \n", "the max primeter in these rectangels is: ", findMax(Perimeters));
    
    /*
    Not Sure if I had to make something else, like point which rectangle is it or
     I have ro give a name to each rectangle and then after finding the one with the
     max area do a for loop to search which rectangle it was that had that area
     or something else, but to do that, it feels like it would require a lot more code
     than what it is asking for, or make the areas into vector of arrays where
     each array has a form {RectName, Area, Perimete} and then do some stuff with this
     but oing so means you would somewhat had to mess with the findMax to make it work
     but findMax feels according to the question feels like we shouldn't mess with.
     So I just print the result of which area and perimeter are the biggest.
    */
    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.