SlideShare a Scribd company logo
Unit:4 Arrays & Basic Algorithms
Unit 4
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.
Unit 4
Unit 4
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.
Unit 4
Unit 4
Unit 4
Unit 4
• 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.
Unit 4
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+);
}
Unit 4
Unit 4
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
Unit 4
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.
Unit 4
Find the minimum and maximum of all
elements of an array and print index as well.
Unit 4
To arrange the elements in sorted order. Find the second
minimum and second maximum of all elements of an array.
Unit 4
To insert elements in an array
Unit 4
To delete an array element
Unit 4
Unit 4
Unit 4
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.
Unit 4
Unit 4
Unit 4
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
Unit 4
Unit 4
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
Unit 4
Unit 4
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.
Unit 4
Unit 4
Unit 4
• 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.
Unit 4
Unit 4
Unit 4
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};
Unit 4
Unit 4
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.
Unit 4
Unit 4
Write a program to find Transpose of a matrix.
Unit 4
Write a program to find multiplication of two matrices.
Unit 4
Unit 4
Unit 4
• 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.
Unit 4
• What is NULL Char “0”?
'0' represents the end of the string. It is also
referred as String terminator & Null
Character(ASCII value=0).
Unit 4
Other ways to print string (character
by character) :
Unit 4
Read and print a string using scanf
Unit 4
Other way can be:
Unit 4
Unit 4
Unit 4
Unit 4
Finding Length of a string Manually
Finding reverse of a string Manually
Concatenation of two strings Manually
Unit 4
Unit 4
• 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
Unit 4
Unit 4
Unit 4
Reading and printing structure
Reading and printing structure
Unit 4
Structure using Function
Unit 4
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.
Unit 4
Unit 4
Unit 4
Unit 4
• 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

PDF
PDF
An Introduction to the C++ Standard Library
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
PPT
linked list (c#)
PDF
Arrays in c_language
PPTX
Concept Of C++ Data Types
 
PPTX
Concept of c data types
PPTX
LISP: Input And Output
An Introduction to the C++ Standard Library
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
linked list (c#)
Arrays in c_language
Concept Of C++ Data Types
 
Concept of c data types
LISP: Input And Output

What's hot (20)

PPTX
Data types in C
PPTX
single linked list
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
PDF
Introduction to c++ ppt
PPTX
Data structure using c module 1
PPTX
LISP: Data types in lisp
PPTX
Python Data-Types
PPT
Linked list
PPTX
Linked list
PPT
C++ data types
PDF
+2 Computer Science - Volume II Notes
PPTX
Doubly linked list
PDF
Data Structures & Algorithm design using C
PPTX
Linked Lists
PPTX
C programming
PPTX
Datatype in c++ unit 3 -topic 2
PPT
List Data Structure
PPTX
Linked List
PPTX
Basic Data Types in C++
PDF
Python-03| Data types
Data types in C
single linked list
358 33 powerpoint-slides_8-linked-lists_chapter-8
Introduction to c++ ppt
Data structure using c module 1
LISP: Data types in lisp
Python Data-Types
Linked list
Linked list
C++ data types
+2 Computer Science - Volume II Notes
Doubly linked list
Data Structures & Algorithm design using C
Linked Lists
C programming
Datatype in c++ unit 3 -topic 2
List Data Structure
Linked List
Basic Data Types in C++
Python-03| Data types
Ad

Similar to Unit 4 (20)

PPTX
PPTX
MODUL new hlgjg thaybkhvnghgpv7E_02.pptx
PPTX
Arrays In C++
PPTX
Array ppt
PDF
Array.pdf
PPTX
Arrays
PPTX
arrayppt.pptx
PPTX
ARRAYS.pptx
PPTX
ARRAY PPT.pptx for mca finals placement
PPTX
Arrays in C.pptx
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PPTX
DS Module1 (1).pptx
PPTX
Data Structures - Array presentation .pptx
PPTX
Arrays.pptx
PPT
Presentation of array
PPTX
Arrays.pptx
PPTX
Advance Algorithm_unit_2_czcbcnhgjy.pptx
PPTX
arrays.pptx
PPTX
Unit 2 linear data structures
PPTX
PPT Lecture 2.2.1 onn c++ data structures
MODUL new hlgjg thaybkhvnghgpv7E_02.pptx
Arrays In C++
Array ppt
Array.pdf
Arrays
arrayppt.pptx
ARRAYS.pptx
ARRAY PPT.pptx for mca finals placement
Arrays in C.pptx
Unit4pptx__2024_11_ 11_10_16_09.pptx
DS Module1 (1).pptx
Data Structures - Array presentation .pptx
Arrays.pptx
Presentation of array
Arrays.pptx
Advance Algorithm_unit_2_czcbcnhgjy.pptx
arrays.pptx
Unit 2 linear data structures
PPT Lecture 2.2.1 onn c++ data structures
Ad

More from SHIKHA GAUTAM (16)

PDF
Agreement Protocols, distributed File Systems, Distributed Shared Memory
PPTX
Distributed Mutual Exclusion and Distributed Deadlock Detection
PPTX
Distributed Systems Introduction and Importance
PDF
Unit iii
PDF
Unit ii_KCS201
DOCX
Type conversion in c
PDF
C intro
PDF
4. algorithm
PDF
3. basic organization of a computer
PDF
Generations of computer
PDF
c_programming
PPTX
Data Mining
PPTX
Warehouse Planning and Implementation
PPTX
Data Warehousing
PPTX
Dbms Introduction and Basics
PPTX
Agreement Protocols, distributed File Systems, Distributed Shared Memory
Distributed Mutual Exclusion and Distributed Deadlock Detection
Distributed Systems Introduction and Importance
Unit iii
Unit ii_KCS201
Type conversion in c
C intro
4. algorithm
3. basic organization of a computer
Generations of computer
c_programming
Data Mining
Warehouse Planning and Implementation
Data Warehousing
Dbms Introduction and Basics

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Well-logging-methods_new................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
573137875-Attendance-Management-System-original
PPTX
CH1 Production IntroductoryConcepts.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT 4 Total Quality Management .pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Geodesy 1.pptx...............................................
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Well-logging-methods_new................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Construction Project Organization Group 2.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
573137875-Attendance-Management-System-original
CH1 Production IntroductoryConcepts.pptx

Unit 4

  • 1. Unit:4 Arrays & Basic Algorithms
  • 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.
  • 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.
  • 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.
  • 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+); }
  • 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
  • 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.
  • 35. Find the minimum and maximum of all elements of an array and print index as well.
  • 37. To arrange the elements in sorted order. Find the second minimum and second maximum of all elements of an array.
  • 39. To insert elements in an array
  • 41. To delete an array element
  • 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.
  • 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
  • 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
  • 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.
  • 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.
  • 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};
  • 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.
  • 74. Write a program to find Transpose of a matrix.
  • 76. Write a program to find multiplication of two matrices.
  • 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.
  • 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).
  • 84. Other ways to print string (character by character) :
  • 86. Read and print a string using scanf
  • 93. Finding Length of a string Manually
  • 94. Finding reverse of a string Manually
  • 95. Concatenation of two strings Manually
  • 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
  • 102. Reading and printing structure
  • 103. Reading and printing structure
  • 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.
  • 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.