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

C Array

C array
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C Array

C array
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

C Array

Why Do We Use Arrays?


• Suppose we want to store the marks of 10 students and find
the average.

• We declare 10 different variables to store 10 different


values.
C Array
• An array is defined as the collection of similar type of data
items stored at contiguous memory locations.

• C programming language which can store the primitive type


of data such as int, char, double, float, etc.

• It also has the capability to store the collection of derived


data types, such as pointers, structure, etc.
Properties of Array
• Each element of an array is of same data type and carries
the same size, i.e., int = 4 bytes.
• Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location.
• Elements of the array can be randomly accessed since we
can calculate the address of each element of the array with
the given base address and the size of the data element.
Advantage of C Array
• 1) Code Optimization: Less code to the access the data.

• 2) Ease of traversing: By using the for loop, we can retrieve the


elements of an array easily.

• 3) Ease of sorting: To sort the elements of the array, we need a


few lines of code only.

• 4) Random Access: We can access any element randomly using


the array.
Disadvantage of C Array

• Fixed Size: Whatever size, we define at the time of


declaration of the array, we can't exceed the limit.

• So, it doesn't grow the size dynamically like Linked


List which we will learn later.
How to declare an array?

dataType arrayName [arraySize];

int data[100];
float mark[5];
Initialization of C Array
Array Example
Types of Arrays

There are majorly three types of arrays:

One-dimensional array (1-D arrays)


Two-dimensional (2D) array
Three-dimensional array
Syntax of Single Dimensional Array
data_type array_name [array_size];
Finding the Average Using an Array
Declaring an Uninitialized Array
Initializing an Array in C
Example
Example
Size of Integer Array
Address of Array Elements
2. Two-dimensional (2D) array:

data_type array_name [sizeof_1st_dimension][sizeof_2nd_dimension];


column0 column1 column2
row0 1 4 2
row1 3 6 8
Change Elements in a 2D Array
Two-dimensional array example in C
Count Even and Odd Elements in Array
Matrix Addition
Print Negative Elements in Array
Find Maximum and Minimum Element in Array
Three-Dimensional Array in C
Syntax:

array_name[x][y][z]
Three-Dimensional Array
Thank You

You might also like