0% found this document useful (0 votes)
3 views29 pages

Unit III Array and Types of Array pointers

The document provides an overview of arrays, defining them as fixed-size collections of elements of the same data type, and discusses their properties, advantages, and disadvantages. It covers one-dimensional and two-dimensional arrays, including their declaration, initialization, and methods for accessing elements. Additionally, it includes examples and syntax for programming with arrays in C, emphasizing their use in storing and manipulating data.

Uploaded by

thejegan31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views29 pages

Unit III Array and Types of Array pointers

The document provides an overview of arrays, defining them as fixed-size collections of elements of the same data type, and discusses their properties, advantages, and disadvantages. It covers one-dimensional and two-dimensional arrays, including their declaration, initialization, and methods for accessing elements. Additionally, it includes examples and syntax for programming with arrays in C, emphasizing their use in storing and manipulating data.

Uploaded by

thejegan31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

Arrays

ARRAYS
• An array is a fixed size sequenced collection of elements of
the same data type.

• A data structure used for storage of a collection of data items


that are all the same type.

• It is simplest form, can be used to represent a list of


numbers, names, etc..

• Examples

List of employees in an organization.

Test scores of a class of students.


Properties of Array
• The array contains the following properties.

• Each element of an array is of same data type and


carries the same size,

• 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.
Advantages of 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.
Introduction

• An array is a collection of similar data elements.


• These data elements have the same data type.
• The elements of the array are stored in consecutive memory locations and
are referenced by an index (also known as the subscript).
• Declaring an array means specifying three things:
Data type - what kind of values it can store. For example, int, char, float
Name - to identify the array
Size- the maximum number of values that the array can hold

marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]

1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
element element element element element element element element element element

• Arrays are declared using the following syntax.


type name[size];
Accessing Elements of an Array

• To access all the elements of the array, you must use a loop.
That is, we can access all the elements of the array by
varying the value of the subscript into the array.

• But note that the subscript must be an integral value or an


expression that evaluates to an integral value.

int i, marks[10];
for(i=0;i<10;i++)
marks[i] =20;
Types of Arrays

• One-dimensional arrays

• Two-dimensional arrays

• Multidimensional arrays
One Dimensional Array

• A list of items can be given one variable name using


only one subscript and such variable is called a single-
subscripted variable.
• Syntax:
data-type array-name [size];

• Example: int a[10];


char name[10];
double temperature [5];

temperature [0] 12.3

temperature [1] 7.5

temperature [2] 65.0

temperature [3] 72.1

temperature [4] 87.5


Calculating the Address of Array Elements

Address of data element, A[k] = BA(A) + w( k – lower_bound)


Here
A is the array
k is the index of the element of which we have to calculate the address
BA is the base address of the array A
w is the word size of one element in memory, for example, size of int is 2

99 67 78 56 88 90 34 85

Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]


1000 1002 1004 1006 1008 1010 1012 1014

Address(Marks[4]) = 1000 + 2(4 – 0)


= 1000 + 2(4) = 1008

© Oxford University Press 2018. All rights reserved.


Calculating the Length of an Array

Length = upper_bound – lower_bound + 1


Where upper_bound is the index of the last element
and lower_bound is the index of the first element in the array

99 67 78 56 88 90 34 85

Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]]

Here,
lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8

© Oxford University Press 2018. All rights reserved.


One Dimensional Array

• An array can be initialized at either of the following

• At Compile time

• At run time
One Dimensional Array

Compile Time Initialization:

• syntax:

• type array-name[size]={list of values};

• Values are separated by commas.

• Ex:

• int total[5]={1,2,3,4,5};
Write a Program to Read and Display N Numbers Using an
Array

#include<stdio.h> for(i=0;i<n;i++)

#include<conio.h> {

int main() printf(“\n Arr[%d] = ”, i);

