SlideShare a Scribd company logo
1 of 115
Download to read offline
Unit:4 Arrays & Basic Algorithms
What is an array?
• Collection of similar type of data items.
• All array elements are stored into consecutive
memory locations.
• Can be accessed using a common name
followed by an index or subscript(specified
inside the square brackets).
• Array name is the pointer to the first location
of the memory block.
Declaration of an array
• Array must be declared before use:
data_type array_name [size]
• C does not allow declaring an array whose
number of elements is not known at the time
of compilation.
• There is no bound checking concept in arrays
in ‘C’, i.e. one can attempt to enter any
number of values irrespective of the integer
index specified during declaration of arrays.
Initializing an array
• Two ways:
1. int arr[4]={5,0,1,7}
2. int arr[] ={2,8,0,4,7,2}
• Error: To many initializers.
• No Error: No bound checking in this way.
• Output: garbage value.
Reading array elements
• scanf(“%d”, &a*0+);
• scanf(“%d”, &a[1]);
• scanf(“%d”, &a[2]);
• Using loops:
for(i=0; i<=2; i++)
{
scanf(“%d”, &a*i+);
}
Printing array elements
• printf(“%d”, a*0+);
• printf(“%d”, a[1]);
• printf(“%d”, a[2]);
• Using loops:
for(i=0; i<=2; i++)
{
printf(“%d”, a*i+);
}
Calculating length of an array
Length = upper_bound – lower_bound + 1
To find the address of a particular
element in an array
• Eg. If base address=2000, each element need
2 bytes, find address of fifth element.
A[5] = 2000+ 2*(5-0) =2010
arr[k] =
Base address (B) + Size of element(W) * (Index of element(K) – Base index)
Operations on Array Elements
• Traversal
• Insertion
• Deletion
• Merging
• Search
• Sorting
Traversal
Programs
• Read an array of elements and print the sum of
elements.
• Copy an array of elements in another array.
• Copy an array of elements in another array in
reverse order.
• Find the sum of even and odd numbers in an array.
• Find the sum of even and odd indexed elements.
• To put even and odd elements in two separate array.
• Find the minimum and maximum of all elements of
an array and print index as well.
• Find the second minimum and second maximum of
all elements of an array and print index as well.
• To interchange largest and smallest number in
an array.
• To search an element in an array.
• Count the occurrence of a number in an array.
• To find whether the array contain a duplicate
number or not.
• To arrange the elements in sorted order.
• To append one array elements with another
array.
• Find cumulative addition of the elements of an
array.
Read an array of elements and print the sum of elements
Copy an array of elements in another array.
Copy an array of elements in another array in
reverse order.
Find the sum of even and odd numbers in an array.
Find the sum of even and odd indexed elements.
To put even and odd elements in two separate array.
Find the minimum and maximum of all
elements of an array and print index as well.
To arrange the elements in sorted order. Find the second
minimum and second maximum of all elements of an array.
To insert elements in an array
To delete an array element
To search an element in an array using linear Search.
To count occurrence of an array element.
To find whether an array of integers contain a duplicate number.
Sorting
Selection Sort
• It sorts an array by repeatedly finding the
minimum element from unsorted part and
putting it at the beginning.
• The algorithm maintains two sub arrays in
given array-
1. Already sorted part – left part of array
2. Remaining unsorted part – right part of array
Bubble Sort
• Simplest sorting algorithm works by repeatedly
swapping the adjacent elements if they are in
wrong order.
• This algorithm works in various pass until a sorted
array is achieved.
• The algorithm maintains two sub arrays in given
array-
1. Already sorted part – right part of array
2. Remaining unsorted part – left part of array
Insertion Sort
• It works the way we sort playing cards in our
hands.
• This algorithm picks elements one by one and
places it to right position where it belongs in
the sorted list of elements.
• An array of arrays is known as 2D array. The two
dimensional (2D) array in C programming is also
known as matrix. A matrix can be represented as
a table of rows and columns.
• A 2D array is stored in the computer's memory
one row following another.
• If each data value of the array requires B bytes
of memory, and if the array has C columns, then
the memory location of an element such as
score[m][n] is (m*c+n)*B from the address of
the first byte.
Initialization of a two dimensional array
• Different ways to initialize two dimensional :
• 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};
Programs
• Write a program to read 2-D array and print
the elements of the array.
• Find the sum of the elements of a 2-D array.
• Write a program to find Transpose of a matrix.
• Write a program to find multiplication of two
matrices.
Write a program to read 2-D array and print the elements of the array.
Find the sum of the elements of a 2-D array.
Write a program to find Transpose of a matrix.
Write a program to find multiplication of two matrices.
• String is a sequence of characters that is treated
as a single data item and terminated by null
character '0'.
• Remember that C language does not support
strings as a data type.
• A string is actually one-dimensional array of
characters in C language.
• For example: The string "hello world" contains
12 characters including '0' character which is
automatically added by the compiler at the end
of the string.
• What is NULL Char “0”?
'0' represents the end of the string. It is also
referred as String terminator & Null
Character(ASCII value=0).
Other ways to print string (character
by character) :
Read and print a string using scanf
Other way can be:
Finding Length of a string Manually
Finding reverse of a string Manually
Concatenation of two strings Manually
• User Defined Data type.
• A way to store information in group variables,
which can store dissimilar type of data.
• Defining structure means creating new data
type.
int
char float
Double
Member
Variables
Structure
Reading and printing structure
Reading and printing structure
Structure using Function
Array VS Sturcture
Array
• Using array only
same type of data
can be stored.
• It is a derived data
type.
• It needs to be
declared and then
used.
Structure
• It can store dissimilar
data as well.
• User defined data
type.
• It needs to be define
first only then we
can use the variables
of that type.
• User Defined Data Types.
• Internally compiler treats the enumerators as
integers.
• Each value on the list of permissible values
corresponds to an integer, starting with 0.
• It helps in writing clear codes and simplify
programming.
Unit 4
Unit 4
Unit 4

