SlideShare a Scribd company logo
REF. BOOK PROGRAMMING IN ANSI C-
7TH EDITION
E. BALAGURUSAMY
1
2
LEARNING OBJECTIVES:
Define the concept of
arrays
Determine how one-
dimensional array is
declared and initialized
Know the concept of
two-dimensional arrays
Discuss how two-
dimensional array is
declared and initialized
Describe multi-
dimensional arrays
Explain dynamic arrays 3
INTRODUCTION:
NORMALLY ONE VARIABLE OF ONE DATATYPE CAN HOLD ONLY
ONE VALUE
4
4
What is array?
An array is a fixed size sequenced collection of elements of the SAME
datatype.
Syntax to declare array:
datatype variable-name [size];
5
5
We don’t need to declare values for
elements. Like below:
6
6
One-dimensional:
Just by declaring one variable we can put an INDEX number which is merely the number variables.
We can index starting with 0 which is preferred by computer.
But we shouldn’t get confused so we can state indexing with 1.
7
7
i
OR,
i
8
8
Compile Time Initialization:
We can initialize the elements of array in same way as
the ordinary variable when they are declared.
Datatype array-name [size] = {list of values}
Int number[3] = {0, 1, 2};
We can omit the size during compile time initialization
only.
int number[ ] = {1, 2, 3, 4};
This approach works fine as long as we initialize every
element in the array. 9
9
Character array Initialization :
char name[ ] = {‘J’,ʻe’,ʻn’,ʻc’,ʻy’,ʻ0’};
or,
char name[ ]=ʺJency”;
Compile time initialization may be partial. That is, the number of initialize may be
less than the declared size.
int number[5]={10, 20, 30};
Here, array index number initialized is 5 but there are 3 elements.
The remaining 2 places are Zero and if the array type is char the Null.
int number[2] = {1,2,3,4};
In this case the declared size has more initialized elements. The compiler will
create error. 10
Run time initialization:
An array can be Explicitly initialized at run time. This approach is usually applied for
initializing user input data. Using for loop can help in this case.
11
11
We can’t leave size empty in array
12
12
Searching And Sorting:
Sorting: The process of arranging elements in the list according to their values, in ascending or
descending order. A sorted list is called an ordered list. Sorted lists are especially important in list
searching because they facilitate rapid search operation.
Important and simple sorting techniques:
 Bubble sort
 Selection sort
 Insertion sort
Searching: The process of finding the location of the specific element in a list. The specified element is often
called the search key. If the search key with list element values, the search is said to be successful else
unsuccessful.
The most commonly used search techniques are:
 Sequential Search
 Binary Search
 Shell sort
 Merge sort
 Quick sort
