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

Array_presentation (1)

The document provides an overview of arrays in C programming, detailing their declaration, initialization, and access methods. It explains the types of arrays, including one-dimensional and multidimensional arrays, along with examples of how to declare, initialize, and traverse them. The content emphasizes the importance of arrays for managing multiple values efficiently in C.

Uploaded by

hashijannat1000
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)
4 views

Array_presentation (1)

The document provides an overview of arrays in C programming, detailing their declaration, initialization, and access methods. It explains the types of arrays, including one-dimensional and multidimensional arrays, along with examples of how to declare, initialize, and traverse them. The content emphasizes the importance of arrays for managing multiple values efficiently in C.

Uploaded by

hashijannat1000
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/ 12

1/31/2025

Array

Array in C is one of the most used data structures in C programming. It is


a simple and fast way of storing multiple values under a single name.

In C programming, an array is a collection of elements of the same


data type stored in contiguous memory locations. Arrays allow you to
store multiple values under a single variable name, making it easier to
manage and manipulate large sets of related data.

1
1/31/2025

Array Declaration

In C, an array must be declared before use. To declare an array, specify its


name, the type of elements it will store, and its size. When an array is
declared, the compiler reserves a block of memory to hold the specified
number of elements.

2
1/31/2025

The C arrays are static in nature, i.e., they are allocated memory at the
compile time

// C Program to illustrate the array declaration


#include <stdio.h>

int main()
{

// declaring array of integers


int arr_int[5];
// declaring array of characters
char arr_char[5];

return 0;
}

3
1/31/2025

Initialization or input

Initialization in C is the process of assigning initial values to a


variable. When an array is declared or memory is allocated for it,
its elements contain garbage values. To avoid this, we initialize
the array with meaningful values. In C, arrays can be initialized in
several ways.

4
1/31/2025

Array Initialization with Declaration


In this method, 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."

Array Initialization without Specifying Size


When using an initializer list, you don’t need to specify the size of the array.
The compiler automatically calculates the size based on the number of
elements in the initializer list. This makes the size of the array equal to the
number of values provided.

The size of the above arrays is 5 which is automatically deduced by the


compiler.

5
1/31/2025

Array Initialization after Declaration (Using Loops)


After declaring an array, you can initialize its elements one by one. This is
done by assigning values to each element individually. You can use a for loop,
while loop, or do-while loop to set values for all elements in the array.

for (int i = 0; i < N; i++)


{
array_name[i] = valuei;
}

// C Program to demonstrate array initialization


#include <stdio.h>

int main()
{
// array initialization using initialization list
int arr[5] = { 10, 20, 30, 40, 50 };
// array initialization using initializer list without specifying size
int arr1[] = { 1, 2, 3, 4, 5 };
// array initialization using for loop
float arr2[5];
for (int i = 0; i < 5; i++) {
arr2[i] = (float)i * 2.1;
}
return 0;
}

6
1/31/2025

Output

Accessing Array Elements


In C, you can access any element of an array using the square brackets [ ]
along with the index of the element. The index tells the position of the
element in the array.

One thing to note is that the


indexing in the array always
starts with 0, i.e., the first
element is at index 0 and the
last element is at N – 1 where
N is the number of elements
in the array.

7
1/31/2025

// C Program to illustrate element access


using array
// subscript
#include <stdio.h>
int main()
{
int arr[5] = { 15, 25, 35, 45, 55 };
printf("Element at arr[2]: %d\n", arr[2]);
printf("Element at arr[4]: %d\n", arr[4]);
printf("Element at arr[0]: %d", arr[0]);
return 0;
}

Updating Array Elements


To update the value of an array element, use the square brackets [ ] with the
index of the element and the assignment operator =. This works the same
way as accessing an element.

8
1/31/2025

C Array Traversal

Traversal means visiting each element of a data structure one by one. In C,


we use loops like for, while, or do-while to go through every element of
an array.

for (int i = 0; i < N; i++)


{
array_name[i];
}

// C Program to demonstrate the use of array


#include <stdio.h>

int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// modifying element at index 2
arr[2] = 100;
// traversing array using for loop
printf("Elements in Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}

9
1/31/2025

Types of Array in C
There are two types of arrays based on the number of dimensions it
has. They are as follows:

>>One Dimensional Arrays (1D Array)


>>Multidimensional Arrays

One-Dimensional Array in C

A one-dimensional array, or 1-D array, in C is an array that has only one


dimension. It stores a list of elements in a single row or line.
Syntax of 1D Array in C
array_name [size];

10
1/31/2025

// C Program to illustrate the use of 1D array


#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
// 1d array initialization using for loop
for (int i = 0; i < 5; i++) {
arr[i] = i * i - 2 * i + 1;
}
printf("Elements of Array: ");
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Multidimensional Array in C

Multidimensional arrays in C are arrays with more than one dimension.


Common examples are 2D arrays (which can be thought of as tables)
and 3D arrays. While you can declare arrays with more than three
dimensions, it's generally avoided because they become very complex
and use a lot of memory

11
1/31/2025

Two-Dimensional Array in C

A two-dimensional array, or 2D array, in C is an array with two dimensions.


It can be visualized as a table or grid with rows and columns, organized in a
two-dimensional plane.

Here,

size1: Size of the first dimension.


size2: Size of the second dimension.

// C Program to illustrate 2d array


#include <stdio.h>
int main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}

12

You might also like