0% found this document useful (0 votes)
13 views40 pages

Arrays in C

please download it.

Uploaded by

mortredknight666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views40 pages

Arrays in C

please download it.

Uploaded by

mortredknight666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Arrays

Introduction to
Data Structures

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Introduction to Data Structure

o So far, we have talked about variables that can store different


data types
 These variables can hold only one value at a time

o As we go deeper in programming, we will need a more


sophisticated data structure
 The array

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Consider this . . .

o You are tasked to store 100 student’s grade for it to be


accessed later.
 Although it is possible to declare all 100 different variables, but it
is not efficient
 What if we scale up to 1000 students grade?

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Having a 100 variables declared . . .

o Your program will look like this

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Arrays

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Array

o Allows us to store multiple data with the same data type


in a single array variable

o By definition, an array is fixed-size sequential collection of data


/ elements of the same type.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Array

o has consecutive memory locations known as elements

o the length (or size) of an array is determined by the number of elements

o the elements are accessed based on its position


 the position is determined by the index (also known as subscript, starts at 0)

o Be careful of using subscripts,


 C has no checks to see if the subscripts you use exceeds the array size

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Declaration

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Declaring Arrays

o Example:

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Storing and Accessing element of an Array

o Storing and Accessing data in an array makes use of the array


index.

o Example: Store 10 at the first position of the array

 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

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Storing and Accessing element of an Array

o Example: Accessing, in this case printing, the value of the first


element we stored.

o The above code will give us the value stored at index 0, which
is the first element of our array.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Storing and Accessing multiple elements in an Array

o To store or access multiple elements in an Array, we will use


loops.

o The next slide shows us how to store 5 values in an array using


a for loop.

o The next next slide shows us how to store and then print the 5
values we stored in our array

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Storing multiple elements in an Array

Value 0 1 2 3 4
Index 78 12 85 96 23

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Storing multiple elements in an Array

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Initialization

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Initializing an Array

o You can initialize array with or without size


 With array size

 Without array size

• The compiler automatically assigns the size according to the number of elements
you initialized.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Initializing and Printing Array Sample Code

ITE 12 – FUNDAMENTALS OF PROGRAMMING


The sizeof()

ITE 12 – FUNDAMENTALS OF PROGRAMMING


sizeof()

o The sizeof()
 returns the size, in bytes, of a variable or data type

o The following are the byte size of each data type


 char = 1 byte

 int = 4 bytes

 float = 4 bytes

 double = 8 bytes

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Printing the sizes of each data type

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Using sizeof() to determine the size of the array

o We can use the sizeof() method to display the


 byte size of an array

 array size

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Displaying the byte size of an array

An integer is 4 bytes, we have 5 integer values stored in the array, thus we


have 20 bytes in total.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Displaying the length or 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.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Strings in C

o C does not support strings as a data type


 A string is a series of character
◦ Example: “Hello”, “Programming”, “programming101”

o To store strings in C, we use a word


 A word is an array of characters

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Declaring and assigning value to a character
array

o In declaring an array character, always add 1 to the size of the array


 If you want to store a string with a length of 5 characters, the size of your
array should be 6;
 The extra space is for the terminating character ( ‘\0’ )

 The terminating character signifies the end of the string

o You may or may not explicitly put the terminating character.


 The compiler automatically appends it at the end of your character array

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Declaring and assigning value to a character
array

Here, we explicitly
wrote the terminating
character

Here, we omitted it
and let the compiler do
the job.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Accessing values from a character array

What could have


happened if I didn’t add
one more space for the
terminator?

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Initializing a character array

o First method:
o No Array size, required terminating character at the end

o With Array size, not required terminating character at the end

Pwede ni

Pwede pd ni

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Initializing a character array

o Second method:

o This method can only be used in initialization. We cannot use it to

assign a string in a previously declared array.

We cannot do this
ITE 12 – FUNDAMENTALS OF PROGRAMMING
Types of Array
ONE DIMENSIONAL ARRAY
MULTI - DIMENSIONAL ARRAY

ITE 12 – FUNDAMENTALS OF PROGRAMMING


One Dimensional Arrays

o So far, what we’ve done are One Dimensional Array

o In a one dimensional array, the elements are accessed by a


single index.

o You can say the array is one dimensional if


 It has only one pair of brackets( [ ] ) upon declaration / initialization

 The elements are accessed using one index.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


One Dimensional Arrays

o You can say the array is one dimensional if


 It has only one pair of brackets( [ ] ) upon declaration / initialization

 The elements are accessed using one index.

o A one dimensional array can be represented by a single row of


cells / columns 12 34 43 65 10
ITE 12 – FUNDAMENTALS OF PROGRAMMING
0 1 2
0 3 4
Multi - Dimensional Arrays

o Array elements are accessed with two or more indices.


 If you have a two dimensional array, you need two indices.

 If you have a three dimensional array, you need three indices

 and so on.

o In this topic, we’ll focus on two dimensional arrays.

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Two Dimensional Arrays

o Two indices are used to access an array element.

o Think of it as a grid (or matrix) of numbers arranged row and


(1, 2)
0 1 2
column wise.
0
oOn the right is a two dimensional array
1
rows
 A 3 x 3 array
2
◦ 3 rows and 3 columns

columns
ITE 12 – FUNDAMENTALS OF PROGRAMMING
Declaring a two dimensional array

o In declaring a two dimensional array, you need to specify the


number of row and columns
 Below example is declaring an integer array named my2darray having 3

rows and 5 columns


0 1 2 3 4
rows columns

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Assigning value to elements of a 2D 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

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Accessing elements of a 2D array

o Let’s say, you want to print the value 35.


0 1 2 3 4

1 3
2
5
7
5
o How about 75?

ITE 12 – FUNDAMENTALS OF PROGRAMMING


Initializing a two dimensional array

o Let’s say, we want to initialize our array with the following


values in the right matrix
0 1 2 3 4

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

o Let’s say, we want to set the value of each element in our 2D


array to Zero using a loop.
0 1 2 3 4

0
0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0

ITE 12 – FUNDAMENTALS OF PROGRAMMING

You might also like