Arrays Lecture Notes
Arrays Lecture Notes
C Programming
Lecture Topics
Array definition
Array declaration
Array assignment
Array Input / Output
Examples
What is an Array?
◼ It’s a collection of variables (the same type)
grouped into one name.
◼ More specifically, it’s a group of memory
locations for variables of the same type and
specified by the same name.
◼ It makes dealing with related variables much
easier.
Parts of Arrays
Elements
Refers to the number of individual items
represented by the array
Arrays
A particular value in an array is referenced using
the array name followed by the index in brackets
For example, the expression
scores[2]
Arrays
For example, an array element can be assigned a
value, printed, or used in a calculation:
scores[2] = 89;
scores[first] = scores[first] + 2;
Declaring Arrays
int scores[10] ;
The type of the variable scores is an array
of integers
Declaring Arrays
char codes[30] ;
Initializing an Array
#define SIZE 10
int main()
{
int myFirstArray[SIZE], i;
for (i=0; i<=SIZE-1; i++)
{
myFirstArray[i] = i * 2;
printf("myFirstArray with subscript of %d holds the
value %d\n", i, myFirstArray[i]);
}
return 0;
}
Two-Dimensional Arrays
A one-dimensional array stores a list of elements
Two Dimensional Arrays
GPA grade
Example