Module 3
Module 3
Module 3
( Arrays, Matrix, Strings, Searching and Sorting)
1. What is Arrays and Matrix? How to declare and initialize 1-D and 2-D Arrays? Explain
with an example.
2. Develop C Program for the following Problems:
i) To display principal diagonal elements of Matrix
ii) To Find the sum of two matrices
ii) To generate Fibonocci series using arrays
iv) To find the sum of array elements
v) To find the largest elements of an array
3. What is searching and sorting? With an example explain linear search , Binary search,
Bubble sort and selection sorting Algorithms
4. Sort the following elements using: i) Selection Sort ii) Bubble Sort
89 78 65 90 75 40
5. Write a C program for the following
a. Linear Search
b. Binary search
c. Selection sort
d. Bubble sort
6. What is String in C? With an example explain any six string handling functions in C
7. Write a C Program to find the length of string, copy the content of one string to other
string, concatenate two string and compare two strings without using built in functions
What is arrays? What are the different types of arrays? How to declare and initialize arrays? Explain
with an example
Array is a collection of homogeneous elements (ie; the collection of elements with same data types)
Why do we need arrays?
We can use normal variables (a1, a2, a3, ..) when we have a small number of objects, but if we
want to store a large number of instances, it becomes difficult to manage them with normal
variables. The idea of an array is to represent many instances in one variable.
• Without using arrays: int a1,a2,a3,a4,a5;
• With array: int a[5]; need to remember only one variable name.
Types of an arrays:
One Dimensional arrays : Known as List Ex: int A[10];
Multi Dimensional (Ex: Two Dimensional) arrays: Ex: int B[5][5];
How to declare arrays?
• One Dimensional array:
Syntax:
Datatype Arrayname[Size];
Data_type Array_name[RowSize][ColSize];
float Bal[5][10];
i/j 0 1 2
Some Example:
Character Array(Strings):
What is strings? How to declare and initialize strings? Explain with an example
What is string?
What are String Handling Functions/String manipulation functions? Explain with an example OR
Demonstrate the use of of strlen(), strcpy(), strcat() strcmp() functions
• C supports a large number of string handling functions in the standard library "string.h".
• #include <string.h> to be used
strlen(): The strlen() function calculates the length of a given string. The strlen() function is
defined in string.h header file. It doesn’t count null character ‘\0’.
Syntax:
length=strlen(str);
Parameter:
str: It represents the string variable whose length we have to find.
Return: This function returns the length of string passed.
Ex: str=”VTU”;
len=strlen(str); // ie;now len=3
strcat(): The strcat() function will append a copy of the source string to the end of destination
string. The strcat() function takes two arguments:
1) dest
2) src
It will append copy of the source string in the destination string. The terminating character at
the end of dest is replaced by the first character of src .
Return value: The strcat() function returns dest, the pointer to the destination string.
Syntax:
strcat(dest, src);
Ex: dest=”VTU”;
src=”Belegavi”;
strcat(dest,src);// ie; now dest=VTUBelegavi
strcpy(): strcpy() is a standard library function in Cand is used to copy one string to another. In
C it is present in string.h header file
Syntax:
strcpy(dest, src);
dest: Pointer to the destination array where the content is to be copied.
src: string which will be copied.
After copying the source string to the destination string, the strcpy() function returns a pointer to
the destination string.
Ex:
strcpy(city,”Delhi”);// ie; Delhi is copied to the string city
Below programs illustrate the strlen(),strcpy() and strcat() function in C:
strcmp(): strcmp() is a built-in library function and is declared in <string.h> header file.
This function takes two strings as arguments and compare these two strings
lexicographically.
Syntax::
f=strcmp(str1,str2);
In the above prototype, function srtcmp takes two strings as parameters and returns an
integer value based on the comparison of strings.
strcmp() compares the two strings lexicographically means it starts comparison
character by character starting from the first character until the characters in both
strings are equal or a NULL character is encountered.
If first character in both strings are equal, then this function will check the second
character, if this is also equal then it will check the third and so on
This process will be continued until a character in either string is NULL or the
characters are unequal.
If strings are equal then it will return 0 , otherwise nonzero
Below programs illustrate the strcmp() function in C:
Ex:
strcmp(name1,name2);
strcmp(name1,”John”);
strcmp(“RAM”,”ROM”);
Write a C Program to find the length of string, copy the content of one string to other string,
concatenate two string and compare two strings without using built in functions
Program:
#include <stdio.h>
char s1[20],s2[20],s3[20];
/* Function to find string Length*/
int stringlen(char s1[])
{
int len=0,i;
for(i=0;s1[i]!='\0';i++)
len++;
return len;
}
/*Function to Compare string*/
void stringcmp(char s1[],char s2[])
{
int i=0,f=0;
while(s1[i]!='\0'||s2[i]!='\0')
{
if(s1[i]!=s2[i])
{
f=1; //srings are different
break;
}
i++;
}
if(f==0)
printf (" Strings are Equal\n"); Flow chart : for stringcmp()
else
printf (" Strings are Different\n");
}
/*Function to Concatinate strings*/
void stringcat(char a[],char b[])
{
int j,i;
i=stringlen(a);
for(j=0;b[j]!='\0';j++,i++)
a[i]=b[j];
a[i]='\0';
puts(a);
}
Example
Consider the following list of elements and the element to be searched...
Binary Search:
Binary Search is a type of searching algorithm. A searching algorithm means you find an item with
a particular value in a sorted sequence. Binary search is a type of searching algorithm which finds
an item by repeatedly halving the search space.
Explanation of Binary Search:
Binary Search: Steps on how it works:
To find the index of element key with a certain value:
1. Start with an array sorted in Ascending/descending order.
2. In each step: Pick the middle element of the array mid and compare it to key. If element
values are equal, then return index of mid. If key is greater than mid, then key must be in
right subarray. If key is less than mid, then key must be in the left subarray.
3. Repeat those steps on new subarray.
Search whether key =76 present or not in the array given below using binary search
algorithm
Output:
Enter size of array: 3
Enter array element (ascending order)
123
Enter the element to search: 6
Element not found
-------------------------------------------------------------------------------------------------------------------
2. The same process goes on for the remaining iterations. After each iteration, the largest element
among the unsorted elements is placed at the end.
In each iteration, the comparison takes place up to the last unsorted element.
The array is sorted when all the unsorted elements are placed at their correct positions.
}
Output:
Enter number of elements
5
Enter 5 integers
62501
Sorted list in ascending order:
0 1 2 5 6
Selection Sort:
First, we give the computer a list of unsorted numbers and store them in an array of memory cells.
To begin the sort, the computer divides the sorted and unsorted sections of the list by placing a marker
before the first number. To sort the numbers, the computer will repeatedly search the unsorted section
for the smallest number, swap this number with the first number in the unsorted section, and update the
sort marker.
To find the smallest number in the unsorted section, the computer must make six comparisons: (7 < 8),
(7 > 5), (5 > 2), (2 < 4), (2 < 6), and (2 > 3). After these comparisons, the computer knows that 2 is the
smallest number, so it swaps this number with 7, the first number in the unsorted section, and advances
the sort marker.
+
Now five more comparisons are needed to find the smallest number in the unsorted section: (8 > 5), (5 < 7),
(5 > 4), (4 < 6), and (4 > 3). After these comparisons, the computer swaps 3, the smallest number in the
unsorted section, with 8, the first number in the unsorted section, and advances the sort marker.
This time four comparisons are needed to determine that 4 is the smallest number in the unsorted section:
(5 < 7), (5 > 4), (4 < 6), and (4 < 8). After these comparisons, the computer swaps 4 with 5 and then
advances the sort marker.
After three more comparisons, the computer identifies 5 as the smallest unsorted number: (7 > 5), (5 < 6),
and (5 < 8). Then the computer swaps 5 with 7 and advances the sort marker.
This time only two comparisons are needed to determine that 6 is the smallest number: (7 > 6) and (6 < 8).
After these two comparisons, the computer swaps 6 with 7 and then advances the sort marker.
Now we only need a single comparison to find the right position for 7: (7 < 8). Since 7 is the smallest
number and it is also the first number in the unsorted section, the computer does not need to swap this
number. It only needs to advance the sort marker. Now there is only one number in the unsorted section, so
the list of numbers is sorted and the Selection Sort algorithm is complete.