0% found this document useful (0 votes)
10 views

Chap - 5 C Arrays

Uploaded by

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

Chap - 5 C Arrays

Uploaded by

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

Programming in Practical Engineering

Languages

Chapter 5 : C Arrays

BURKINA INSTITUTE OF TECHNOLOGY


Electrical Engineering
(E.E)

Academic year : 2024-2025

Semester 3

24 octobre 2024
Course outline

1 Arrays definition in C language

2 Access Array Elements

3 Traversal Array Elements

4 Arrays dimensions

5 Applications

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 2 / 40


Quote

A programming language is a convention for giving commands to a

computer. It’s not supposed to be obscure, weird and full of

subtle traps...

Dave Small

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 3 / 40


1. Arrays definition in C language

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 4 / 40


1. Arrays definition in C language

1.1. Introduction
An array in C is a fixed-size collection of similar data
items stored in contiguous memory locations.

It can be used to store the collection of primitive data


types such as int, char, float, etc., and also derived and
user-defined data types such as pointers, structures, etc.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 5 / 40


1. Arrays definition in C language

1.2. Advantages of Array in C


Random and fast access of elements using the array index.

Use of fewer lines of code as it creates a single array of


multiple elements.

Traversal through the array becomes easy using a single loop.

Sorting becomes easy as it can be accomplished by writing


fewer lines of code.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 6 / 40


1. Arrays definition in C language

1.3. Disadvantages of Array in C


Allows a fixed number of elements to be entered which is
decided at the time of declaration.

Unlike a linked list, an array in C is not dynamic.

Insertion and deletion of elements can be costly since the


elements are needed to be rearranged after insertion and
deletion.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 7 / 40


1. Arrays definition in C language

1.4. Explanation array

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 8 / 40


1. Arrays definition in C language

1.5. C Array Declaration


In C, we have to declare the array like any other variable
before using it.

We can declare an array by specifying its name, the type of


its elements, and the size of its dimensions.

When we declare an array in C, the compiler allocates the


memory block of the specified size to the array name.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 9 / 40


1. Arrays definition in C language
1.5.1. Syntax of Array Declaration
1 data_type array_name [ size ];
2

3 data_type array_name [ size1 ] [ size2 ]...[ sizeN ];

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 10 / 40


1. Arrays definition in C language
1.5.2. Example of Array Declaration
1 // C Program to illustrate the array declaration
2 # include < stdio .h >
3
4 int main () {
5 // declaring array of integers
6 int arr_int [5];
7 // declaring array of characters
8 char arr_char [5];
9
10 return 0;
11 }

The C arrays are static in nature, i.e., they are allocated


memory at the compile time.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 11 / 40


1. Arrays definition in C language

1.6. C Array Initialization


Initialization in C is the process to assign some initial
value to the variable.

So, we need to initialize the array to some meaningful value.

There are multiple ways to initialize an array in C :

Array initialization with declaration


Array initialization with declaration without size

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 12 / 40


1. Arrays definition in C language

1.6.1. Array Initialization with declaration


We initialize the array along with its declaration.

We use an initializer list to initialize multiple elements of


the array.

An initializer list is the list of values enclosed within


braces {} separated b a comma:

data_type array_name [size] = {value1, value2, ... valueN};

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 13 / 40


1. Arrays definition in C language

1.6.2. Array Initialization with declaration without size


If we initialize an array using an initializer list, we can
skip declaring the size of the array as the compiler can
automatically deduce the size of the array in these cases.

The size of the array in these cases is equal to the number


of elements present in the initializer list as the compiler
can automatically deduce the size of the array.

data_type array_name [ ] = {value1, value2, ... valueN};

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 14 / 40


1. Arrays definition in C language
1.6.3. Examples
1 # include < stdio .h >
2

