SlideShare a Scribd company logo
ARRAYS
MODULE 3
1
• An array is a collection of similar data elements.
• These data elements have the same data type.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a
numerical index, which is used to identify the element.
• The elements of the array are stored in consecutive
memory locations and are referenced by an index
(subscript).
2
DEFINITION
ARRAY REPRESENTATION
Array Name Elements
int marks[10] = { 56, 59, 69, 78, 96, 43, 90, 88, 94, 70}
Data type Array Size
Index starts with 0
Mark1 mark2 mark3 mark4 mark5 mark6 mark7 mark8 mark9 mark10
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
3
56 59 69 78 96 43 90 88 94 70Elements
Element name
LIMITATIONS OF ARRAY
• Arrays are generally used when we want to store large
amount of similar type of data.
• But they have the following limitations:
• Arrays are of fixed size.
• Data elements are stored in contiguous memory
locations which may not be always available.
• Insertion and deletion of elements can be problematic
because of shifting of elements from their positions.
4
EXAMPLE CODE 5
With Array
#include <stdio.h>
void main ()
{
int marks[6] = {56,78,88,76,56,89);
int i,tot=0;
float avg;
for (i=0; i<6; i++ )
{
tot=tot+ marks[i];
}
avg=tot/6;
printf(avg);
}
Without Array
#include <stdio.h>
void main ()
{
int marks_1 = 56, marks_2 = 78, marks_3 = 88,
marks_4 = 76, marks_5 = 56, marks_6 = 89;
float avg = (marks_1 + marks_2 + marks_3 +
marks_4 + marks_5 +marks_6) / 6 ;
printf(avg);
}
TYPES OF ARRAYS
• One Dimensional Arrays - Linear List
• Two Dimensional Arrays – Table, Matrix
• Three Dimensional Arrays – Multi Dimensional
6
TWO DIMENSIONAL ARRAYS
7
// 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};
//Declaration of 2D Array
float x[3][4];
C1 C2 C3 C4
R1 X[0][0] X[0][1] X[0][2] X[0][3]
R2 X[1][0] X[1][1] X[1][2] X[1][3]
R3 X[2][0] X[2][1] X[2][2] X[2][3]
THREE DIMENSIONAL ARRAY 8
// Initialization of 3D Array with Input Values
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}}};
// Declaration
float y[2][4][3];
OPERATIONS
• Traversing: It prints all the array elements one after another.
• Inserting: It adds an element at given index.
• Deleting: It is used to delete an element at given index.
• Searching: It searches for an element(s) using given index or by value.
• Updating: It is used to update an element at given index.
9
TRAVERSAL 10
// Access the first element of the
array
printf("%d", mark[0]);
// Access the third element of the
array printf("%d", mark[2]);
// Access ith element of the array
printf("%d", mark[i-1]);
#include <stdio.h>
void main()
{
int array[10];
int i;
printf("enter the elements n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
printf(“Elements of the array n");
for (i = 0; i < 10; i =i++)
printf( "%dn", array[i]) ;
}
INSERTION
• First get the element to be inserted, say x
• Then get the position at which this element is to be inserted, say pos
• Then shift the array elements from this position to one position
forward, and do this for all the other elements next to pos.
• Insert the element x now at the position pos, as this is now empty.
11
12 87 14 68 73 90 12 3
12 87 14 68 73 50 90 12 3
X= 50
Pos = 5
12CODE
// element to be inserted
x = 50;
// position at which elements
to be inserted
pos = 5;
// increase the size by 1
n++;
// shift elements forward
for (i = n; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
DELETION 13
Step by step descriptive logic to remove element from array.
• Move to the specified location which you want to remove in given
array.
• Copy the next element to the current element of array. Which is you
need to perform array[i] = array[i + 1].
• Repeat above steps till last element of array.
• Finally decrement the size of array by one.
14
12 87 14 68 73 50 90
12 14 68 73 50 90 90
Element to be deleted
12 14 68 73 50 90 90
15
printf("Enter the element position to delete :
");
scanf("%d", &pos);
/* Invalid delete position */
if(pos < 0 || pos > size)
{
printf("Invalid position! Please enter position
between 1 to %d", size);
}
else
{
/* Copy next element value to current
element */
for(i=pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}
/* Decrement array size by 1 */
size--;
}
/* Print array after deletion */
printf("nElements of array after delete are :
");
for(i=0; i<size; i++)
{
printf("%dt", arr[i]);
}
SEARCHING
• Input size and elements in array from user.
• Store it in some variable say size and arr.
• Input number to search from user in some variable say toSearch.
• Define a flag variable as found = 0.
• Run loop from 0 to size.
• Inside loop check if current array element is equal to searched
number or not.
• Which is if(arr[i] == toSearch) then set found = 1 flag and
terminate from loop.
• Since element is found no need to continue further.
• Outside loop if(found == 1) then element is found otherwise not.
16
UPDATION
• Consider A is a linear array with N elements
• K is a positive integer such that K<=N.
• Following is the algorithm to update an element available at the
Kth position of A.
17
printf("The original array elements are :n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d n", i, LA[i]);
}
A[k-1] = item;
printf("The array elements after updation :n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d n", i, LA[i]);
} }
18

More Related Content

What's hot

Linked list
Linked listLinked list
Linked list
akshat360
 
HTML5 Form Validation
HTML5 Form ValidationHTML5 Form Validation
HTML5 Form Validation
Ian Oxley
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
HTML Marquee
HTML MarqueeHTML Marquee
HTML Marquee
Hameda Hurmat
 
Html
HtmlHtml
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Cascading style sheets (CSS)
Cascading style sheets (CSS)Cascading style sheets (CSS)
Cascading style sheets (CSS)
Harshita Yadav
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Web topic 18 conflict resolution in css
Web topic 18  conflict resolution in cssWeb topic 18  conflict resolution in css
Web topic 18 conflict resolution in css
CK Yang
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
Ajharul Abedeen
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
WordPress Memphis
 
Css position
Css positionCss position
Css position
Webtech Learning
 
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
Rameesha Sadaqat
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
Html formatting
Html formattingHtml formatting
Html formatting
Webtech Learning
 

What's hot (20)

Linked list
Linked listLinked list
Linked list
 
HTML5 Form Validation
HTML5 Form ValidationHTML5 Form Validation
HTML5 Form Validation
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
HTML Marquee
HTML MarqueeHTML Marquee
HTML Marquee
 
Html
HtmlHtml
Html
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Cascading style sheets (CSS)
Cascading style sheets (CSS)Cascading style sheets (CSS)
Cascading style sheets (CSS)
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Web topic 18 conflict resolution in css
Web topic 18  conflict resolution in cssWeb topic 18  conflict resolution in css
Web topic 18 conflict resolution in css
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
Css position
Css positionCss position
Css position
 
Array in c++
Array in c++Array in c++
Array in c++
 
Linked list
Linked listLinked list
Linked list
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
Html formatting
Html formattingHtml formatting
Html formatting
 

Similar to Arrays

SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Array
ArrayArray
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
chin463670
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
Mohammad Imam Hossain
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
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
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
MithuBose3
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
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
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
Dhiviya Rose
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
HarmanShergill5
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
ahtishamtariq511
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
Array
ArrayArray
Array
PRN USM
 
Arrays
ArraysArrays
Arrays
Aman Agarwal
 
Array i imp
Array  i impArray  i imp
Array i imp
Vivek Kumar
 

Similar to Arrays (20)

SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Array
ArrayArray
Array
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 
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
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. 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
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Array i imp
Array  i impArray  i imp
Array i imp
 

Recently uploaded

Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
GauravCar
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Data Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptxData Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptx
ramrag33
 

Recently uploaded (20)

Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Data Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptxData Control Language.pptx Data Control Language.pptx
Data Control Language.pptx Data Control Language.pptx
 

Arrays

  • 2. • An array is a collection of similar data elements. • These data elements have the same data type. • Element − Each item stored in an array is called an element. • Index − Each location of an element in an array has a numerical index, which is used to identify the element. • The elements of the array are stored in consecutive memory locations and are referenced by an index (subscript). 2 DEFINITION
  • 3. ARRAY REPRESENTATION Array Name Elements int marks[10] = { 56, 59, 69, 78, 96, 43, 90, 88, 94, 70} Data type Array Size Index starts with 0 Mark1 mark2 mark3 mark4 mark5 mark6 mark7 mark8 mark9 mark10 index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 3 56 59 69 78 96 43 90 88 94 70Elements Element name
  • 4. LIMITATIONS OF ARRAY • Arrays are generally used when we want to store large amount of similar type of data. • But they have the following limitations: • Arrays are of fixed size. • Data elements are stored in contiguous memory locations which may not be always available. • Insertion and deletion of elements can be problematic because of shifting of elements from their positions. 4
  • 5. EXAMPLE CODE 5 With Array #include <stdio.h> void main () { int marks[6] = {56,78,88,76,56,89); int i,tot=0; float avg; for (i=0; i<6; i++ ) { tot=tot+ marks[i]; } avg=tot/6; printf(avg); } Without Array #include <stdio.h> void main () { int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 56, marks_6 = 89; float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6) / 6 ; printf(avg); }
  • 6. TYPES OF ARRAYS • One Dimensional Arrays - Linear List • Two Dimensional Arrays – Table, Matrix • Three Dimensional Arrays – Multi Dimensional 6
  • 7. TWO DIMENSIONAL ARRAYS 7 // 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}; //Declaration of 2D Array float x[3][4]; C1 C2 C3 C4 R1 X[0][0] X[0][1] X[0][2] X[0][3] R2 X[1][0] X[1][1] X[1][2] X[1][3] R3 X[2][0] X[2][1] X[2][2] X[2][3]
  • 8. THREE DIMENSIONAL ARRAY 8 // Initialization of 3D Array with Input Values 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}}}; // Declaration float y[2][4][3];
  • 9. OPERATIONS • Traversing: It prints all the array elements one after another. • Inserting: It adds an element at given index. • Deleting: It is used to delete an element at given index. • Searching: It searches for an element(s) using given index or by value. • Updating: It is used to update an element at given index. 9
  • 10. TRAVERSAL 10 // Access the first element of the array printf("%d", mark[0]); // Access the third element of the array printf("%d", mark[2]); // Access ith element of the array printf("%d", mark[i-1]); #include <stdio.h> void main() { int array[10]; int i; printf("enter the elements n"); for (i = 0; i < 10; i++) scanf("%d", &array[i]); printf(“Elements of the array n"); for (i = 0; i < 10; i =i++) printf( "%dn", array[i]) ; }
  • 11. INSERTION • First get the element to be inserted, say x • Then get the position at which this element is to be inserted, say pos • Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos. • Insert the element x now at the position pos, as this is now empty. 11 12 87 14 68 73 90 12 3 12 87 14 68 73 50 90 12 3 X= 50 Pos = 5
  • 12. 12CODE // element to be inserted x = 50; // position at which elements to be inserted pos = 5; // increase the size by 1 n++; // shift elements forward for (i = n; i >= pos; i--) arr[i] = arr[i - 1]; // insert x at pos arr[pos - 1] = x; // print the updated array for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("n");
  • 13. DELETION 13 Step by step descriptive logic to remove element from array. • Move to the specified location which you want to remove in given array. • Copy the next element to the current element of array. Which is you need to perform array[i] = array[i + 1]. • Repeat above steps till last element of array. • Finally decrement the size of array by one.
  • 14. 14 12 87 14 68 73 50 90 12 14 68 73 50 90 90 Element to be deleted 12 14 68 73 50 90 90
  • 15. 15 printf("Enter the element position to delete : "); scanf("%d", &pos); /* Invalid delete position */ if(pos < 0 || pos > size) { printf("Invalid position! Please enter position between 1 to %d", size); } else { /* Copy next element value to current element */ for(i=pos-1; i<size-1; i++) { arr[i] = arr[i + 1]; } /* Decrement array size by 1 */ size--; } /* Print array after deletion */ printf("nElements of array after delete are : "); for(i=0; i<size; i++) { printf("%dt", arr[i]); }
  • 16. SEARCHING • Input size and elements in array from user. • Store it in some variable say size and arr. • Input number to search from user in some variable say toSearch. • Define a flag variable as found = 0. • Run loop from 0 to size. • Inside loop check if current array element is equal to searched number or not. • Which is if(arr[i] == toSearch) then set found = 1 flag and terminate from loop. • Since element is found no need to continue further. • Outside loop if(found == 1) then element is found otherwise not. 16
  • 17. UPDATION • Consider A is a linear array with N elements • K is a positive integer such that K<=N. • Following is the algorithm to update an element available at the Kth position of A. 17 printf("The original array elements are :n"); for(i = 0; i<n; i++) { printf("A[%d] = %d n", i, LA[i]); } A[k-1] = item; printf("The array elements after updation :n"); for(i = 0; i<n; i++) { printf("A[%d] = %d n", i, LA[i]); } }
  • 18. 18