Professional Documents
Culture Documents
Arrays and Structures
Arrays and Structures
Objectives
• Introduce arrays
• Introduce structures
– Connect part I and II of Fundamentals of
Programming
What is Array
• A collection of identical data objects, which are stored in
consecutive memory locations under a common heading
or a variable name
• The individual values in array are called elements and are
also variables
• Syntax
• Type arryName[arrySize]; arraySize must be constant and
refers to the # of elements the array has
• Eg: int studGrade[50];//declaration
– the array variable studGrade together with index can be used
to refer to individual elements like studGrade[3] refers to the
grade of the fourth student since arrays are zero bounded
meaning refer to 0 to arraySize-1
Initializing Arrays
• Declaring an array of local scope (within a
function) will not initialize, so its content is
undetermined.
• Declaring a global array (outside any function)
initialize all elements to zero.
• Ex. Global array declaration: int day [7];
• every element of day will be set initially to 0:
Day 0 1 2 3 4 5 6
0 0 0 0 0 0 0
Initializing Arrays…
• when we declare an Array, we have the possibility to assign initial
values to each one of its elements using curly brackets { } .
• For example:
int day [7] = { 16, 4, 77, 40, 12, 71,88 };
Day 0 1 2 3 4 5 6
16 4 77 40 12 71 88