Reading Assignment
read up to multidimensional arrays
cplusplus
accessing the elements of an array
the most common method for simple programs is to use the index operator
it is basically a fancy function
given
int myarray[] = {5,10,15,20,25,30,35,40};
you can use the index operator
cout << myarray[0] << endl; // this will display the first value in the array, 5
myarray[2] = 66; this will assign the 3rd value in the array to be 66;
we can use the index operator [] to get or set an element of the array
----------------------------------------------------------------------------
you can also use pointer arithmatic.
we like to call the thing we created above and array...
but "myarray" is just a pointer, thats it, nothing special at all
it does allocate some memory as well, in this case a piece of memory large enough to store 8 integers
myarray is set to the address of that piece of memory
cout << myarray[0] << endl; // prints out the first value in the array
cout << *myarray << endl; // prints out the first value in the array * before a pointer will retrieve the value it points to
cout << myarray[4] << endl; // prints out the fifth value in the array
cout << *(myarray +4) << endl; // prints out the first value in the array * before a pointer will retrieve the value it points to
*(myarray+3) = 33; // set the 4th value in the array to 33
myarray+0 is a pointer to the 1st value
myarray+1 is a pointer to the 2nd value
myarray+2 is a pointer to the 3rd value
myarray+3 is a pointer to the 4th value
myarray+4 is a pointer to the 5th value
myarray+5 is a pointer to the 6th value
// print the values in the array with out using []
for (int loop =0;loop <8 ; loop++)
cout << "element number " << loop << " in the array has a value of " << * (myarray + loop) << endl;
// now with using []
for (int loop =0;loop <8 ; loop++)
cout << "element number " << loop << " in the array has a value of " << myarray[loop] << endl;