Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Programming in C

Session 5
Agenda

• Recap
• Arrays
• Single dimensional array and 2-Dimensional array
• Matrix Operation
• Strings
• String- An Array
• Binary Search

2
Recap

• Branching
 If Statement
 If .. Else Statement
 Nesting If.. Else Statement
• Looping
 If Statement
 For.. Statement
 While Statement
 Do..While Statement
• Switch Statement
 Break Statement

3
C Arrays
• An Arrays can store a fixed-size sequential collection
of elements of the same type
 An array is used to store a collection of data
 An array as a collection of variables of the same
type
 All arrays consist of contiguous memory locations
 To access a value, one requires Array name and
index number
C Arrays
• An Arrays can store a fixed-size sequential collection
of elements of the same type
 An array is used to store a collection of data
 An array as a collection of variables of the same
type
 All arrays consist of contiguous memory locations
 To access a value, one requires Array name and
index number
C Multidimensional Array
• C programming language allows multidimensional
arrays
• Declaring a multidimensional array
 data type array_name[size1][size2]….[sizeN];
 An example of 3-Dimensional integer array
 int Roll_number[2][2][1]
 2-dimensional arrays are widely used
 2-D array is represented as a matrix with rows
and column
 int Roll_Number[4][2] is a 2-D array with 4 Row and 2 Column
C 2-D Array
• Initialising a 2-D array
 int Roll_Number[4][2]={{1,2},{3,4},{5,6},
{7,8}};

 Roll_Number[2][1] will fetch data from 3rd row


and second column i.e. Roll_Number[2][1]=6
Matrix Operation

• Basic Matrix Operations are


Addition
 Size(row and column) of the two (more) matrix should be equal
Subtraction
 Size(row and column) of the two (more) matrix should be equal
Division
 Size(row and column) of the two (more) matrix should be equal
 Division by zero not allowed
Multiplication
 Column size of first matrix=Row size of second matrix
Transpose
Matrix Operation

• Basic Matrix Operations are


Addition
 Size(row and column) of the two (more) matrix should be equal
Subtraction
 Size(row and column) of the two (more) matrix should be equal
Division
 Size(row and column) of the two (more) matrix should be equal
 Division by zero not allowed
Multiplication
 Column size of first matrix=Row size of second matrix
Transpose
Matrix Operation-Addition
Matrix Operation-Subtraction
Matrix Operation-Division
Matrix Operation-Multiplication

R_1_A – Row 1 of Matrix A


Matrix Operation-Multiplication

R_1_A – Row 1 of Matrix A


Matrix Operation-Transpose
Matrix Operation-Addition
Matrix Operation-Subtraction
Matrix Operation-Division
Matrix Operation-Multiplication

R_1_A – Row 1 of Matrix A


Matrix Operation-Multiplication

R_1_A – Row 1 of Matrix A


Matrix Operation-Transpose
String in C
• String is an one dimensional array
• It is an array of characters
• It is terminated by NULL character or ‘\0’
• Declaring and initializing a string
 char name[7]={‘A’,’B’,’C’,’D’,’E’,’F’,’\0’};
 char name[]=“ABCDEF”;/*C automatically adds NULL at the
end
String in C
• String is an one dimensional array
• It is an array of characters
• It is terminated by NULL character or ‘\0’
• Declaring and initializing a string
 char name[7]={‘A’,’B’,’C’,’D’,’E’,’F’,’\0’};
 char name[]=“ABCDEF”;/*C automatically adds NULL at the
end
String in C
• String is an one dimensional array
• It is an array of characters
• It is terminated by NULL character or ‘\0’
• Declaring and initializing a string
 char name[7]={‘A’,’B’,’C’,’D’,’E’,’F’,’\0’};
 char name[]=“ABCDEF”;/*C automatically adds NULL at the
end
Linear Search
• Linear search is an algorithm to search a
value/character in an array
• This is a sequential search
1. Start with the first value in the given array
2. If the match is found the algorithm stops
3. Else it goes to the next value in the array
4. Repeat step 2 & 3 till it reaches the end of array
Thank You

You might also like