Arrays in C
Arrays in C
Introduction to
Data Structures
o Example:
Note: The array index always starts at 0. Also, if we want to assign value at the
last element of the number array, we do
o The above code will give us the value stored at index 0, which
is the first element of our array.
o The next next slide shows us how to store and then print the 5
values we stored in our array
Value 0 1 2 3 4
Index 78 12 85 96 23
• The compiler automatically assigns the size according to the number of elements
you initialized.
o The sizeof()
returns the size, in bytes, of a variable or data type
int = 4 bytes
float = 4 bytes
double = 8 bytes
array size
We first get the byte size of an element from the array, preferably the first one,
and divide it to the total byte size, giving us 5.
Here, we explicitly
wrote the terminating
character
Here, we omitted it
and let the compiler do
the job.
o First method:
o No Array size, required terminating character at the end
Pwede ni
Pwede pd ni
o Second method:
We cannot do this
ITE 12 – FUNDAMENTALS OF PROGRAMMING
Types of Array
ONE DIMENSIONAL ARRAY
MULTI - DIMENSIONAL ARRAY
and so on.
columns
ITE 12 – FUNDAMENTALS OF PROGRAMMING
Declaring a two dimensional array
o Let’s say, you want to put the value 35 in the green cell
0 1 2 3 4
1 3
2
5
1 3
2
5
7
5
o How about 75?
0
1 2 4 1 8
2 3 5 0 9
1
1 6 3 1 9
2
3 7 5 4 8
8 1 2 7 1
0 8 0 5
ITE 12 – FUNDAMENTALS OF PROGRAMMING
Assigning values to a 2D array using a loop
0
0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0