Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 10th, 2006, 9:26 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This is one way to do it, although I consider the requirements to be strained. If I were going to be dealing in carton batches and couldn't use containers (such as vectors), I'd define a carton batch object and use a dynamically allocated array, as shown. However, I'd pass back a batch object that contained a pointer to the array as well as the length of the array. I wouldn't specify a fixed batch size (such as 4), but would accept lwh entries until ENTER was input on a blank line. I'd then allocate however many cartons that was, fill in the batch object, and return it. The example shown here prints out all volumes. You can see how you'd just replace that with code to pluck out the max. I forgot to put in "delete pCartons []"; I leave that as an exercise.
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Carton
{
private:
    double length;
    double width;
    double height;

public:
    Carton ();
    Carton (double l, double w, double h);
    double getVolume () {return length * width * height;}
    void setLength (double l) {length = l;}
    void setWidth (double w) {width = w;}
    void setHeight (double h) {height = h;}
};

Carton::Carton()
{
    length = 1.0;
    width = 1.0;
    height = 1.0;
}


Carton::Carton (double l, double w, double h = 1.0)
{
    length = l;
    width = w;
    height = h;
}

Carton *inputCartons ();

int main ()
{
    Carton *pCartons = inputCartons ();
    Carton *pTemp = pCartons;

    for (int i = 0; i < 4; ++i)
    {
        cout << "Carton " << i << " volume = ";
        cout << pTemp->getVolume ();
        cout << endl;
        ++pTemp;
    }


    return 0;
}

Carton *inputCartons ()
{
    double Dimensions [4][3];
    int i;
 
    for (i = 0; i < 4; i++)
    {
        cout << "Package " << i+1 << endl;
        cout << "Enter the dimension for the length (enter 1 for default): ";
        cin >> Dimensions [i][0];
  
        cout << "Enter the dimension for the width (enter 1 for default): ";
        cin >> Dimensions [i][1];
    
        cout << "Enter the dimension for the height (enter 1 for default): ";
        cin >> Dimensions [i][2];
    }
    
    Carton *pCartons = new Carton [4];
    Carton *pTemp = pCartons;

    for (i = 0; i < 4; ++i)
    {
        pTemp->setLength (Dimensions [i][0]);
        pTemp->setWidth  (Dimensions [i][1]);
        pTemp->setHeight (Dimensions [i][2]);
        ++pTemp;
    }

    return pCartons;
}
Quote:
Originally Posted by Output
Package 1
Enter the dimension for the length (enter 1 for default): 2
Enter the dimension for the width (enter 1 for default): 4
Enter the dimension for the height (enter 1 for default): 5
Package 2
Enter the dimension for the length (enter 1 for default): 6
Enter the dimension for the width (enter 1 for default): 7
Enter the dimension for the height (enter 1 for default): 8
Package 3
Enter the dimension for the length (enter 1 for default): 5
Enter the dimension for the width (enter 1 for default): 5
Enter the dimension for the height (enter 1 for default): 5
Package 4
Enter the dimension for the length (enter 1 for default): 4
Enter the dimension for the width (enter 1 for default): 2
Enter the dimension for the height (enter 1 for default): 9
Carton 0 volume = 40
Carton 1 volume = 336
Carton 2 volume = 125
Carton 3 volume = 72
Press any key to continue
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 10th, 2006, 3:27 PM   #12
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by DaWei View Post
This is one way to do it, although I consider the requirements to be strained. If I were going to be dealing in carton batches and couldn't use containers (such as vectors), I'd define a carton batch object and use a dynamically allocated array, as shown. However, I'd pass back a batch object that contained a pointer to the array as well as the length of the array. I wouldn't specify a fixed batch size (such as 4), but would accept lwh entries until ENTER was input on a blank line. I'd then allocate however many cartons that was, fill in the batch object, and return it. The example shown here prints out all volumes. You can see how you'd just replace that with code to pluck out the max. I forgot to put in "delete pCartons []"; I leave that as an exercise.
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Carton
{
private:
    double length;
    double width;
    double height;

public:
    Carton ();
    Carton (double l, double w, double h);
    double getVolume () {return length * width * height;}
    void setLength (double l) {length = l;}
    void setWidth (double w) {width = w;}
    void setHeight (double h) {height = h;}
};

Carton::Carton()
{
    length = 1.0;
    width = 1.0;
    height = 1.0;
}


Carton::Carton (double l, double w, double h = 1.0)
{
    length = l;
    width = w;
    height = h;
}

Carton *inputCartons ();

int main ()
{
    Carton *pCartons = inputCartons ();
    Carton *pTemp = pCartons;

    for (int i = 0; i < 4; ++i)
    {
        cout << "Carton " << i << " volume = ";
        cout << pTemp->getVolume ();
        cout << endl;
        ++pTemp;
    }


    return 0;
}

Carton *inputCartons ()
{
    double Dimensions [4][3];
    int i;
 
    for (i = 0; i < 4; i++)
    {
        cout << "Package " << i+1 << endl;
        cout << "Enter the dimension for the length (enter 1 for default): ";
        cin >> Dimensions [i][0];
  
        cout << "Enter the dimension for the width (enter 1 for default): ";
        cin >> Dimensions [i][1];
    
        cout << "Enter the dimension for the height (enter 1 for default): ";
        cin >> Dimensions [i][2];
    }
    
    Carton *pCartons = new Carton [4];
    Carton *pTemp = pCartons;

    for (i = 0; i < 4; ++i)
    {
        pTemp->setLength (Dimensions [i][0]);
        pTemp->setWidth  (Dimensions [i][1]);
        pTemp->setHeight (Dimensions [i][2]);
        ++pTemp;
    }

    return pCartons;
}
Thank you Dawei!
I start now the code to search for the max volume
brad sue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to invoke static method from a Class object? smith.norton Java 1 Oct 5th, 2006 4:00 AM
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 2:35 AM
Syntax Error in defining class Object Mack1982 C++ 14 May 3rd, 2006 6:06 PM
C++ Object class? titaniumdecoy C++ 2 Mar 23rd, 2006 12:36 AM
Problem assigning a string to a class object. omdown C++ 3 Oct 4th, 2005 1:48 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC