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
Ad

More Related Content

What's hot (20)

One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
poonam.rwalia
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
Talha mahmood
 
Chap09
Chap09Chap09
Chap09
Terry Yoast
 
1-D array
1-D array1-D array
1-D array
Swarup Kumar Boro
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Sangani Ankur
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
array
array array
array
Yaswanth Babu Gummadivelli
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
ARRAY
ARRAYARRAY
ARRAY
ayush raj
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Array
ArrayArray
Array
HarshKumar943076
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 

Similar to Array in C full basic explanation (20)

Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Anurag University Hyderabad
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docxArraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Array In C++ programming object oriented programming
Array In C++ programming object oriented programmingArray In C++ programming object oriented programming
Array In C++ programming object oriented programming
Ahmad177077
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Various Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptxVarious Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptx
atirathpal007
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Cunit3.pdf
Cunit3.pdfCunit3.pdf
Cunit3.pdf
zeenatparveen24
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
Ain-ul-Moiz Khawaja
 
Array Data Structure for programing language
Array Data Structure for programing languageArray Data Structure for programing language
Array Data Structure for programing language
deepuranjankumar08
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
wrushabhsirsat
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docxArraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Array In C++ programming object oriented programming
Array In C++ programming object oriented programmingArray In C++ programming object oriented programming
Array In C++ programming object oriented programming
Ahmad177077
 
Various Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptxVarious Operations Of Array(Data Structure Algorithm).pptx
Various Operations Of Array(Data Structure Algorithm).pptx
atirathpal007
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Array Data Structure for programing language
Array Data Structure for programing languageArray Data Structure for programing language
Array Data Structure for programing language
deepuranjankumar08
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
wrushabhsirsat
 
Ad

Recently uploaded (20)

2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Ad

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
  翻译: