arrays in c

V
vidhi mehtaLecturer en Swami Sahajanand Group of Colleges
Vidhi Mehta
Unit 3
Arrays
What is Array?
 when we want to store more data type of the
same value we need to declare more variable
which leads to more memory utilization. C
supports array that is a derived data type.
 An array is a fixed size sequential group of
elements of the same data types which are
referred by common name. In other words array
is a collective name given to a group of ‘similar
quantities’.
 An array is a series of elements of the same
data type placed consecutively in memory
that can be individually referenced by adding
an index to a unique name.
Types of arrays
 There are 3 types of array:
 1) One-Dimensional array.
 2) Two-Dimensional array.
 3) Multi-Dimensional array.
1- Dimensional array
 A list of items can be given one variable name
using only one subscript and such a variable
is called a single subscripted variable or a
one dimensional array. The subscript begins
with 0.
How to declare 1-D array
 Type <variablename> [size];
 Eg:
 int a[5];
 float b[4];
 char c[5];
arrays in c
Initialization of 1D array
 After an array is declared, its elements must be
initialized. Otherwise they will contain “garbage”.
An array can be initialized at 2 stages:
 1) At compile time
 2) At runtime
Compile time initialization
 We can initialize the elements of array in the same
way as the ordinary variable during the time of
declaration.
 Syntax:
 type <array name> [size]={values};
 Eg:
 int a[4]={1,1,1,2};
 float b[4]={1.2, 2.34, 31.2,-2};
 The values in the list are separated by commas. The
size may be omitted. In such cases the compiler
allocates enough space for all initialized elements.
 Eg: int a[]={1,1,1,1};
 Here the variable a will contain four elements with
initial value 1.
 Compile time initialization may be partial. That is
number of initialize may be less than the declared
size. In such case the remaining elements are
initialized to zero in case of int and float type and
null in case of character.
 Eg:
 int a[4]={1,1};
 float b[2]={1.2,1.3};
 char c[4]={‘h’,’I’};
Run time initialization
 An array can be explicitly initialized at run time. This is
generally done for initializing large array.
 for(i=0;i<=200;i++)
{
If(i<50) {
Sum[i]=5.0;
}
Else {
Sum[i]=1.0;
}
 }
2D Array
 There could be situations where a table of values
will have to be stored. We can think of this table
as a matrix consisting of rows and columns. C
allows us to define such tables of items by using
2D array. 2D arrays are declared as follows:
 Syntax :
 Type <array name> [rows size] [column size];
 Eg : int table [3][3];
 Here the name of the array is table having 3 rows
and 3 columns.
 Initializing 2D array
 Int table[2][2]={0,0,1,1};
Multi Dimensional Array
 C allows arrays of three or more dimensions. The
exact limit is determined by the compiler.
 Syntax:
 type <array name> [size1][size2][size3]….[sizen];
 Eg: int a[3][3][2][5][6];
 ANSI C does not specify any limit for array
dimension. However, most compilers permit 7 to
10 dimensions.
Dynamic Arrays
 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 a static memory allocation and the arrays that
receive static memory allocation are called static
arrays.
 But in C it is possible to allocate memory to arrays at
run time. This feature is known as dynamic memory
allocation and the arrays created at run time are
called dynamic arrays.
 Dynamic arrays are created using pointer variables
and memory management functions called malloc,
calloc and realloc. These functions are included in the
Searching
 Searching is a process of finding the location of
the specified element in a list. The specified
element is often called the search key. If the
process of searching finds a match of the search
key with a list element value, the search is said to
be successful otherwise unsuccessful. The two
search techniques are:
 1.) Sequential Search
 2.) Binary Search
LINEAR SEARCH
 This is simplest technique to find out an element
in an unsorted list. Suppose, we have an
unsorted list which contain n elements and want
to search the value of a particular elements for
that we have to check each elements one by one
and find at any passion.
 One of the most straightforward and elementary
searches is the sequential search, also known as
a linear search.
Applications of Linear Search->
 To find out particular string linear search is
efficient
 Linear search is usually very simple to implement
and is practical when the list has only few
elements or when performing a single search in
an unordered list
 Pseduocode game is the eg of linear search
Merits of Linear Search->
 The primary advantage of linear search is its
simplicity.
 It is also very resource efficient and memory
efficient. It also operates equally well on both
unsorted and sorted data.
Demerits of Linear Search->
 It needs more space and time complexity
 In linear search if the key element is the last
element and the search is from first element that
is a worst case or if the key element is the first
element and the search is from last element then
also is the worst case.
BINARY SEARCH
 Binary search works for sorted list and it is very
efficient searching technique.
 It is used to find the location of given element or
record in the list. Other information associated
with the element can also be fetched if required.
 To find the location of a file in the computer
directory one can use this searching technique.
 If we are on the internet then it is not easy task
using linear search to find out the information
about some records or files.
 For this one prefers binary searching technique.
Applications of binary search
 Guessing game
 Word list
Merits of binary search
 Searching speed is fast
Demerits of binary search
 The disadvantage of binary search in any
language is that you must provide an ordered list.
Sorting
 Sorting means arranging a set of data in some
order. There are different methods that are used
to sort the data in ascending order or descending
order.
BUBBLE SORT
 It is very simple sorting technique thus this
technique is not efficient to other techniques.
Suppose we sort the element in descending
order. The bubble sort loops through the elements
in the list comparing the adjacent element and
moves the largest element to the top of the list.
 Application of Bubble Sort
 In a small group of data we can use bubble sort
like classroom
 Sorting data of friends group
 Sorting data of our books collection
 Merits of Bubble Sort
 Easy to implement
 Efficient for small data set
 A bubble sort is a sort where adjacent items in the
array or list are scanned repeatedly swapping as
necessary until one full scan performs no swaps.
 Demerits of Bubble Sort
 A bubble sort, sort N elements where N is the
size of array or list because of out of position item
is only moved one position per scan and sorting
continues until all the elements are not swapped.
 It is less efficient in large data set.
SELECTON SORT
 The selection sort starts from element and
searches the entire list until it find the minimum
value. The sort palace the minimum value in the
first place. select the second element and
searches for the smallest element. The process
continuous until the complete the list.
 The Selection Sort is a very basic sort. It works by
finding the smallest element in the array and
putting it at the beginning of the list and then
repeating that process on the unsorted remainder
of the data. Rather than making successive
swaps with adjacent elements like bubble sort,
selection sort makes only one, swapping the
smallest number with the number occupying its
correct position.
 Application of Selection Sort
 In a small group of data we can use selection sort
 Sorting of friends group
 Sorting of data of our books collection
 Merits of Selection Sort
 Easy to implement
 Efficient for small data set
 Demerits of Selection Sort
 The worst case occurs if the array is already
sorted in descending order.
 Selection sort spends most of its time trying to
find the minimum element in the unsorted part of
the array
 It clearly shows the similarity between selection
sort and bubble sort.
1 de 30

Recomendados

C++ Arrays por
C++ ArraysC++ Arrays
C++ Arraysأحمد محمد
3.3K vistas20 diapositivas
Arrays in c por
Arrays in cArrays in c
Arrays in cvampugani
1K vistas23 diapositivas
Programming in c Arrays por
Programming in c ArraysProgramming in c Arrays
Programming in c Arraysjanani thirupathi
24K vistas31 diapositivas
Arrays in c por
Arrays in cArrays in c
Arrays in cJeeva Nanthini
7.3K vistas14 diapositivas
Array in c por
Array in cArray in c
Array in cRavi Gelani
50.9K vistas22 diapositivas
2D Array por
2D Array 2D Array
2D Array Ehatsham Riaz
3.4K vistas19 diapositivas

Más contenido relacionado

La actualidad más candente

Pointers por
PointersPointers
PointersProf. Dr. K. Adisesha
389 vistas23 diapositivas
Presentation on array por
Presentation on array Presentation on array
Presentation on array topu93
6.9K vistas16 diapositivas
Arrays por
ArraysArrays
ArraysKulachi Hansraj Model School Ashok Vihar
3.7K vistas14 diapositivas
Passing an Array to a Function (ICT Programming) por
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Fatima Kate Tanay
1.8K vistas5 diapositivas
Constants in C Programming por
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
6.3K vistas11 diapositivas
Array in c programming por
Array in c programmingArray in c programming
Array in c programmingMazharul Islam
1.4K vistas8 diapositivas

