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

Unit 4

  • 1.
    Unit:4 Arrays &Basic Algorithms
  • 3.
    What is anarray? • 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 anarray • 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 isno 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: Tomany initializers.
  • 15.
    • No Error:No bound checking in this way.
  • 16.
  • 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 ofan array Length = upper_bound – lower_bound + 1
  • 22.
    To find theaddress 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 ArrayElements • Traversal • Insertion • Deletion • Merging • Search • Sorting
  • 24.
  • 26.
    Programs • Read anarray 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 interchangelargest 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 arrayof elements and print the sum of elements
  • 29.
    Copy an arrayof elements in another array.
  • 30.
    Copy an arrayof elements in another array in reverse order.
  • 31.
    Find the sumof even and odd numbers in an array.
  • 32.
    Find the sumof even and odd indexed elements.
  • 33.
    To put evenand odd elements in two separate array.
  • 35.
    Find the minimumand maximum of all elements of an array and print index as well.
  • 37.
    To arrange theelements in sorted order. Find the second minimum and second maximum of all elements of an array.
  • 39.
    To insert elementsin an array
  • 41.
    To delete anarray element
  • 45.
    To search anelement in an array using linear Search.
  • 46.
    To count occurrenceof an array element.
  • 47.
    To find whetheran array of integers contain a duplicate number.
  • 51.
  • 52.
    Selection Sort • Itsorts 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 • Simplestsorting 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 • Itworks 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 arrayof 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 atwo 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 aprogram 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 programto read 2-D array and print the elements of the array.
  • 71.
    Find the sumof the elements of a 2-D array.
  • 74.
    Write a programto find Transpose of a matrix.
  • 76.
    Write a programto find multiplication of two matrices.
  • 80.
    • String isa 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 isNULL Char “0”? '0' represents the end of the string. It is also referred as String terminator & Null Character(ASCII value=0).
  • 84.
    Other ways toprint string (character by character) :
  • 86.
    Read and printa string using scanf
  • 88.
  • 93.
    Finding Length ofa string Manually
  • 94.
    Finding reverse ofa string Manually
  • 95.
    Concatenation of twostrings Manually
  • 98.
    • User DefinedData 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.
  • 103.
  • 105.
  • 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 DefinedData 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.