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.
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
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.
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) :
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
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.