Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

box class array

Hi,
Is there anyone here help me with arrays ?? i attached two files but i don't know how to start with the arrays ?

* Creates an array that can hold up to 100 boxes

* open a file name "box.dat" that holds the name, height, width, and depth for an unknown number of boxes(not to exceed 100) one data element per line. Note: you will need to use getline to read the name.

* Read in each box and store it in the array

* print a list of boxes that have a volume of at least 100 cubic units. The list should include the name, volume, and surface area of each box. This list should also be sorted by name.
[651 byte] By [leapinlizard] at [2007-11-11 9:52:30]
# 1 Re: box class array
an array is defined by
type name[size];
an array of 100 integers then would be
int an_array[100];

note that these are unsafe (you can try to hit location 105 in your code and make a memory error, for example). Vectors are an object oriented array wrapper built into c++ -- they are a bit heavier but very feature rich. There are places to use both.

it works for a class too:

box Bunch_of_boxes[1000];
jonnin at 2007-11-11 20:59:47 >
# 2 Re: box class array
so what u suggest like int an_array[1000] ?
leapinlizard at 2007-11-11 21:00:52 >
# 3 Re: box class array
no, your assignment was to make the array of boxes.
oh, and to use it, you do something like

for(x = 0; x < size; x++) //where size is you array size
Do_Something_With(Bunch_of_boxes[x]);
jonnin at 2007-11-11 21:01:51 >
# 4 Re: box class array
is there anyway u can write this prog for me at least just the part of it ? plzzzz thanks
leapinlizard at 2007-11-11 21:02:52 >
# 5 Re: box class array
example, it should not actually be used AS your assignment.

const int size = 10;
class box
{
public:
int height;
int width;
};

box BoxArray[size];
int i;
for(i = 0; i < size; i++)
{
BoxArray[i].width = i;
BoxArray[i].height = i*3;
}

etc...
then you do the file stuff.

ifstream inf;
inf.open("filename");
inf >> BoxArray[0].height;
... etc...
jonnin at 2007-11-11 21:03:51 >