{ scanf(“%d”,&arr[i]);
}
int i=0, n, arr[20];
printf(“\n The array elements are ”);
clrscr();
for(i=0;i<n;i++)
printf(“\n Enter the number of elements : ”);
{
scanf(“%d”, &n);
printf(“Arr[%d] = %d\t”, i, arr[i]);
}
return 0;
}
Two Dimensional Arrays
Multi Dimensional Array

• An array of arrays is called as multi dimensional array. In simple words, an


array created with more than one dimension (size) is called as multi
dimensional array. Multi dimensional array can be of two dimensional
array or three dimensional array or four dimensional array or more...

Most popular and commonly used multi dimensional array is two


dimensional array. The 2-D arrays are used to store data in the form of
table. We also use 2-D arrays to create mathematical matrices.
Two dimensional arrays

• At times we need to store the data in form of tables or matrices. For this, we can
use the two dimensional arrays.

• This array is specified by using two subscripts where one subscript is denoted as
the row and the other as the column.

• It is also viewed as an array of arrays.


• A 2D array is treated as a collection of 1D arrays. Each row of a 2D array
• corresponds to a 1D array consisting of n elements, where n is the
number of columns.
Declaration of Two Dimensional Array

• We use the following general syntax for declaring a two


dimensional array...

datatype arrayName [ rowSize ] [ columnSize ] ;

Example Code

int matrix_A [2][3] ;

• The above declaration of two dimensional array reserves 6


continuous memory locations of 2 bytes each in the form of 2
rows and 3 columns
• There are two ways of storing a two-dimensional array in the memory. The
first way is the row major order and the second is the column major order.

• Let us see how the elements of a 2D array are stored in a row major
order. Here, the elements of the first row are stored before the elements of
the second and third rows
• when we store the elements in a column major order, the elements of the first
column are stored before the elements of the second and third column.
Initialization of Two Dimensional Array
We use the following general syntax for declaring and initializing a two dimensional array
with specific number of rows and coloumns with initial values.
datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ;
Example Code
int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
The above declaration of two-dimensional array reserves 6 contiguous memory locations
of 2 bytes each in the form of 2 rows and 3 columns. And the first row is initialized with
values 1, 2 & 3 and second row is initialized with values 4, 5 & 6.
We can also initialize as follows...
Example Code
int matrix_A [2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Example: Initialization of 2D array
int score [3] [2] ={50, 60, 70, 95, 3, 36};

The initialization of the array is done row-by-row.

The above can also be written in the following manner:


int score [3] [2] ={{50, 60} , {70,95} ,{3,36}};

The elements will be seen in the following format once they are initialized.

score[0][0] = 50 score [0][1] = 60


score[1][0] = 70 score [1][1] = 95
score[2][0] = 3 score [2][1] = 36

The above example has been divided into three rows and two columns.
• The declaration statement given below is valid.

int marks[][3]={{90,87,78},{68, 62, 71}};

• In order to initialize the entire two-dimensional array to zeros, simply


specify the first value as zero. That is,

int marks[2][3] = {0};


Accessing Individual Elements of Two Dimensional
Array

In a c programming language, to access elements of a two-dimensional array we use array name followed by row index value and column
index value of the element that to be accessed. Here the row and column index values must be enclosed in separate square braces. In
case of the two-dimensional array the compiler assigns separate index values for rows and columns.

We use the following general syntax to access the individual elements of a two-dimensional array...

arrayName [ rowIndex ] [ columnIndex ]

Example Code

matrix_A [0][1] = 10 ;

In the above statement, the element with row index 0 and column index 1 of matrix_A array is assigned with value 10.

.
Simple program for printing the elements of 2D array.
#include <stdio.h>
void main()
{
int score[3][2]= {10,20,30,40,50,60};
int i,j;
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d\t",score[i][j]);
}
}
}
In a small company there are five salesmen. Each salesman is supposed to sell three
products. Write a program using a 2D array to print (i) the total sales by each salesman
and (ii) total sales of each item.

You might also like