Arrays - Arrays COP3014 Arrays Introduction An array is a...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays COP3014
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays Introduction An array is a data structure comprised of a consecutive group of memory locations that all have the same name and same type Arrays are static entities in that they remain the same size during program execution Array elements are referenced by their relative position number The first element always starts with zero. To refer to a particular location, or element, in an array, specify the name of the array and the position number of the element in the array
Background image of page 2
// Example of an array of integers // with 10 elements numbered 0 through 9. int c [10]; c[0] = -45; c[1] = 5; c[2] = 6 c[3] = 34; c[4] = 9; c[5] = 10; c[6] = 11; c[7] = 12; c[8] = 87; c[9] = 100;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What it looks like in memory -45 5 6 34 9 10 11 12 87 100 c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9]
Background image of page 4
Arrays are your first introduction to a concept in programming called Pointers Pointers are variables that, instead of containing a value, contain the address of where the value exists An array variable is a Pointer to the beginning address where the array begins. Example: C[0] says that go to the array “C” and start with the offset of zero (0).
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declaring Arrays Declaring an array and allocating memory at the same time: // Declare an array called “score” that // will hold 5 integers. int score[5]; Declaring an array and allocating memory at the same time using a constant to define the size: // Declare an array called “score” that // will hold Maxsize integers. // This is the recommended practice. // Maxsize is used throughout the program // to control the array. Changes to the // array size need only be done in one // place const int Maxsize=5 int score[Maxsize];
Background image of page 6
Declaring Arrays Declaring an array of unknown size and allocating memory during execution // Declare an array called “score” that // that does not have a size. // Initially it just holds an address // without a value (null) const int Maxsize=5; int * score; //score will hold // an address now. // The following goes to the // operating system and gets 5 integer // spaces in memory and gives the // beginning address to score score = new int[Maxsize];
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Accessing Arrays You access an individual component of an array using the square [] brackets as a subscript cout << count[X] << count[X=1]<<endl; Note there are two uses of square brackets In declaring the array size Everywhere else the subscript is used Subscript can be any discrete integer value int x=1; cout << count[1+1]; count << count [X+1]; count << count [ count[x]];
Background image of page 8
Array // During compilation, initialize 5 items int count [5]= {0,1,2,3,4}; // During compilation, initialize all to 0 int count [5] = {0}; // During compilation, the number of items // in the array are dictated by the number // being initialized. int count [] = {1,2,3,4,5};
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

Arrays - Arrays COP3014 Arrays Introduction An array is a...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online