Re: I need help with homework problem
From: J Swift (junk_at_hotmail.CUT.com)
Date: 02/05/05
- Next message: Faheem Mitha: "Re: accessor member functions and const"
- Previous message: Gregory W. Ernest: "Access one character in an array of characters"
- In reply to: Alwyn: "Re: I need help with homework problem"
- Next in thread: Alwyn: "Re: I need help with homework problem"
- Reply: Alwyn: "Re: I need help with homework problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 4 Feb 2005 19:01:33 -0700
"Alwyn" <alwyn@blueyonder.co.uk> wrote in message
news:pan.2005.02.04.23.24.34.545625@blueyonder.co.uk...
> On Fri, 04 Feb 2005 15:40:16 -0700, J Swift wrote:
>>
>> CODE SO FAR
>> class rectangle
>
> Why not call it 'Room'?
>
>> {
>> public:
>> rectangle(double l = 0.0, double w = 0.0);
>> ~rectangle();
>
> I don't think you'll need a destructor, since you probably won't be
> deallocating anything.
>
>> double area()const;
>> double perimeter()const;
>> void setSides(double l, double w);
>
> Are you going to need 'setSides'? The sides will be set only once in the
> constructor, I think.
>
>> double getLength()const;
>> double getWidth()const;
>> private:
>> double length;
>> double width;
>
> In the example in the problem description, the inputs and outputs were
> integers.
>
>> };
>>
>> int main ()
>> {
>> // input the number of rooms in the house int n;
>>
>> // dynamically allocate an array of n rectangles rectangle *room;
>>
>> int maxPerimIndex = -1;
>> // maintains the index of the room with largest perimeter. //
>> the -1 is replaced by 0 immediately
>>
>> double length, width;
>> double totalArea = 0.0, maxPerimeter = 0.0;
>>
>> // prompt for the number of rooms and allocate an array // of
>> rectangle objects
>> cout << "Enter the number of rooms in the house: "; cin >> n; room =
>> new rectangle [n];
>> if (room == NULL)
>> {
>> cerr << "Cannot allocate the array of rectangles" << endl;
>> exit(1);
>> }
>
> No need for this test; you'll get an exception thrown if the memory can
> not be allocated.
>
> You are assuming, by the way, that the input will be successful. There
> will be trouble if things go wrong here.
>
>> }
>> // loop inputing dimensions of each room, // computing total area of
>> rooms,
>> // and locating room with the largest perimeter. for( int i=0; i <
>> n; i++ )
>>
>> {
>> /* THIS IS WHERE I AM STUCK*/
>
> I think you're trying to put too much into this loop. Just get the length
> and the width, then create a 'rectangle' (or 'Room') object and put it at
> the position indexed by 'i' in the array.
/* THIS IS WHERE I *WAS* STUCK*/
// and this is the progress:
for( int i=0; i < n; i++ )
{
cout << "Room dimension: ";
cin >> length >> width;
rectangle aRoom(length, width);
room[i] = aRoom;
double tempPerimeter = aRoom.perimeter();
if (tempPerimeter > maxPerimeter)
maxPerimIndex = i;
double tempArea = aRoom.area();
totalArea += tempArea;
}
cout << "Total area of the rooms = "
<< totalArea << endl;
cout << "The room with the largest perimeter is "
<< room + maxPerimIndex << endl;
This is getting better, thanks to you and Mike, but as you can see I'm still
having some problems. One problem is that I'm getting a strange number for
the greatest perimeter.
>
>> }
>
> You now loop through the array to calculate the total area and then print
> it.
>
> You then search the array for the room with the biggest perimeter and
> then print its area.
>
>> }
>> delete [] room;
>> return 0;
>> }
>
> The above may not be the most efficient way of doing it, but it's simplest
> to break it down into manageable parts.
>
> HTH
>
> Alwyn
>
- Next message: Faheem Mitha: "Re: accessor member functions and const"
- Previous message: Gregory W. Ernest: "Access one character in an array of characters"
- In reply to: Alwyn: "Re: I need help with homework problem"
- Next in thread: Alwyn: "Re: I need help with homework problem"
- Reply: Alwyn: "Re: I need help with homework problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]