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

What's hot

One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
Talha mahmood
 
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 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
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
أحمد محمد
 

What's hot (20)

One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
Chap09
Chap09Chap09
Chap09
 
1-D array
1-D array1-D array
1-D array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Array ppt
Array pptArray ppt
Array ppt
 
array
array array
array
 
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
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
ARRAY
ARRAYARRAY
ARRAY
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Array
ArrayArray
Array
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 

Similar to Array in C full basic explanation

Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Chap 6 c++Chap 6 c++
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
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
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
Cunit3.pdf
Cunit3.pdfCunit3.pdf
Cunit3.pdf
zeenatparveen24
 
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
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
arshpreetkaur07
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
MithuBose3
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 

Similar to Array in C full basic explanation (20)

Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
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
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Cunit3.pdf
Cunit3.pdfCunit3.pdf
Cunit3.pdf
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
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
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 

Recently uploaded

HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 

Recently uploaded (20)

HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 

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