Last post was the end of the basics of functions… This post starts the data legacy, where I’ll be covering information about different types of data and how to use them! Starting off our new “legacy” as I’ll call it, is probably the most commonly used and heard of member, the Arrays!
Programming wise it is: “An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.” (taken from cplusplus.com)
But that sounds awfully confusing right? Well here are some points you need to understand:
- Contiguous means touching, which in this case means next to each other; or adjacent
- Arrays are a collection of variables all of the same type
- They are stored under a common name
- Arrays can store up to just about as much variables as you’ll need.
- YOU MAY NOT CHANGE THE AMOUNT OF VARIABLES AN ARRAY CAN HOLD ONCE IT HAS BEEN DECLARED
So isn’t it about time we get down to business? We got an array of stuff to cover (excuse the pun, I’m feeling punny right now)!
The structure of an array is:
type name [element] = { value, value, value… }
Now to break it down:
- type – the type of array it’ll be (int, float, double, etc.)
- name – how the array will be called, identified as, and referred to as
- [element] – number of variables the array holds
- { value, value, value } – if you want to set initial values to the variables, here’s where you set them
NOTE: [element] can be left blank as long as the { value, value, value } part is filled, in that case [element] will equal the number of values you enter.
A full initialized array would look something like the following:
int herp [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Pretty simple right? Good luck and have fun (and for those in high school who live in California, good luck with the CAHSEE!)!