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

"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSneha Padhiar
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical trainingGladiatorsKasper
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfDrew Moseley
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Romil Mishra
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptJohnWilliam111370
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectSTATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectGayathriM270621
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmDeepika Walanjkar
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTSneha Padhiar
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 

Recently uploaded (20)

Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectSTATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subject
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 

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.