13
13
TWO-DIMENSIONAL ARRAYS:
We represent a particular value in matrix using 2 subscripts such as vrc here, r is for row
and c is for column.
Syntax:
datatype array_name[row_size][column_size];
Like single-dimensional arrays, each dimension of the array is indexed from zero to its maximum size-1.
The first index selects the row and the second index selects the column within row.
14
14
2D Array Compile time initialization:
Array size declaration and values initialized in braces:
int table[2][3]={0, 0, 0, 1, 1, 1};
OR,
int table[2][3] = {{0, 0, 0},{1, 1, 1} };
OR,
int table[2][3] = { {0, 0, 0},
{1, 1, 1}
};
The initialization is done
row by row
We can initialize a two
dimensional array in the
form of matrix
15
15
OR,
int table[ ][3] = {
{0, 0, 0},
{1, 1, 1}
};
*****************************
int table[2][3] = {
{1,2},
{2}
};
*****************************
int table[2][3] = {{0}, {0}, {0}};
or,
int table[2][3] = {0, 0};
When the array is completely
initialized with all values, explicitly,
we need not specify the size of the
dimension
If the values are missing in
initialize, they are
automatically set to Zero
When all the elements are
to be initialized to Zero, this
short-cut may be used 16
16
#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ ){
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %dn", i, j, a[i][j] );
}
}
return 0;
} 17
17
2D matrix Run time Input and Display:
#include<stdio.h>
#define MAX 10
int main()
{
int array[MAX][MAX],i, j, r, c;
printf("Enter row and column number:n");
scanf("%d %d", &r, &c);
printf("Enter %d X %d elements:n", r, c);
for(i = 0; i <r; i++)
{
for(j=0;j<c; j++)
{
printf("Enter array[%d][%d]: ",i+1,j+1);
scanf("%d", &array[i][j]);
}
}
printf("Your entered 2D matrix of %dX%d
elements:n", r, c);
for(i=0;i<r; i++)
{
for(j=0;j<c; j++)
{
printf("%5d", array[i][j]);
}
printf("n");
}
return 0;
}
18
18
19
19
MULTI-DIMENSIONAL ARRAYS:
C allows three or more dimensions.
The exact limit is determined by the compiler. It's an array or collection of 2D arrays, and
a 2D array is an array of 1D array.
The general form of a multi-dimensional array is
datatype arrary_name[s1][s2]…..[si];
here si is size of i-th dimension.
Examples:
int survey[3][5][12]; >>holds: 3*5*12=180 integer type elements<<
20
20
Declaration and Initialization 3D Array :
#include<stdio.h>
int main()
{
int i, j, k;
int arr[3][3][3]=
{
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
},
};
printf(":::3D Array Elements:::nn");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%dt", arr[i][j][k]);
}
printf("n");
}
printf("n");
}
return 0;
}
21
21
22
22
Dynamic Array:
We create arrays at compile time. An array created at compile
time by specifying SIZE in the source code has a fixed size and
cannot be modified at run time. The process of allocating
memory at compile time is known as Static Memory Allocation.
Considering a situation where we want to use array that can vary
greatly in size. In C it is possible to allocate memory to array at
run time are called Dynamic arrays.
Dynamic arrays are created using what are known as pointer
variables and memory management function malloc, calloc and
realloc. These functions are included in header file <stdlib.h>.
These are used in data structure such as linked lists, stacks and
queues. 23
THANK YOU!!!!!!
FOR YOUR PATIENCE
JENCY

More Related Content

PPTX
Recursion in Data Structure
PPT
Data Structures- Part5 recursion
PPTX
Hash table in data structure and algorithm
PPTX
Doubly Linked List
PPTX
Terminology of tree
PPTX
Red black trees
PPTX
PPT
Recursion in Data Structure
Data Structures- Part5 recursion
Hash table in data structure and algorithm
Doubly Linked List
Terminology of tree
Red black trees

What's hot (20)

PDF
Array data structure
PPTX
heap Sort Algorithm
PPTX
Binary Search Tree
PDF
sparse matrix in data structure
PPTX
Priority queue in DSA
PPTX
Arrays in Data Structure and Algorithm
PPTX
Signed Addition And Subtraction
PPTX
Data structure array
PPTX
Allocation methods (1).pptx
PPTX
Data Structure and Algorithms The Tower of Hanoi
PPTX
Disk Scheduling Algorithm in Operating System
PPTX
Binary Tree Traversal
PPT
FUNCTIONS IN c++ PPT
PPTX
PPTX
Priority Queue in Data Structure
PPT
Hashing PPT
PDF
Iterations and Recursions
PPTX
Linked List
PPTX
Fractional Knapsack Problem
PPTX
Threaded binary tree
Array data structure
heap Sort Algorithm
Binary Search Tree
sparse matrix in data structure
Priority queue in DSA
Arrays in Data Structure and Algorithm
Signed Addition And Subtraction
Data structure array
Allocation methods (1).pptx
Data Structure and Algorithms The Tower of Hanoi
Disk Scheduling Algorithm in Operating System
Binary Tree Traversal
FUNCTIONS IN c++ PPT
Priority Queue in Data Structure
Hashing PPT
Iterations and Recursions
Linked List
Fractional Knapsack Problem
Threaded binary tree
Ad

Similar to Array in C full basic explanation (20)