More Related Content

What's hot

What's hot (20)

Data types in C
Data types in CData types in C
Data types in C
 
single linked list
single linked listsingle linked list
single linked list
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
C++ data types
C++ data typesC++ data types
C++ data types
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
C programming
C programmingC programming
C programming
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 

Similar to Unit 4

DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptxAnuJoseph95
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptxEpsiba1
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structuresSenthil Murugan
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresmidtushar
 
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
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAhmad Bashar Eter
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptxdfsdg3
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxcoc7987515756
 

Similar to Unit 4 (20)

ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Array ppt
Array pptArray ppt
Array ppt
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
Arrays
ArraysArrays
Arrays
 
arrayppt.pptx
arrayppt.pptxarrayppt.pptx
arrayppt.pptx
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptx
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Presentation of array
Presentation of arrayPresentation of array
Presentation of array
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
 
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...
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
 
Programming fundamentals week 12.pptx
Programming fundamentals week 12.pptxProgramming fundamentals week 12.pptx
Programming fundamentals week 12.pptx
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
 
Cunit3.pdf
Cunit3.pdfCunit3.pdf
Cunit3.pdf
 

More from SHIKHA GAUTAM

Agreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared MemoryAgreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared MemorySHIKHA GAUTAM
 
Distributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock DetectionDistributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock DetectionSHIKHA GAUTAM
 
Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance SHIKHA GAUTAM
 
Type conversion in c
Type conversion in cType conversion in c
Type conversion in cSHIKHA GAUTAM
 
3. basic organization of a computer
3. basic organization of a computer3. basic organization of a computer
3. basic organization of a computerSHIKHA GAUTAM
 
Generations of computer
Generations of computerGenerations of computer
Generations of computerSHIKHA GAUTAM
 
Warehouse Planning and Implementation
Warehouse Planning and ImplementationWarehouse Planning and Implementation
Warehouse Planning and ImplementationSHIKHA GAUTAM
 