La actualidad más candente(20)

Presentation on array por topu93
Presentation on array Presentation on array
Presentation on array
topu936.9K vistas
Passing an Array to a Function (ICT Programming) por Fatima Kate Tanay
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay1.8K vistas
Constants in C Programming por programming9
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming96.3K vistas
sparse matrix in data structure por MAHALAKSHMI P
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P2.5K vistas
Array in c language por home
Array in c languageArray in c language
Array in c language
home42K vistas
Strings in c por vampugani
Strings in cStrings in c
Strings in c
vampugani2.4K vistas
Python-03| Data types por Mohd Sajjad
Python-03| Data typesPython-03| Data types
Python-03| Data types
Mohd Sajjad2.2K vistas
Sparse matrix and its representation data structure por Vardhil Patel
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel7.5K vistas
structure and union por student
structure and unionstructure and union
structure and union
student15.4K vistas

Similar a arrays in c

DS - Unit 2 FINAL (2).pptx por
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
18 vistas87 diapositivas
Data Structures_ Sorting & Searching por
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingThenmozhiK5
68 vistas87 diapositivas
Array.pdf por
Array.pdfArray.pdf
Array.pdfDEEPAK948083
8 vistas18 diapositivas
Array ppt por
Array pptArray ppt
Array pptKaushal Mehta
39.5K vistas18 diapositivas
Arrays por
ArraysArrays
Arrayssana younas
153 vistas18 diapositivas
4.1 sequentioal search por
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
518 vistas10 diapositivas

Similar a arrays in c(20)

DS - Unit 2 FINAL (2).pptx por prakashvs7
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs718 vistas
Data Structures_ Sorting & Searching por ThenmozhiK5
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK568 vistas
4.1 sequentioal search por Krish_ver2
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2518 vistas
Arrays por uos
ArraysArrays
Arrays
uos79 vistas
Arrays In C++ por Awais Alam
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam20.3K vistas
Searching,sorting por LavanyaJ28
Searching,sortingSearching,sorting
Searching,sorting
LavanyaJ2832 vistas
MODULE 5-Searching and-sorting por nikshaikh786
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786149 vistas
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI por Sowmya Jyothi
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi323 vistas
SORTING techniques.pptx por Dr.Shweta
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
Dr.Shweta4 vistas

Último

AWS Certified Solutions Architect Associate Exam Guide_published .pdf por
AWS Certified Solutions Architect Associate Exam Guide_published .pdfAWS Certified Solutions Architect Associate Exam Guide_published .pdf
AWS Certified Solutions Architect Associate Exam Guide_published .pdfKiran Kumar Malik
6 vistas121 diapositivas
REACTJS.pdf por
REACTJS.pdfREACTJS.pdf
REACTJS.pdfArthyR3
39 vistas16 diapositivas
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R... por
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...IJCNCJournal
5 vistas25 diapositivas
Programmable Logic Devices : SPLD and CPLD por
Programmable Logic Devices : SPLD and CPLDProgrammable Logic Devices : SPLD and CPLD
Programmable Logic Devices : SPLD and CPLDUsha Mehta
44 vistas54 diapositivas
Global airborne satcom market report por
Global airborne satcom market reportGlobal airborne satcom market report
Global airborne satcom market reportdefencereport78
8 vistas13 diapositivas
dummy.pptx por
dummy.pptxdummy.pptx
dummy.pptxJamesLamp
7 vistas2 diapositivas

Último(20)

