0% found this document useful (0 votes)
27 views31 pages

UNIT-3 Arrays

The document discusses different types of arrays in C programming language. It explains one dimensional and multi dimensional arrays. It provides examples of declaring, initializing and accessing elements of one dimensional, two dimensional and three dimensional arrays.

Uploaded by

padhmasri2006
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)
27 views31 pages

UNIT-3 Arrays

The document discusses different types of arrays in C programming language. It explains one dimensional and multi dimensional arrays. It provides examples of declaring, initializing and accessing elements of one dimensional, two dimensional and three dimensional arrays.

Uploaded by

padhmasri2006
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/ 31

Unit –III

Arrays:-
• Introduction to arrays
• one dimensional arrays
• Two dimensional arrays
• Multi dimensional arrays
• Sample Programs
• An array is defined as the collection of similar
type of data items stored at contiguous memory
locations.
• Arrays are the derived data type in 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.
• The array is the simplest data structure where
each data element can be randomly accessed by
using its index number.
For example,
• if we want to store the marks of a
student in 6 subjects, then we don't
need to define different variables for
the marks in the different subject.
• Instead of that, we can define an
array which can store the marks in
each subject at the contiguous
memory locations.
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

1) 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 LinkedList which we
will learn later.
Declaration of C Array
• Declare the array( like any other variable before using it) 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.

Syntax of Array Declaration

data_type array_name [size];


or

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

where N is the number of dimensions.


C arrays are static in nature, i.e., they are allocated memory
at the compile time.
Example of Array Declaration

// declaring array of integers


int arr_int[5];

// declaring array of characters


char arr_char[5];
Access Array Elements

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, 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.
Array Initialization

• Initialization in C is the process to assign some initial


value to the variable.
• When the array is declared or allocated memory,
the elements of the array contain some garbage
value. So, we need to initialize the array to some
meaningful value.
• There are multiple ways in which we can initialize
an array in C.
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 by a comma.

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


The Index Values Starts from 0 to N-1, where
N is the size of the Array
Example:

#include<stdio.h>
int main()
{
int i=0;
int marks[5]; //declaration of array

//initialization of array
marks[0]=80;
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
//end of for loop
return 0;
} Output

80
60
70
85
75
2. Array Initialization with Declaration without Size

• 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

The size of the above arrays is 5 which is automatically


deduced by the compiler.

data_type array_name[] = {1,2,3,4,5};


Example:
#include<stdio.h>
int main()
{
int i=0;
Output
//declaration and initialization of array
int marks[5]={20,30,40,50,60}; 20
30
40
//traversal of array 50
for(i=0;i<5;i++) 60
{
printf("%d \n",marks[i]);
}
return 0;
}
3. Array Initialization after Declaration (Using
Loops)

• initialize the array after the declaration by assigning the


initial value to each element individually.
• We can use for loop, while loop, or do-while loop to
assign the value to each element of the array.

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


array_name[i] = valuei;
}
Example

//Program To read and print Array Elelements

int main()
{
int arr[5],i;

//reading Array Elements


printf(“Enter Array Elelements:”);

for(i=0;i<5;i++)
scanf(“%d”,arr[i]);
//Printng Array Elements
printf(“The Given Array elements are:”);

for(i=0;i<5;i++)
Printf(“%d\n”,arr[i]);

return 0;
}
Types of Array in C

There are two types of arrays based on


the number of dimensions it has.

• One Dimensional Arrays (1D Array)


• Multidimensional Arrays
1. One Dimensional Array in C

The One-dimensional arrays, also known as 1-D arrays,


and have only one dimension.

Syntax

array_name [size];
Example
int arr[5];
char status[10];
2. Multidimensional 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.
A. Two-Dimensional Array in C

• A Two-Dimensional array or 2D array in C is an array that


has exactly two dimensions.
• They can be visualized in the form of rows and columns
organized in a two-dimensional plane.

Syntax

array_name[size1] [size2];
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"); Output


// printing 2d array
for (int i = 0; i < 2; i++) { 2D Array:
for (int j = 0; j < 3; j++) { 10 20 30
printf("%d ",arr[i][j]); 40 50 60
}
printf("\n");
}

return 0;
}
B. Three-Dimensional Array in C

• Another popular form of a multi-dimensional array is Three


Dimensional Array or 3D Array.
• 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

array_name [size1] [size2] [size3];


// C Program to illustrate the 3d array
#include <stdio.h>

int main()
{

// 3D array declaration
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60 };

// printing elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n \n");
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy