Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

ARRAY

By Debraj Chatterjee
What is an array ?
• An array is a variable that can store multiple values. For
example, if you want to store 100 integers, you can create an
array for it.
• Array is a collection of homogeneous elements.
Cont…

• An array is a fixed-size sequential collection of elements of same


data types that share a
• common name.
• It is simply a group of data types.
• An array is a derived data type.
• An array is used to represent a list of numbers, or a list of names.
Example:
1. List of employees in an organization.
2. Test scores of a class of students.
3. List of customers and their telephone numbers.
4. List of students in the college.
Types of Array :
1. One-dimensional arrays
A variable which represent the list of items using only one index
(subscript) is called one-dimensional array. For Example , if we want
to represent a set of five numbers say(35,40,20,57,19), by an array
variable number, then number is declared as follows int number [5] ;
and the computer store these numbers as shown below :
number [0] number [1] number [2] number [3] number [4].
The values can be assigned to the array as follows :
number [0] = 35;
number [1] = 40;
number [2] = 20;
number [3] = 57;
number [4] = 19;
How to declare and initialize an array?
• dataType arrayName [arraySize];
• For example,
• float mark[5];
• Here, we declared an array, mark, of floating-point type. And its
size is 5. Meaning, it can hold 5 floating-point values.
• It's important to note that the size and type of an array cannot be
changed once it is declared.
• It is possible to initialize an array during declaration. For example,
• int mark[5] = {19, 10, 8, 17, 9};You can also initialize an array like
this.
• int 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.
How to take Input and print Output Array
Elements?
Input
//Take input and store it in the 3rd element
​scanf("%d", &mark[2]);
// Take input and store it in the ith element
scanf("%d", &mark[i-1]);
Output
// print the first element of the array
printf("%d", mark[0]);
// print the third element of the array
printf("%d", mark[2]);
// print ith element of the array
printf("%d", mark[i-1]);
Concept used for initializing large arrays.
Example:
for(i=0; i < 100; i++)
{
if( i < 50)
sum[i] = 0.0;
else
sum[i] = 1.0;
}
Here first 50 elements of the array sum are initialized to 0 and
the remaining 50 elements are initialized to 1 at run time.
2. Two-dimensional arrays
A variable which represent the list of items using two index (subscript) is
called two-dimensional array.
In Two dimensional arrays, the data is stored in rows and columns format.
For example: int table[2][3];
• In C programming, you can create an array of arrays. These arrays are
known as multidimensional arrays. For example,
• float x[3][4];
• Here, x is a two-dimensional (2d) array. The array can hold 12 elements.
You can think the array as a table with 3 rows and each row has 4
columns.
• Initialization of a 2d array
• // Different ways to initialize two-dimensional array
• int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
• int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
• int c[2][3] = {1, 3, 0, -1, 5, 9};
Contd…
In Two dimensional arrays, the data is stored in rows and columns
format.
For example:
int table[2][3] = {1,2,3,4,5,6};
The memory layout of above example :
table[0][0] = 1;
table[0][1] = 2 ;
table[0][2] = 3;
table[1][0] = 4;
table[1][1] = 5;
table[1][2] = 6;
3. Multidimensional arrays

You can initialize a three-dimensional array in a similar way like a two-dimensional


array.
Here's an example,
int test[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5},

{3, 1, 4, 9}}};
Row major & column major order
• In computing, row-major order and column-major order are
methods for storing multidimensional arrays in linear storage such
as random access memory.
• The difference between the orders lies in which elements of an
array are contiguous in memory.
• In row-major order, the consecutive elements of a row reside next
to each other,
• Whereas the same holds true for consecutive elements of a column
in column-major order.
Problem:
Calculate the address of an element from an array [20X5] with a
given location [18X4].
Prerequisites: Base Address=1000;
No of words/memory location=2;
A[I][J]= BA+{I*N+J}*SIZE OF DATA TYPE
A[18][4]= 1000+{18*5+4}*2
=1000+94*2
=1000+188=1188

• Address of [I,J][I,J]th element in row major order =B+W[n(I−Lr)+


[J−Lc]]=B+W[n(I−Lr)+[J−Lc]]
• where B denotes base address, W denotes element size in bytes, n
is the number of columns; Lr is the first row number, Lc is the first
column number.
Applications:
• Using pointers for accessing arrays.
• Passing arrays as function parameters.
• Arrays as members of structures.
• Using structure type data as array elements.
• Arrays as dynamic data structures.
• Manipulating character arrays and strings.
Programs you should learn

A. Addition of two matrices


B. Multiplication of two matrices
C. Transpose of a matrix

You might also like