Lecture_Notes_11_12_13

Lecture_Notes_11_12_13 - Arrays - Grouping variables...

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

View Full Document Right Arrow Icon
Arrays - Grouping variables together Instead of assigning different names to different variable, just like: int m1, m2, m3, m4, m5; You can assign one name to all five variable: int m[5]; m1 m2 m3 m4 m5 m[0] m[1] m[2] m[3] m[4] type size name
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 An array is a consecutive group of memory locations that all have the same name and the same type
Background image of page 2
Declaring Arrays type name [size]; type name [size]={value1, value2,…. .}; e.g., int scores [5]; or int scores[5]={1,2,4,6,9}; or const int x=5; //in case of const int x; x is not initialized and results in compile time error (from week2) float scores[x]; [0] [1] [2] [3] [4] 1 2 4 6 9 size variable should be of type const int - also known as array subscript
Background image of page 3

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

View Full DocumentRight Arrow Icon
Declaring Arrays If the array size is omitted from a declaration with initializer list , the number of elements in the array will be the number of elements in the initializer list. e.g., int scores[]={1, 2, 4, 6, 9}; // the size is not specified, yet 5 initialization values ____________________________________________________________ If there are fewer intializers than the elements in the array, the remaining elements are automatically initialized to zero. e.g., int scores[5]={1,2,4}; //unspecified values will be zero Or int scores[10]={0}; ____________________________________________________________ Providing more initializers in an array initializer list than there are elements in the array is a syntax error. e.g., int scores[5]={1, 2, 4, 6, 9, 12}; [0] [1] [2] [3] [4] 1 2 4 0 0
Background image of page 4
Declaring Arrays In summary, the programmer specifies the type of each element and the number of elements required by each array so that the compiler may reserve the appropriate amount of memory. e.g.,
Background image of page 5

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

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

This note was uploaded on 04/01/2012 for the course GENE 121 taught by Professor Smucker during the Winter '11 term at Waterloo.

Page1 / 17

Lecture_Notes_11_12_13 - Arrays - Grouping variables...

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

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