PPT
PDF
PDF
Introduction to Arrays in C
PPT
Array 31.8.2020 updated
PPT
Arrays in c programing. practicals and .ppt
PPTX
Chapter 13.pptx
PPTX
Arrays in C language
PPTX
Arrays_in_c++.pptx
PPTX
C (PPS)Programming for problem solving.pptx
PDF
array-191103180006.pdf
PPTX
Array Introduction One-dimensional array Multidimensional array
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPTX
array.pptx ppt ggsipu bpit delhi rohini.
PPTX
Array In C++ programming object oriented programming
PDF
ARRAYS
PPTX
Various Operations Of Array(Data Structure Algorithm).pptx
PDF
Arrays-Computer programming
PDF
Cunit3.pdf
PPT
Algo>Arrays
Introduction to Arrays in C
Array 31.8.2020 updated
Arrays in c programing. practicals and .ppt
Chapter 13.pptx
Arrays in C language
Arrays_in_c++.pptx
C (PPS)Programming for problem solving.pptx
array-191103180006.pdf
Array Introduction One-dimensional array Multidimensional array
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
array.pptx ppt ggsipu bpit delhi rohini.
Array In C++ programming object oriented programming
ARRAYS
Various Operations Of Array(Data Structure Algorithm).pptx
Arrays-Computer programming
Cunit3.pdf
Algo>Arrays
Ad

Recently uploaded (20)