3 int main () {
4 // array initialization using initialier list
5 int arr [5] = { 10 , 20 , 30 , 40 , 50 };
6

7 /*
8 array initialization using initializer list without
9 specifying size
10 */
11 int arr1 [ ] = { 1 , 2 , 3 , 4 , 5 };
12

13 return 0;
14 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 15 / 40


2. Access array elements

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 16 / 40


2. Access array elements

2.1. Definition
We can access any element of an array in C using the array
subscript operator [ ] and the index value i of the element.

array_name [index];

One thing to note is that the indexing in the array always


starts with 0

The first element is at index 0, the last element is at N – 1


where N is the number of elements in the array.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 17 / 40


2. Access array elements

2.2. Explanation

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 18 / 40


2. Access array elements
2.3. Examples
1 # include < stdio .h >
2
3 int main () {
4 // array declaration and initialization
5 int arr [5] = { 15 , 25 , 35 , 45 , 55 };
6
7 // accessing element at index 2 i . e 3 rd element
8 printf ( " Element at arr [2]: % d \ n " , arr [2]) ;
9
10 // accessing element at index 4 i . e last element
11 printf ( " Element at arr [4]: % d \ n " , arr [4]) ;
12
13 // accessing element at index 0 i . e first element
14 printf ( " Element at arr [0]: % d " , arr [0]) ;
15
16 return 0;
17 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 19 / 40


3. Traversal array elements

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 20 / 40


3. Traversal array elements

3.1. Definition
Traversal is the process in which we visit every element of
the data structure.

For C array traversal, we use loops to iterate through each


element of the array.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 21 / 40


3. Traversal array elements
3.2. Explanation

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 22 / 40


3. Traversal array elements

3.3. Array traversal using for Loop


1 for ( int i = 0; i < N ; i ++)
2 {
3 array_name [ i ];
4 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 23 / 40


3. Traversal array elements
3.4. Example
1 # include < stdio .h >
2
3 int main () {
4 // array declaration and initialization
5 int arr [5] = { 10 , 20 , 30 , 40 , 50 };
6
7 // modifying element at index 2
8 arr [2] = 100;
9
10 // traversing array using for loop
11 printf ( " Elements in Array : " ) ;
12 for ( int i = 0; i < 5; i ++) {
13 printf ( " % d " , arr [ i ]) ;
14 }
15 return 0;
16 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 24 / 40


4. Arrays dimensions

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 25 / 40


4. Arrays dimensions

4.1. Generalities
There are two types of arrays based on the number of
dimensions it has.

One Dimensional Arrays (1D Array )

Multidimensional Arrays nD Array

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 26 / 40


4. Arrays dimensions
4.2. One dimensional array in C
The One-dimensional arrays are those arrays that have only
one dimension.
Syntax of 1D Array in C
array_name [size];

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 27 / 40


4. Arrays dimensions
4.2.1. Example 1
1 # include < stdio .h >
2 int main () {
3 // 1 d array declaration
4 int arr [5];
5
6 // 1 d array initialization using for loop
7 for ( int i = 0; i < 5; i ++) {
8 arr [ i ] = i * i - 2 * i + 1;
9 }
10
11 printf ( " Elements of Array : " ) ;
12 // printing 1 d array by traversing using for loop
13 for ( int i = 0; i < 5; i ++) {
14 printf ( " % d " , arr [ i ]) ;
15 }
16 return 0;
17 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 28 / 40


4. Arrays dimensions
4.2.2. Example 2
1 # include < stdio .h >
2 int main () {
3 // creating array of character
4 char arr [7] = { ’B ’ , ’i ’ , ’t ’ , ’ ’ , ’E ’ , ’E ’ , ’ \0 ’ };
5

6 // printing string
7 int i = 0;
8 while ( arr [ i ]) {
9 printf ( " % c " , arr [ i ++]) ;
10 }
11 return 0;
12 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 29 / 40


4. Arrays dimensions

4.3. One dimensional array in C


Multi-dimensional Arrays in C are those arrays that have more
than one dimension.
Some of the popular multidimensional arrays are 2D arrays and
3D arrays.
We can declare arrays with more dimensions than 3d arrays but
they are avoided as they get very complex and occupy a large
amount of space.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 30 / 40


4. Arrays dimensions
4.3.1. Two dimensional array in C
A Two-Dimensional array is an array that has two dimensions.
They can be visualized in the form of rows and columns
organized in a two-dimensional plane.
Syntax of 2D Array in C
array_name [size1] [size2];

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 31 / 40


4. Arrays dimensions
4.3.2. Example
1 # include < stdio .h >
2 int main () {
3 // declaring and initializing 2 d array
4 int arr [2][3] = { 10 , 20 , 30 , 40 , 50 , 60 };
5
6 printf ( " 2 D Array :\ n " ) ;
7 // printing 2 d array
8 for ( int i = 0; i < 2; i ++) {
9 for ( int j = 0; j < 3; j ++) {
10 printf ( " % d " , arr [ i ][ j ]) ;
11 }
12 printf ( " \ n " ) ;
13 }
14
15 return 0;
16 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 32 / 40


4. Arrays dimensions

4.4.1. Three dimensional array in C


A 3D array has exactly three dimensions.
It can be visualized as a collection of 2D arrays stacked on
top of each other to create the third dimension.
Syntax of 3D Array in C
array_name [size1] [size2] [size3];

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 33 / 40


4. Arrays dimensions
4.4.2. Example
1 # include < stdio .h >
2 int main () {
3 // 3 D array declaration
4 int arr [2][2][2] = { 10 , 20 , 30 , 40 , 50 , 60 };
5
6 // printing elements
7 for ( int i = 0; i < 2; i ++) {
8 for ( int j = 0; j < 2; j ++) {
9 for ( int k = 0; k < 2; k ++) {
10 printf ( " % d " , arr [ i ][ j ][ k ]) ;
11 }
12 printf ( " \ n " ) ;
13 }
14 printf ( " \ n \ n " ) ;
15 }
16 return 0;
17 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 34 / 40


5. Applications

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 35 / 40


5. Arrays dimensions
5.1. Application 1
1 # include < stdio .h >
2 int main () {
3 // declaring an integer array
4 int arr [5];
5
6 // taking input to array elements one by one
7 for ( int i = 0; i < 5; i ++) {
8 scanf ( " % d " , & arr [ i ]) ;
9 }
10
11 // printing array elements
12 printf ( " Array Elements : " ) ;
13 for ( int i = 0; i < 5; i ++) {
14 printf ( " % d " , arr [ i ]) ;
15 }
16 return 0;
17 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 36 / 40


5. Arrays dimensions
5.2. Application 2

1 # include < stdio .h >


2 // function to calculate average of the function
3 float getAverage ( float * arr , int size ) {
4 int sum = 0;
5 // calculating cumulative sum of all the array elements
6 for ( int i = 0; i < size ; i ++) {
7 sum += arr [ i ];
8 }
9 return sum / size ; // returning average
10 }
11
12 int main () {
13 float arr [5] = { 10 , 20 , 30 , 40 , 50 };
14 // size of array using sizeof operator
15 int n = sizeof ( arr ) / sizeof ( float ) ;
16
17 printf ( " Array Elements : " ) ; // printing array elements
18 for ( int i = 0; i < n ; i ++) {
19 printf ( " %.0 f " , arr [ i ]) ;
20 }
21
22 // calling getAverage function and printing average
23 printf ( " \ nAverage : %.2 f " , getAverage ( arr , n ) ) ;
24 return 0;
25 }
(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 37 / 40
5. Arrays dimensions
5.3. Application 3
1 # include < stdio .h >
2 // function to return max value
3 int getMax ( int * arr , int size ) {
4 int max = arr [0];
5 for ( int i = 1; i < size ; i ++) {
6 if ( max < arr [ i ]) {
7 max = arr [ i ];
8 }
9 }
10 return max ;
11 }
12
13 int main () {
14 int arr [5] = { 135 , 15 , 1 , 19 , 51 };
15
16 printf ( " Largest Number in the Array : % d " ,
17 getMax ( arr , 5) ) ;
18
19 return 0;
20 }

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 38 / 40


Conclusion

4.3. One dimensional array in C


The array is one of the most used and important data
structures in C.

It is one of the core concepts of C language that is used in


every other program.

Though it is important to know about its limitation so that


we can take advantage of its functionality.

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 39 / 40


==END==

(B.i.t/E.E, S3) Engineering Practical Programming 24 octobre 2024 40 / 40

You might also like