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

Array

The document provides an overview of arrays in programming, detailing their definition, types (one-dimensional and multi-dimensional), and how to declare, initialize, and access them. It also covers the advantages of using arrays, methods for reading user input, and passing arrays to functions. Additionally, it includes information on C strings and their initialization methods.

Uploaded by

aishdevkota6
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)
13 views25 pages

Array

The document provides an overview of arrays in programming, detailing their definition, types (one-dimensional and multi-dimensional), and how to declare, initialize, and access them. It also covers the advantages of using arrays, methods for reading user input, and passing arrays to functions. Additionally, it includes information on C strings and their initialization methods.

Uploaded by

aishdevkota6
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/ 25

Array

➢INTRODU CTION
➢ONE -DIMEN SION AL
ARRAY
➢MULTIDIMEN SION AL ARRAY
Introduction

 An array is a sequence of data items that are:


 all of the same type
a sequence of integers, or a sequence of characters, or a
sequence of floats, etc.
 indexible
 youcan indicate or select the first element or second
element or ...
 stored contiguously in memory
 each
element of the sequence is stored in memory
immediately after the previous element
Introduction

 An array is a variable that can store multiple values.


For example, if you want to store 50 integers,
you can create an array for it.
int data[50];

Array Declaration:
dataType arrayName[arraySize];
Example:
float marks[5];
Here an array, mark is declared of floating-point type
which size is 5. Meaning, it can hold 5 floating-point
values.
Introduction

Note:-

The size and type of an array cannot be


changed once it is declared.
Introduction

 Access Array Elements


 access elements of an array by indices.
float mark[5];

 NOTE :
- Arrays have 0 as the first index, not 1. In this example,
mark[0] is the first element.
- If the size of an array is n, to access the last element, the
n-1 index is used. In this example, mark[4]
- Let the starting address of mark[0] is 2120. Then, the
address of the mark[1] will be 2124 and so on. This is
because the size of a float is 4 bytes.
introduction

Initialization of an Array:
 It is possible to initialize an array during declaration
(at compile time). For example,
float mark[5] = {19, 10, 8, 17, 9};
Also, it can be initialized at run time as,
float mark[] = {19, 10, 8, 17, 9};

 Here, we haven't specified the size. However, the


compiler knows its size is 5 as we are initializing it
with 5 elements.
introduction

Initialization of an Array:
 Here, we haven't specified the size. However, the
compiler knows its size is 5 as we are initializing it
with 5 elements.

 mark[0] is equal to 19
 mark[1] is equal to 10
 mark[2] is equal to 8
 mark[3] is equal to 17
 mark[4] is equal to 9
introduction

Initialization of an Array:
 We can Change Value of Array elements:
int mark[5] = {19, 10, 8, 17, 9}

// make the value of the third element to -1


mark[2] = -1;

// make the value of the fifth element to 0


mark[4] = 0;
Advantage of Array

 Huge amount of data can be stored under single


variable name.
 Searching of data item is faster.
 2 dimension arrays are used to represent the
matrices.
 It is helpful in implementing other data structure
like linked list, queue, stack.
One dimensional Array

SYNTAX:
data-type name[index];
EXAMPLE:
int num[10];
Initialization

 int num[6]={2,4,6,7,8,12};

 Individual elements can also be initialize as:


 num[0]=2;
 num[1]=4;
 num[2]=6;
 num[3]=7;
 num[4]=8;
 num[5]=12;
Reading Data from User

 for loop is used to read data from the user.


Class work

 WAP to read 10 numbers from the user and display


them.

 WAP to read 20 numbers from the user and find out


the highest number.
Multi-dimensional Array

 In multi-dimensional we focus on the two


dimensional array.
 also, called array of array.
Declaration of 2D array:
data-type ArrayName[row-size][column-size];

EXAMPLE:
int a[3][3];
Multi-dimensional Array

Size of Multidimensional Arrays


 The total number of elements that can be stored in a
multidimensional array can be calculated by
multiplying the size of both dimensions.
Example: int arr[5][4]
 The array int arr[5][4] can store total of (5*4) =
20 elements.
Multi-dimensional Array

Initialization of 2D Arrays
 by using a list of values enclosed inside ‘{ }’ and
separated by a comma as shown in the example
below:
int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 1 , 7}
or
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 1,7}};
 Elements will be stored in the array from left to right
and top to bottom So, the first 4 elements from the left
will be filled in the first row, the next 4 elements in the
second row, and so on.
Multi-dimensional Array

2D Array Traversal
 Traversal means accessing all the elements of the
array one by one.
 2D array elements are accessed using row indexes and
column indexes inside array subscript operator [].
arr_name[i][j], i and j are row and column index.
 Two loops will be used to go through the entire array.
one to access each row from top to bottom and the
other to access each element in the current row from
left to right
Multi-dimensional Array

2D Array Example
Reading Data from the user

 Nested for Loop is used.


Class Work

 WAP to read a 3*3 matrix from the user and display


it in the appropriate format.
 WAP to read two 3*3 matrices from the user and
display their sum.
Passing arrays to Function

 Like ordinary variables and values, it’s possible to


pass the value of an array element and even the entire
array as an argument to a function.
 To pass an entire array to a function, array must
appear by itself without brackets as an actual
argument in function call statement.
 The size of the array is not declared within the formal
argument declaration.
Syntax for function call passing array as an argument:
Function_name(array_name);
Passing arrays to Function

 Syntax for function prototype which accepts array:


return_type FunctionName(dataType, arrayName[]);
 When an array is passed to a function, the value of an
array elements are not passed to the function. Rather,
array name is interpreted as address of first element
of array.
 This address is assigned to corresponding formal
argument when the function is called.
 The formal argument therefore becomes a pointer to a
first array element, Thus, array is passed using call by
reference.
Passing arrays to Function

 Examples:
Strings

 A String in C programming is a sequence of characters


terminated with a null character ‘\0’.
 The C String is stored as an array of characters.
 The terminating \0 is important because it’s the way
string handling function knows where the function
ends.
C String Declaration Syntax
char stringName[size];
Or
char stringName[] =“”;
Strings

We can initialize a C string in 4 different ways which are as follows:


1. Assigning a String Literal without Size
char str[] = "Hello";
2. Assigning a String Literal with a Predefined Size
 If we want to store a string of size n then we should always declare
a string with a size equal to or greater than n+1.
 One extra space which will be assigned to the null character.
char str[10] = "Hello";
3. Assigning Character by Character with Size
char str[10] = {'H','e','l','l','o','\0'};
4. Assigning Character by Character without Size
 The size of the string is determined by the compiler automatically.
char str[] = {'H','e','l','l','o','\0'};

You might also like