AWS Certified Solutions Architect Associate Exam Guide_published .pdf por Kiran Kumar Malik
AWS Certified Solutions Architect Associate Exam Guide_published .pdfAWS Certified Solutions Architect Associate Exam Guide_published .pdf
AWS Certified Solutions Architect Associate Exam Guide_published .pdf
REACTJS.pdf por ArthyR3
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR339 vistas
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R... por IJCNCJournal
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
IJCNCJournal5 vistas
Programmable Logic Devices : SPLD and CPLD por Usha Mehta
Programmable Logic Devices : SPLD and CPLDProgrammable Logic Devices : SPLD and CPLD
Programmable Logic Devices : SPLD and CPLD
Usha Mehta44 vistas
Different type of computer networks .pptx por nazmul1514788
Different  type of computer networks .pptxDifferent  type of computer networks .pptx
Different type of computer networks .pptx
nazmul151478820 vistas
DevFest 2023 Daegu Speech_이재규, Implementing easy and simple chat with gol... por JQLEE6
DevFest 2023 Daegu Speech_이재규,  Implementing easy and simple chat with gol...DevFest 2023 Daegu Speech_이재규,  Implementing easy and simple chat with gol...
DevFest 2023 Daegu Speech_이재규, Implementing easy and simple chat with gol...
JQLEE616 vistas
REPORT Data Science EXPERT LECTURE.doc por Parulkhatri11
REPORT Data Science EXPERT LECTURE.docREPORT Data Science EXPERT LECTURE.doc
REPORT Data Science EXPERT LECTURE.doc
Parulkhatri117 vistas
MongoDB.pdf por ArthyR3
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
ArthyR353 vistas
Ansari: Practical experiences with an LLM-based Islamic Assistant por M Waleed Kadous
Ansari: Practical experiences with an LLM-based Islamic AssistantAnsari: Practical experiences with an LLM-based Islamic Assistant
Ansari: Practical experiences with an LLM-based Islamic Assistant
M Waleed Kadous13 vistas
Field Programmable Gate Arrays : Architecture por Usha Mehta
Field Programmable Gate Arrays : ArchitectureField Programmable Gate Arrays : Architecture
Field Programmable Gate Arrays : Architecture
Usha Mehta33 vistas
Design_Discover_Develop_Campaign.pptx por ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth659 vistas
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... por csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn17 vistas