Dbms Introduction and Basics
Dbms Introduction and BasicsDbms Introduction and Basics
Dbms Introduction and BasicsSHIKHA GAUTAM
 

More from SHIKHA GAUTAM (16)

Agreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared MemoryAgreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared Memory
 
Distributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock DetectionDistributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Mutual Exclusion and Distributed Deadlock Detection
 
Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance Distributed Systems Introduction and Importance
Distributed Systems Introduction and Importance
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Unit ii_KCS201
Unit ii_KCS201Unit ii_KCS201
Unit ii_KCS201
 
Type conversion in c
Type conversion in cType conversion in c
Type conversion in c
 
C intro
C introC intro
C intro
 
4. algorithm
4. algorithm4. algorithm
4. algorithm
 
3. basic organization of a computer
3. basic organization of a computer3. basic organization of a computer
3. basic organization of a computer
 
Generations of computer
Generations of computerGenerations of computer
Generations of computer
 
c_programming
c_programmingc_programming
c_programming
 
Data Mining
Data MiningData Mining
Data Mining
 
Warehouse Planning and Implementation
Warehouse Planning and ImplementationWarehouse Planning and Implementation
Warehouse Planning and Implementation
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Dbms Introduction and Basics
Dbms Introduction and BasicsDbms Introduction and Basics
Dbms Introduction and Basics
 
DBMS
DBMSDBMS
DBMS
 

Recently uploaded

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 

Recently uploaded (20)

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 

