hello...i'm currently a third year highschool student...and i was wondering if anyone can help me on how to use arrays and utilizing them in fstreams...i'm currently doing my case study and i'm stuck since i still don't have any idea on how to use arrays...our teacher didn't teach us about them, and others just self-studied...i just don't know how to deal with arrays...can anyone help me?

Recommended Answers

All 9 Replies

An array is a number of objects.

Example on array:

const int array_size = 10; // const because Nick Evan says so :)
float floatArray[array_size]; // this is how we make an array
floatArray[0] = 4.2; // this is how we access the first element in the array

for(int i = 0; i <= array_size-1; i++)
{
    floatArray[i] = i*2.56; // how we dynamically set values in all elements in the array.
} // note that i said array_size minus one. Thats because the index number for the last element will always be one less than the array size, because the indexing starts at 0 instead of 1.

A normal array cannot be dynamically changed, so if you make an array of 10, and suddenly want it to hold 11, then you have to make a whole new array and copy the original data to it.

You can redetermine the size of an array like this:

float bigArray[x]; // x, because we don't know the size of the array.
array_size = sizeof(bigArray)/sizeof(float); // we take the size of the array, and divide with the size of a single float. Leaves us with the number of floats in the array.

for(int i = 0; i < array_size-1; i++) You lose the last element this way. Should be i<array_size, so if you have an array of size 3 you get 0,1,2

Oh yeah, you're right, thanks for noticing.

Also this:

int array_size = 10;
float floatArray[array_size]; // this is how we make an array

..is not allowed in C++. array_size has to be declared constant. (or use new and delete)
So:

const int array_size = 10;
float floatArray[array_size]; // this is how we make an array

Really? I'm sure I've done that it the way I posted before.
Anyway, thanks for correcting x)

what compiler are you using? MinGW and G++ have the option "-pedantic" to ensure ISO C++ standard. It should give you a error like "ISO C++ forbids variable size array"
Visual studio has -w4 as option to ensure that you comply with the ISO standard.

Yeah, I have ISO C++ standards on. I'm using the GNU compiler usually.
The error you mentioned sounds familiar, probably it's just me being forgetful. :D

Also this:

int array_size = 10;
float floatArray[array_size]; // this is how we make an array

..is not allowed in C++. array_size has to be declared constant. (or use new and delete)
So:

const int array_size = 10;
float floatArray[array_size]; // this is how we make an array

LOL. My brain read const into the expression int array_size = 10. Sorry OP! Thanks "NE"

LOL. My brain read const into the expression int array_size = 10. Sorry OP! Thanks "NE"

OP haven't been online during all this, so he'll probably not mind anyway ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.