arrays in c

  • 2. What is Array?  when we want to store more data type of the same value we need to declare more variable which leads to more memory utilization. C supports array that is a derived data type.  An array is a fixed size sequential group of elements of the same data types which are referred by common name. In other words array is a collective name given to a group of ‘similar quantities’.  An array is a series of elements of the same data type placed consecutively in memory that can be individually referenced by adding an index to a unique name.
  • 3. Types of arrays  There are 3 types of array:  1) One-Dimensional array.  2) Two-Dimensional array.  3) Multi-Dimensional array.
  • 4. 1- Dimensional array  A list of items can be given one variable name using only one subscript and such a variable is called a single subscripted variable or a one dimensional array. The subscript begins with 0.
  • 5. How to declare 1-D array  Type <variablename> [size];  Eg:  int a[5];  float b[4];  char c[5];
  • 7. Initialization of 1D array  After an array is declared, its elements must be initialized. Otherwise they will contain “garbage”. An array can be initialized at 2 stages:  1) At compile time  2) At runtime
  • 8. Compile time initialization  We can initialize the elements of array in the same way as the ordinary variable during the time of declaration.  Syntax:  type <array name> [size]={values};  Eg:  int a[4]={1,1,1,2};  float b[4]={1.2, 2.34, 31.2,-2};  The values in the list are separated by commas. The size may be omitted. In such cases the compiler allocates enough space for all initialized elements.  Eg: int a[]={1,1,1,1};  Here the variable a will contain four elements with initial value 1.
  • 9.  Compile time initialization may be partial. That is number of initialize may be less than the declared size. In such case the remaining elements are initialized to zero in case of int and float type and null in case of character.  Eg:  int a[4]={1,1};  float b[2]={1.2,1.3};  char c[4]={‘h’,’I’};
  • 10. Run time initialization  An array can be explicitly initialized at run time. This is generally done for initializing large array.  for(i=0;i<=200;i++) { If(i<50) { Sum[i]=5.0; } Else { Sum[i]=1.0; }  }
  • 11. 2D Array  There could be situations where a table of values will have to be stored. We can think of this table as a matrix consisting of rows and columns. C allows us to define such tables of items by using 2D array. 2D arrays are declared as follows:  Syntax :  Type <array name> [rows size] [column size];  Eg : int table [3][3];  Here the name of the array is table having 3 rows and 3 columns.  Initializing 2D array  Int table[2][2]={0,0,1,1};
  • 12. Multi Dimensional Array  C allows arrays of three or more dimensions. The exact limit is determined by the compiler.  Syntax:  type <array name> [size1][size2][size3]….[sizen];  Eg: int a[3][3][2][5][6];  ANSI C does not specify any limit for array dimension. However, most compilers permit 7 to 10 dimensions.
  • 13. Dynamic Arrays  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 a static memory allocation and the arrays that receive static memory allocation are called static arrays.  But in C it is possible to allocate memory to arrays at run time. This feature is known as dynamic memory allocation and the arrays created at run time are called dynamic arrays.  Dynamic arrays are created using pointer variables and memory management functions called malloc, calloc and realloc. These functions are included in the
  • 14. Searching  Searching is a process of finding the location of the specified element in a list. The specified element is often called the search key. If the process of searching finds a match of the search key with a list element value, the search is said to be successful otherwise unsuccessful. The two search techniques are:  1.) Sequential Search  2.) Binary Search
  • 15. LINEAR SEARCH  This is simplest technique to find out an element in an unsorted list. Suppose, we have an unsorted list which contain n elements and want to search the value of a particular elements for that we have to check each elements one by one and find at any passion.  One of the most straightforward and elementary searches is the sequential search, also known as a linear search.
  • 16. Applications of Linear Search->  To find out particular string linear search is efficient  Linear search is usually very simple to implement and is practical when the list has only few elements or when performing a single search in an unordered list  Pseduocode game is the eg of linear search
  • 17. Merits of Linear Search->  The primary advantage of linear search is its simplicity.  It is also very resource efficient and memory efficient. It also operates equally well on both unsorted and sorted data.
  • 18. Demerits of Linear Search->  It needs more space and time complexity  In linear search if the key element is the last element and the search is from first element that is a worst case or if the key element is the first element and the search is from last element then also is the worst case.
  • 19. BINARY SEARCH  Binary search works for sorted list and it is very efficient searching technique.  It is used to find the location of given element or record in the list. Other information associated with the element can also be fetched if required.  To find the location of a file in the computer directory one can use this searching technique.  If we are on the internet then it is not easy task using linear search to find out the information about some records or files.  For this one prefers binary searching technique.
  • 20. Applications of binary search  Guessing game  Word list
  • 21. Merits of binary search  Searching speed is fast
  • 22. Demerits of binary search  The disadvantage of binary search in any language is that you must provide an ordered list.
  • 23. Sorting  Sorting means arranging a set of data in some order. There are different methods that are used to sort the data in ascending order or descending order.
  • 24. BUBBLE SORT  It is very simple sorting technique thus this technique is not efficient to other techniques. Suppose we sort the element in descending order. The bubble sort loops through the elements in the list comparing the adjacent element and moves the largest element to the top of the list.
  • 25.  Application of Bubble Sort  In a small group of data we can use bubble sort like classroom  Sorting data of friends group  Sorting data of our books collection
  • 26.  Merits of Bubble Sort  Easy to implement  Efficient for small data set  A bubble sort is a sort where adjacent items in the array or list are scanned repeatedly swapping as necessary until one full scan performs no swaps.
  • 27.  Demerits of Bubble Sort  A bubble sort, sort N elements where N is the size of array or list because of out of position item is only moved one position per scan and sorting continues until all the elements are not swapped.  It is less efficient in large data set.
  • 28. SELECTON SORT  The selection sort starts from element and searches the entire list until it find the minimum value. The sort palace the minimum value in the first place. select the second element and searches for the smallest element. The process continuous until the complete the list.  The Selection Sort is a very basic sort. It works by finding the smallest element in the array and putting it at the beginning of the list and then repeating that process on the unsorted remainder of the data. Rather than making successive swaps with adjacent elements like bubble sort, selection sort makes only one, swapping the smallest number with the number occupying its correct position.
  • 29.  Application of Selection Sort  In a small group of data we can use selection sort  Sorting of friends group  Sorting of data of our books collection
  • 30.  Merits of Selection Sort  Easy to implement  Efficient for small data set  Demerits of Selection Sort  The worst case occurs if the array is already sorted in descending order.  Selection sort spends most of its time trying to find the minimum element in the unsorted part of the array  It clearly shows the similarity between selection sort and bubble sort.