Unit 4

  • 1. Unit:4 Arrays & Basic Algorithms
  • 2.
  • 3. What is an array? • Collection of similar type of data items. • All array elements are stored into consecutive memory locations. • Can be accessed using a common name followed by an index or subscript(specified inside the square brackets). • Array name is the pointer to the first location of the memory block.
  • 4.
  • 5.
  • 6. Declaration of an array • Array must be declared before use: data_type array_name [size] • C does not allow declaring an array whose number of elements is not known at the time of compilation.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. • There is no bound checking concept in arrays in ‘C’, i.e. one can attempt to enter any number of values irrespective of the integer index specified during declaration of arrays.
  • 12.
  • 13. Initializing an array • Two ways: 1. int arr[4]={5,0,1,7} 2. int arr[] ={2,8,0,4,7,2}
  • 14. • Error: To many initializers.
  • 15. • No Error: No bound checking in this way.
  • 17. Reading array elements • scanf(“%d”, &a*0+); • scanf(“%d”, &a[1]); • scanf(“%d”, &a[2]); • Using loops: for(i=0; i<=2; i++) { scanf(“%d”, &a*i+); }
  • 18.
  • 19.
  • 20. Printing array elements • printf(“%d”, a*0+); • printf(“%d”, a[1]); • printf(“%d”, a[2]); • Using loops: for(i=0; i<=2; i++) { printf(“%d”, a*i+); }
  • 21. Calculating length of an array Length = upper_bound – lower_bound + 1
  • 22. To find the address of a particular element in an array • Eg. If base address=2000, each element need 2 bytes, find address of fifth element. A[5] = 2000+ 2*(5-0) =2010 arr[k] = Base address (B) + Size of element(W) * (Index of element(K) – Base index)
  • 23. Operations on Array Elements • Traversal • Insertion • Deletion • Merging • Search • Sorting
  • 25.
  • 26. Programs • Read an array of elements and print the sum of elements. • Copy an array of elements in another array. • Copy an array of elements in another array in reverse order. • Find the sum of even and odd numbers in an array. • Find the sum of even and odd indexed elements. • To put even and odd elements in two separate array. • Find the minimum and maximum of all elements of an array and print index as well. • Find the second minimum and second maximum of all elements of an array and print index as well.
  • 27. • To interchange largest and smallest number in an array. • To search an element in an array. • Count the occurrence of a number in an array. • To find whether the array contain a duplicate number or not. • To arrange the elements in sorted order. • To append one array elements with another array. • Find cumulative addition of the elements of an array.
  • 28. Read an array of elements and print the sum of elements
  • 29. Copy an array of elements in another array.
  • 30. Copy an array of elements in another array in reverse order.
  • 31. Find the sum of even and odd numbers in an array.
  • 32. Find the sum of even and odd indexed elements.
  • 33. To put even and odd elements in two separate array.
  • 34.
  • 35. Find the minimum and maximum of all elements of an array and print index as well.
  • 36.
  • 37. To arrange the elements in sorted order. Find the second minimum and second maximum of all elements of an array.
  • 38.
  • 39. To insert elements in an array
  • 40.
  • 41. To delete an array element
  • 42.
  • 43.
  • 44.
  • 45. To search an element in an array using linear Search.
  • 46. To count occurrence of an array element.
  • 47. To find whether an array of integers contain a duplicate number.
  • 48.
  • 49.
  • 50.
  • 52. Selection Sort • It sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning. • The algorithm maintains two sub arrays in given array- 1. Already sorted part – left part of array 2. Remaining unsorted part – right part of array
  • 53.
  • 54.
  • 55. Bubble Sort • Simplest sorting algorithm works by repeatedly swapping the adjacent elements if they are in wrong order. • This algorithm works in various pass until a sorted array is achieved. • The algorithm maintains two sub arrays in given array- 1. Already sorted part – right part of array 2. Remaining unsorted part – left part of array
  • 56.
  • 57.
  • 58. Insertion Sort • It works the way we sort playing cards in our hands. • This algorithm picks elements one by one and places it to right position where it belongs in the sorted list of elements.
  • 59.
  • 60.
  • 61.
  • 62. • An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. • A 2D array is stored in the computer's memory one row following another. • If each data value of the array requires B bytes of memory, and if the array has C columns, then the memory location of an element such as score[m][n] is (m*c+n)*B from the address of the first byte.
  • 63.
  • 64.
  • 65.
  • 66. Initialization of a two dimensional array • Different ways to initialize two dimensional : • 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};
  • 67.
  • 68.
  • 69. Programs • Write a program to read 2-D array and print the elements of the array. • Find the sum of the elements of a 2-D array. • Write a program to find Transpose of a matrix. • Write a program to find multiplication of two matrices.
  • 70. Write a program to read 2-D array and print the elements of the array.
  • 71. Find the sum of the elements of a 2-D array.
  • 72.
  • 73.
  • 74. Write a program to find Transpose of a matrix.
  • 75.
  • 76. Write a program to find multiplication of two matrices.
  • 77.
  • 78.
  • 79.
  • 80. • String is a sequence of characters that is treated as a single data item and terminated by null character '0'. • Remember that C language does not support strings as a data type. • A string is actually one-dimensional array of characters in C language. • For example: The string "hello world" contains 12 characters including '0' character which is automatically added by the compiler at the end of the string.
  • 81.
  • 82. • What is NULL Char “0”? '0' represents the end of the string. It is also referred as String terminator & Null Character(ASCII value=0).
  • 83.
  • 84. Other ways to print string (character by character) :
  • 85.
  • 86. Read and print a string using scanf
  • 87.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93. Finding Length of a string Manually
  • 94. Finding reverse of a string Manually
  • 95. Concatenation of two strings Manually
  • 96.
  • 97.
  • 98. • User Defined Data type. • A way to store information in group variables, which can store dissimilar type of data. • Defining structure means creating new data type. int char float Double Member Variables Structure
  • 99.
  • 100.
  • 101.
  • 102. Reading and printing structure
  • 103. Reading and printing structure
  • 104.
  • 106.
  • 107. Array VS Sturcture Array • Using array only same type of data can be stored. • It is a derived data type. • It needs to be declared and then used. Structure • It can store dissimilar data as well. • User defined data type. • It needs to be define first only then we can use the variables of that type.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112. • User Defined Data Types. • Internally compiler treats the enumerators as integers. • Each value on the list of permissible values corresponds to an integer, starting with 0. • It helps in writing clear codes and simplify programming.