CH.5 ARRAYS IN C
-By Prof. Sangeeta Borde
� Program to take 5 values from the user and store
them in an array .Print the elements stored in the
array
� Program to find the average of n numbers using
arrays.
� Program to store n numbers in array and display
them in reverse order.
� Program to store n numbers in array and didplay
maximum of them.
EXAMPLES:
TYPES OF ARRAY
ONE DIMENSIONAL TWO DIMENSIONAL MULTIDIMENSIONAL
1. A One dimensional array has one subscript. One
dimensional array(1D) is an array which is
represented either in one row or in one column.
2. The one dimensional arrays are known as
vectors.
3. It is also known as single dimensional array.
1.ONE DIMENSIONAL ARRAY
Row
1 D
Array
1.ONE DIMENSIONAL ARRAY
continue…
Column
1.Declaration of 1 dimensional array.
2.Initialization of one dimensional array.
3.Accessing one dimensional array
1. An array consists of two subscripts is known as
array of the array.
2. In two dimensional array is divided into rows and
column. These are often known as array of the
array.
2.Two Dimensional Array
� Syntax:
� Datatype array_name[row_size] [column_size]
� Ex:int matrix[3][4];
Declaration of 2D array
� For example:-
int a[2][3]={0,0,0,1,1,1,1};
Initializes the elements of the first row to zero and
the second row to one.
The initialization is done row by row.
The above statement can also be written as:
Int a[2][3]={{0,0,0},{1,1,1}};
Initialization of two Dimensional
Array
� Q 1.program to store numbers in matrix and
display matrix.
Example for accessing two
dimensional array elements:
� 1.Row Major Representation-
� In this method the elements are stored row wise.
� i.e n elements of first row are stored in first n
locations, n elements of second row are stored in
next n locations and so on.
� i.e row1,row2,row3
Memory Representation of two dimensional
Array(Row Major and Column Major)
Mark
s[0][
0]
Mark
s[0[[
1]
Mark
s[0][
2]
Mark
s[1][
0]
Mark
s
[1][1]
Mark
s[1][
2]
Mark
s
[2][0]
Mark
s[2][
1]
Mark
s[2][
2]
1 2 3 4 5 6 7 8 9
1000 1002 1004 1006 1008 1008 1010 1012 1014
1.Row Major Representation-
Element
Value
Address
Mark
s[0][
0]
Mark
s[1][
0]
Mark
s[2][
0]
Mark
s[0][
1]
Mark
s[1][
1]
Mark
s[2][
1]
Mark
s[0][
2]
Mark
s[1][
2]
Mark
s[2][
2]
1 4 7 2 5 8 3 6 9
1000 1002 1004 1006 1008 1010 1012 1014 1016
2.Column Major Representation
Element
Value
Address
� Let A[m,n] is the two dimension array,To find address
of A[I,j] in row major order consider formula—
� Loc A[i][j]=BaseAddress(A)+w(N*i+j)
Where,
M-No. of rows,
N-No of columns
Base Address(A)=Starting address of the array.
i.e address of [0][0] element.
W=size of the data type used i.e no of bytes per storage
location for one element of the array.
Address Calculation of 2D of array
elements: Row Major Order
� Let A[m,n] is the two dimension array,To find address
of A[I,j] in row major order consider formula—
� Loc A[i][j]=BaseAddress(A)+w(M*j+i)
Where,
M-No. of rows,
N-No of columns
Base Address(A)=Starting address of the array.
i.e address of [0][0] element.
W=size of the data type used i.e no of bytes per storage
location for one element of the array.
Address Calculation of 2D of array
elements: Row Major Order
� In Row Major & Column Major
1. For the array marks[3][3] calculate the address of
marks[1][2] in row major and column major order.
Example:
� Arrays can have more than one dimension, these
arrays-of arrays are called multidimensional
arrays.
� Syntax:
Data_type name[size1][size2][size3]…[size4]
Example:
Three dimensional integer array:
Int threedim[2][5][3];
Multi-dimensional array
� Arrays are used in wide range of applications:
1.Arrays are used to store List of values.
2.Arrays are used to maintain multiple variables with
the same name.
3.Arrays are used to perform matrix operations.
4.Arrays are used to implement mathematical
vectors and matrices, as well as other kinds of
rectangular tables.
5.Arrays are used to implement search Algo’s
Array Applications:
� Arrays are used to implement sorting Algorithms:
� Insertion Sort, Selection Sort,Bubble Sort,Quick
sort,Merge sort etc.
� Arrays are used to implement Data Structures.
▪ Stack using arrays
▪ Queue using arrays
▪ Arrays are also used to implement CPU
scheduling Algorithms.
Array Applications: continue…
1. It is better and convenient way of storing the
data of same data type with same size.
2. It allows us to store known number of elements
in it.
3. Arrays allocate memory in contiguous memory
locations for all its elements. Hence there is no
chance of extra memory being allocated in case
of arrays.
4. This avoids memory overflow or shortage of
memory in arrays.
Advantages of an Arrays:
� In arrays, the elements can be accessed randomly
by using the index number. Iterating the arrays
using their index is faster compared to any other
methods like linked list etc.
� It allows to store the elements in any dimensional
array
Advantages of an Arrays:
continue…
1. Inserting and deleting the records from the array
would be costly since we add/delete the
elements from the array, We need to manage
space too.
2. Allocating more memory than the requirement
leads to wastage of memory space and less
allocation of memory also leads to a problem.
Disadvantages of Array:
� In C programming, there are two methods to pass
the array to the function namely, passing array
elements by elements and passing entire array to
function.
Passing Arrays to function.
� Passing array elements to a function is similar to
passing variables to a function.
� In this method, array elements are passed one by
one to function.
� The function gets access only one element at a
time and cannot modify this value.
Passing Arrays to function.
� Example:
Void display(int age1,int age2)
{
Printf(“%dn”,age1);
Printf(“%dn”,age2);
}
main()
{
int ageArray[]={20,40,60,12};
display(ageArray[1],ageArray[2]);
}
Passing Arrays to function
� To send entire array to the function ,we will only
send in the name of the array as argument, which
is nothing but the address of the starting element
of the array, or we can say the starting memory
address.
� Base address of the array is the address of the
very first element of the array.
� i.e address of the 0th element in linear array.
� So the array name marks is equivalent to
&marks[0].
� It is a constant pointer cannot be changed.
Passing entire array to function
THANK YOU

UNIT-5_Array in c_part1.pptx

  • 1.
    CH.5 ARRAYS INC -By Prof. Sangeeta Borde
  • 2.
    � Program totake 5 values from the user and store them in an array .Print the elements stored in the array � Program to find the average of n numbers using arrays. � Program to store n numbers in array and display them in reverse order. � Program to store n numbers in array and didplay maximum of them. EXAMPLES:
  • 3.
    TYPES OF ARRAY ONEDIMENSIONAL TWO DIMENSIONAL MULTIDIMENSIONAL
  • 4.
    1. A Onedimensional array has one subscript. One dimensional array(1D) is an array which is represented either in one row or in one column. 2. The one dimensional arrays are known as vectors. 3. It is also known as single dimensional array. 1.ONE DIMENSIONAL ARRAY Row 1 D Array
  • 5.
    1.ONE DIMENSIONAL ARRAY continue… Column 1.Declarationof 1 dimensional array. 2.Initialization of one dimensional array. 3.Accessing one dimensional array
  • 6.
    1. An arrayconsists of two subscripts is known as array of the array. 2. In two dimensional array is divided into rows and column. These are often known as array of the array. 2.Two Dimensional Array
  • 7.
    � Syntax: � Datatypearray_name[row_size] [column_size] � Ex:int matrix[3][4]; Declaration of 2D array
  • 8.
    � For example:- inta[2][3]={0,0,0,1,1,1,1}; Initializes the elements of the first row to zero and the second row to one. The initialization is done row by row. The above statement can also be written as: Int a[2][3]={{0,0,0},{1,1,1}}; Initialization of two Dimensional Array
  • 9.
    � Q 1.programto store numbers in matrix and display matrix. Example for accessing two dimensional array elements:
  • 10.
    � 1.Row MajorRepresentation- � In this method the elements are stored row wise. � i.e n elements of first row are stored in first n locations, n elements of second row are stored in next n locations and so on. � i.e row1,row2,row3 Memory Representation of two dimensional Array(Row Major and Column Major)
  • 11.
    Mark s[0][ 0] Mark s[0[[ 1] Mark s[0][ 2] Mark s[1][ 0] Mark s [1][1] Mark s[1][ 2] Mark s [2][0] Mark s[2][ 1] Mark s[2][ 2] 1 2 34 5 6 7 8 9 1000 1002 1004 1006 1008 1008 1010 1012 1014 1.Row Major Representation- Element Value Address
  • 12.
    Mark s[0][ 0] Mark s[1][ 0] Mark s[2][ 0] Mark s[0][ 1] Mark s[1][ 1] Mark s[2][ 1] Mark s[0][ 2] Mark s[1][ 2] Mark s[2][ 2] 1 4 72 5 8 3 6 9 1000 1002 1004 1006 1008 1010 1012 1014 1016 2.Column Major Representation Element Value Address
  • 13.
    � Let A[m,n]is the two dimension array,To find address of A[I,j] in row major order consider formula— � Loc A[i][j]=BaseAddress(A)+w(N*i+j) Where, M-No. of rows, N-No of columns Base Address(A)=Starting address of the array. i.e address of [0][0] element. W=size of the data type used i.e no of bytes per storage location for one element of the array. Address Calculation of 2D of array elements: Row Major Order
  • 14.
    � Let A[m,n]is the two dimension array,To find address of A[I,j] in row major order consider formula— � Loc A[i][j]=BaseAddress(A)+w(M*j+i) Where, M-No. of rows, N-No of columns Base Address(A)=Starting address of the array. i.e address of [0][0] element. W=size of the data type used i.e no of bytes per storage location for one element of the array. Address Calculation of 2D of array elements: Row Major Order
  • 15.
    � In RowMajor & Column Major 1. For the array marks[3][3] calculate the address of marks[1][2] in row major and column major order. Example:
  • 16.
    � Arrays canhave more than one dimension, these arrays-of arrays are called multidimensional arrays. � Syntax: Data_type name[size1][size2][size3]…[size4] Example: Three dimensional integer array: Int threedim[2][5][3]; Multi-dimensional array
  • 17.
    � Arrays areused in wide range of applications: 1.Arrays are used to store List of values. 2.Arrays are used to maintain multiple variables with the same name. 3.Arrays are used to perform matrix operations. 4.Arrays are used to implement mathematical vectors and matrices, as well as other kinds of rectangular tables. 5.Arrays are used to implement search Algo’s Array Applications:
  • 18.
    � Arrays areused to implement sorting Algorithms: � Insertion Sort, Selection Sort,Bubble Sort,Quick sort,Merge sort etc. � Arrays are used to implement Data Structures. ▪ Stack using arrays ▪ Queue using arrays ▪ Arrays are also used to implement CPU scheduling Algorithms. Array Applications: continue…
  • 19.
    1. It isbetter and convenient way of storing the data of same data type with same size. 2. It allows us to store known number of elements in it. 3. Arrays allocate memory in contiguous memory locations for all its elements. Hence there is no chance of extra memory being allocated in case of arrays. 4. This avoids memory overflow or shortage of memory in arrays. Advantages of an Arrays:
  • 20.
    � In arrays,the elements can be accessed randomly by using the index number. Iterating the arrays using their index is faster compared to any other methods like linked list etc. � It allows to store the elements in any dimensional array Advantages of an Arrays: continue…
  • 21.
    1. Inserting anddeleting the records from the array would be costly since we add/delete the elements from the array, We need to manage space too. 2. Allocating more memory than the requirement leads to wastage of memory space and less allocation of memory also leads to a problem. Disadvantages of Array:
  • 22.
    � In Cprogramming, there are two methods to pass the array to the function namely, passing array elements by elements and passing entire array to function. Passing Arrays to function.
  • 23.
    � Passing arrayelements to a function is similar to passing variables to a function. � In this method, array elements are passed one by one to function. � The function gets access only one element at a time and cannot modify this value. Passing Arrays to function.
  • 24.
    � Example: Void display(intage1,int age2) { Printf(“%dn”,age1); Printf(“%dn”,age2); } main() { int ageArray[]={20,40,60,12}; display(ageArray[1],ageArray[2]); } Passing Arrays to function
  • 25.
    � To sendentire array to the function ,we will only send in the name of the array as argument, which is nothing but the address of the starting element of the array, or we can say the starting memory address. � Base address of the array is the address of the very first element of the array. � i.e address of the 0th element in linear array. � So the array name marks is equivalent to &marks[0]. � It is a constant pointer cannot be changed. Passing entire array to function
  • 26.