PPTX
web development for engineering and engineering
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Digital Logic Computer Design lecture notes
DOCX
573137875-Attendance-Management-System-original
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
web development for engineering and engineering
OOP with Java - Java Introduction (Basics)
Digital Logic Computer Design lecture notes
573137875-Attendance-Management-System-original
Model Code of Practice - Construction Work - 21102022 .pdf
Foundation to blockchain - A guide to Blockchain Tech
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Array in C full basic explanation

  • 1. REF. BOOK PROGRAMMING IN ANSI C- 7TH EDITION E. BALAGURUSAMY 1
  • 2. 2
  • 3. LEARNING OBJECTIVES: Define the concept of arrays Determine how one- dimensional array is declared and initialized Know the concept of two-dimensional arrays Discuss how two- dimensional array is declared and initialized Describe multi- dimensional arrays Explain dynamic arrays 3
  • 4. INTRODUCTION: NORMALLY ONE VARIABLE OF ONE DATATYPE CAN HOLD ONLY ONE VALUE 4 4
  • 5. What is array? An array is a fixed size sequenced collection of elements of the SAME datatype. Syntax to declare array: datatype variable-name [size]; 5 5
  • 6. We don’t need to declare values for elements. Like below: 6 6
  • 7. One-dimensional: Just by declaring one variable we can put an INDEX number which is merely the number variables. We can index starting with 0 which is preferred by computer. But we shouldn’t get confused so we can state indexing with 1. 7 7
  • 9. Compile Time Initialization: We can initialize the elements of array in same way as the ordinary variable when they are declared. Datatype array-name [size] = {list of values} Int number[3] = {0, 1, 2}; We can omit the size during compile time initialization only. int number[ ] = {1, 2, 3, 4}; This approach works fine as long as we initialize every element in the array. 9 9
  • 10. Character array Initialization : char name[ ] = {‘J’,ʻe’,ʻn’,ʻc’,ʻy’,ʻ0’}; or, char name[ ]=ʺJency”; Compile time initialization may be partial. That is, the number of initialize may be less than the declared size. int number[5]={10, 20, 30}; Here, array index number initialized is 5 but there are 3 elements. The remaining 2 places are Zero and if the array type is char the Null. int number[2] = {1,2,3,4}; In this case the declared size has more initialized elements. The compiler will create error. 10
  • 11. Run time initialization: An array can be Explicitly initialized at run time. This approach is usually applied for initializing user input data. Using for loop can help in this case. 11 11
  • 12. We can’t leave size empty in array 12 12
  • 13. Searching And Sorting: Sorting: The process of arranging elements in the list according to their values, in ascending or descending order. A sorted list is called an ordered list. Sorted lists are especially important in list searching because they facilitate rapid search operation. Important and simple sorting techniques:  Bubble sort  Selection sort  Insertion sort Searching: The process of finding the location of the specific element in a list. The specified element is often called the search key. If the search key with list element values, the search is said to be successful else unsuccessful. The most commonly used search techniques are:  Sequential Search  Binary Search  Shell sort  Merge sort  Quick sort 13 13
  • 14. TWO-DIMENSIONAL ARRAYS: We represent a particular value in matrix using 2 subscripts such as vrc here, r is for row and c is for column. Syntax: datatype array_name[row_size][column_size]; Like single-dimensional arrays, each dimension of the array is indexed from zero to its maximum size-1. The first index selects the row and the second index selects the column within row. 14 14
  • 15. 2D Array Compile time initialization: Array size declaration and values initialized in braces: int table[2][3]={0, 0, 0, 1, 1, 1}; OR, int table[2][3] = {{0, 0, 0},{1, 1, 1} }; OR, int table[2][3] = { {0, 0, 0}, {1, 1, 1} }; The initialization is done row by row We can initialize a two dimensional array in the form of matrix 15 15
  • 16. OR, int table[ ][3] = { {0, 0, 0}, {1, 1, 1} }; ***************************** int table[2][3] = { {1,2}, {2} }; ***************************** int table[2][3] = {{0}, {0}, {0}}; or, int table[2][3] = {0, 0}; When the array is completely initialized with all values, explicitly, we need not specify the size of the dimension If the values are missing in initialize, they are automatically set to Zero When all the elements are to be initialized to Zero, this short-cut may be used 16 16
  • 17. #include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; /* output each array element's value */ for ( i = 0; i < 5; i++ ){ for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i, j, a[i][j] ); } } return 0; } 17 17
  • 18. 2D matrix Run time Input and Display: #include<stdio.h> #define MAX 10 int main() { int array[MAX][MAX],i, j, r, c; printf("Enter row and column number:n"); scanf("%d %d", &r, &c); printf("Enter %d X %d elements:n", r, c); for(i = 0; i <r; i++) { for(j=0;j<c; j++) { printf("Enter array[%d][%d]: ",i+1,j+1); scanf("%d", &array[i][j]); } } printf("Your entered 2D matrix of %dX%d elements:n", r, c); for(i=0;i<r; i++) { for(j=0;j<c; j++) { printf("%5d", array[i][j]); } printf("n"); } return 0; } 18 18
  • 19. 19 19
  • 20. MULTI-DIMENSIONAL ARRAYS: C allows three or more dimensions. The exact limit is determined by the compiler. It's an array or collection of 2D arrays, and a 2D array is an array of 1D array. The general form of a multi-dimensional array is datatype arrary_name[s1][s2]…..[si]; here si is size of i-th dimension. Examples: int survey[3][5][12]; >>holds: 3*5*12=180 integer type elements<< 20 20
  • 21. Declaration and Initialization 3D Array : #include<stdio.h> int main() { int i, j, k; int arr[3][3][3]= { { {11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27, 28, 29} }, { {31, 32, 33}, {34, 35, 36}, {37, 38, 39} }, }; printf(":::3D Array Elements:::nn"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { printf("%dt", arr[i][j][k]); } printf("n"); } printf("n"); } return 0; } 21 21
  • 22. 22 22
  • 23. Dynamic Array: We create arrays at compile time. An array created at compile time by specifying SIZE in the source code has a fixed size and cannot be modified at run time. The process of allocating memory at compile time is known as Static Memory Allocation. Considering a situation where we want to use array that can vary greatly in size. In C it is possible to allocate memory to array at run time are called Dynamic arrays. Dynamic arrays are created using what are known as pointer variables and memory management function malloc, calloc and realloc. These functions are included in header file <stdlib.h>. These are used in data structure such as linked lists, stacks and queues. 23
  • 24. THANK YOU!!!!!! FOR YOUR PATIENCE JENCY