0% found this document useful (0 votes)
2 views31 pages

Array and String

The document covers Unit II of a C Programming course focusing on Arrays and Strings. It explains the concepts of one-dimensional and two-dimensional arrays, their declarations, initializations, and operations such as mean, median, and mode calculations. Additionally, it includes example programs demonstrating array operations like addition, multiplication, and transposition of matrices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Array and String

The document covers Unit II of a C Programming course focusing on Arrays and Strings. It explains the concepts of one-dimensional and two-dimensional arrays, their declarations, initializations, and operations such as mean, median, and mode calculations. Additionally, it includes example programs demonstrating array operations like addition, multiplication, and transposition of matrices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

lOMoARcPSD|20892706

UNIT II Arrays AND Strings

C Programming and Data Structures (Anna University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])
lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

UNIT IIARRAYS AND STRINGS

Introduction to Arrays: Declaration, Initialization – One dimensional array –


Example Program: Computing Mean, Median and Mode - Two dimensional
arrays – Example Program: Matrix Operations (Addition, Scaling,
Determinant and Transpose) - String operations: length, compare,
concatenate, copy – Selection sort, linear and binary search

2.1 Introduction to Arrays

An Array is a collection of similar data elements. These data elements have


the same data type. The elements of the array are stored in consecutive memory
locations and are referenced by an index (also known as subscript). Subscript
indicates an ordinal number of the elements counted from the beginning of the
array.

Definition:

An array is a data structure that is used to store data of the same type. The position
of an element is specified with an integer value known as index or subscript.

E.g.
1 3 5 2
a(integer array)

1.2 3.5 5.4 2.1


b(float array )

[0] [1] [2] [3]

Characteristics:

1 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

i) All the elements of an array share a common name called as array name

ii) The individual elements of an array are referred based on their position.

iii) The array index in c starts with 0.

In general arrays are classified as:

1. Single dimensional array

2. Multi-dimensional array

2.2 Declarations of Arrays

Array has to be declared before using it in C Program. Declaring Array means


specifying three things.

Data_type Data Type of Each Element of the array

Array_name Valid variable name

Size Dimensions of the Array

Arrays are declared using the following syntax:

type name[size]

Here the type can be either int, float, double, char or any oher valid data type. The
number within the brackets indicates the size of the array, i.e., the maximum
number of elements that can be stored in the array.

ex: int marks[10]

2 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

2.3 Initialization of arrays

Elements of the array can also be initialized at the time of declaration as in


the case of every other variable. When an array is initialized, we need to provide a
value for every element in the array. Arrays are initialized by writing,

type array_name[size] = { list of values};

The values are written with curly brackets and every value is separated by a
comma. It is a compiler error to specify more number of values than the number of
elements in the array.

ex: int marks[5] = {90, 92, 78, 82, 58};

2.4 One dimensional Array


• It is also known as one-dimensional arrays or linear array or vectors
• It consists of fixed number of elements of same type
• Elements can be accessed by using a single subscript. eg) a[2]=9;
Eg)
1 3 5 2

a
[0] [1] [2] [3] subscripts or indices

Declaration of Single Dimensional Array


Syntax:
datatype arrayname [array size];

E.g. int a[4]; // a is an array of 4 integers


char b[6]; //b is an array of 6 characters

3 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

Initialization of single dimensional array


Elements of an array can also be initialized.
Rules
a) Elements of an array can be initialized by using an initialization list. An
initialization list is a comma separated list of initializers enclosed within braces.
Eg) int a[3]={1,3,4};
b) If the number of initializers in the list is less than array size, the leading array
locations gets initialized with the given values. The rest of the array locations gets
initialized to
0 - for int array
0.0 - for float array
\0 - for character array
Eg) int a[2]={1};
1 0 a

char b[5]={‘A’.’r’,’r’};

b ‘A’ ‘r’ ‘r’ ‘\0’ ‘\0’

Usage of single dimensional array


The elements of single dimensional array can be accessed by using a
subscript operator([]) and a subscript.

Reading storing and accessing elements:


An iteration statement (i.e loop) is used for storing and reading elements.

4 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

Ex:1 Program to calculate the average marks of the class


#include <stdio.h>
void main()
{
int m[5],i,sum=0,n;
float avg;
printf(“enter number of students \n”);
scanf(“%d”,&n);
printf(“enter marks of students \n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&m[i]);
}
for(i=0;i<n;i++)
sum=sum+m[i];
avg=(float)sum/n;
printf(“average=%f”,avg);
}
Output:
Enter number of students
5
Enter marks of students
55
60
78
85
90

5 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

Average=73.6

2.5 Example Programs


C Program to Find Mean, Median, and Mode of Given Numbers.
#define SIZE 100
#include"stdio.h"
float mean_function(float[],int);
float median_function(float[],int);
float mode_function(float[],int);
int main()
{
int i,n,choice;
float array[SIZE],mean,median,mode;
printf("Enter No of Elements\n");
scanf("%d",&n);
printf("Enter Elements\n");
for(i=0;i
scanf("%f",&array[i]);
do
{
printf("\n\tEnter Choice\n\[Link]\n\[Link]\n\[Link]\[Link]");
scanf("%d",&choice);
switch(choice)
{
case 1: mean=mean_function(array,n);
printf("\n\tMean = %f\n",mean);
break;

6 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

case 2: median=median_function(array,n);
printf("\n\tMedian = %f\n",median);
break;
case 3: mode=mode_function(array,n);
printf("\n\tMode = %f\n",mode);
break;
case 4: break;
default:printf("Wrong Option");
break;
}
}while(choice!=4);
}
float mean_function(float array[],int n)
{
int i;
float sum=0;
for(i=0;i
sum=sum+array[i];
return (sum/n);
}
float median_function(float a[],int n)
{
float temp;
int i,j;
for(i=0;i
for(j=i+1;j
{

7 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
if(n%2==0)
return (a[n/2]+a[n/2-1])/2;
else
return a[n/2];
}
float mode_function(float a[],int n)
{
return (3*median_function(a,n)-2*mean_function(a,n));
}
Output
Enter Elements
2
3
4

Enter Choice
[Link]
[Link]
[Link]
[Link]

8 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

Mean = 3.000000

Enter Choice
[Link]
[Link]
[Link]
[Link]
2

Median = 3.000000
Enter Choice
[Link]
[Link]
[Link]
[Link]
3
Mode = 3.000000
Enter Choice
[Link]
[Link]
[Link]
[Link]
4

9 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

2.6 Two dimensional Array


• A 2D array is an array of 1-D arrays and can be visualized as a plane that has
rows and columns.
• The elements can be accessed by using two subscripts, row subscript (row
no), column subscript(column no).
• It is also known as matrix.
E.g,
1 2 3 6 7
9 10 5 0 4
a[3][5] 3 1 2 1 6

Declaration
datatype arrayname [row size][column size]

e.g) int a [2][3]; //a is an integer array of 2 rows and 3 columns


number of elements=2*3=6

Initialization
1. By using an initialization list, 2D array can be initialized.
e.g. int a[2][3] = {1,4,6,2}

1 4 6
2 0 0
a

2. The initializers in the list can be braced row wise.


e.g int a[2][3] = {{1,4,6} , {2}};

10 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

2.7 Example Programs


Progarm for addition,transpose and multiplication of array
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,k,j,c1,c2,r1,r2;
int m1[10][10],m2[10][10],m3[10][10];
clrscr();
while(1)
{

printf("\n 1. Transpose of Matrix:-\n");


printf("\n 2. Addition of Matrix:-\n");
printf("\n 3. Multiplication of Matrix:-\n");
printf("\n 4. Exit\n");
printf("\n Enter your choice:-");
scanf("%d",&a);
switch(a)
{
case 1 :
printf("\n Enter the number of row and coloum:-");
scanf("%d%d",&r1,&c1);
printf("\n Enter the element :-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)

11 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

{
scanf("%d",&m1[i][j]);
m2[j][i]=m1[i][j];
}
}
/*Displaying transpose of matrix*/
printf("\n Transpose of Matrix is:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("\t%d",m2[i][j]);
printf("\n");
}
break;
case 2:
printf("\n how many row and coloum in Matrix one:-");
scanf("%d%d",&r1,&c1);
printf("\n How amny row and coloum in Matrix two:-");
scanf("%d%d",&r2,&c2);
if((r1==r2)&&(c1==c2))
{
printf("\n Addition is possible:-");
printf("\n Input Matrix one:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);

12 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

}
printf("\n Input Matrix two:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
/* Addition of Matrix*/
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
m3[i][j]=m1[i][j]+ m2[i][j];
}
printf("\n The sum is:-\n");
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
printf("%5d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Addition is not possible:-");

break;
case 3:

13 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

printf("\n Enter number of row and coloum in matrix one:-");


scanf("%d%d",&r1,&c1);
printf("\n Enter number of row and coloum in matrix two:-");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("\n Multiplication is possible:-");
printf("\n Input value of Matrix one:-");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("\n Input value of Matrix two:-");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
m3[i][j]=0;
for(k=0;k<c1;k++)
m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j];
}
/*Displaying final matrix*/

14 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

printf("\n Multiplication of Matrix:-\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Multiplication is not possible");

break;
case 4:
exit(0);
break;
}
getch();
}
}

2.8 String Operations


Definition:
The group of characters, digits, & symbols enclosed within double quotes is
called as Strings. Every string is terminated with the NULL (‘\0’) character.
E.g. “INDIA” is a string. Each character of string occupies 1 byte of
memory. The last character is always ‘\0’.
Declaration:

15 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

String is always declared as character arrays.


Syntax

char stringname[size];

E.g. char a[20];


Initialization:
We can use 2 ways for initializing.
1. By using string constant
E.g. char str[6]= “Hello”;
2. By using initialisation list
E.g. char str[6]={‘H’, ‘e’, ‘l’, ;l’, ;o’, ‘\0’};

2.9 String Operations or String Functions


These functions are defined in string.h header file.
1. strlen() function
It is used to find the length of a string. The terminating character (‘\0’) is not
counted.
Syntax
temp_variable = strlen(string_name);

E.g.
s= “hai”;
strlen(s)-> returns the length of string s i.e. 3.
2. strcpy() function
It copies the source string to the destination string
Syntax

16 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

strcpy(destination,source);

E.g.
s1=“hai”;
s2= “welcome”;
strcpy(s1,s2); -> s2 is copied to s1. i.e. s1=welcome.
3. strcat() function
It concatenates a second string to the end of the first string.
Syntax

strcat(firststring, secondstring);

E.g.
s1=“hai ”;
s2= “welcome”;
strcat(s1,s2); -> s2 is joined with s1. Now s1 is hai welcome.
E.g. Program:
#include <stdio.h>
#include <string.h>
void main ()
{
char str1[20] = "Hello";
char str2[20] = "World";
char str3[20];
int len ;
strcpy(str3, str1);
printf("Copied String= %s\n", str3 );

17 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

strcat( str1, str2);


printf("Concatenated String is= %s\n", str1 );
len = strlen(str1);
printf("Length of string str1 is= %d\n", len );
return 0;
}
Output:
Copied String=Hello
Concatenated String is=HelloWorld
Length of string str1is

4. strcmp() function
It is used to compare 2 strings.
Syntax
temp_varaible=strcmp(string1,string2)
;

• If the first string is greater than the second string a positive number is
returned.
• If the first string is less than the second string a negative number is
returned.
• If the first and the second string are equal 0 is returned.

5. strlwr() function
It converts all the uppercase characters in that string to lowercase characters.
Syntax

strlwr(string_name);

18 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

E.g.
str[10]= “HELLO”;
strlwr(str);
puts(str);
Output: hello

6. strupr() function
It converts all the lowercase characters in that string to uppercase characters.
Syntax

strupr(string_name);

E.g.
str[10]= “HEllo”;
strupr(str);
puts(str);
Output: HELLO

7. strrev() function
It is used to reverse the string.
Syntax

strrev(string_name);

E.g.
str[10]= “HELLO”;
strrev(str);
puts(str);
Output: OLLEH

19 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

String functions
Functions Descriptions
strlen() Determines the length of a String
strcpy() Copies a String from source to destination
strcmp() Compares two strings
strlwr() Converts uppercase characters to lowercase
strupr() Converts lowercase characters to uppercase
strdup() Duplicates a String
strstr() Determines the first occurrence of a given String in another string
strcat() Appends source string to destination string
strrev() Reverses all characters of a string

Example: String Comparison


void main()
{
char s1[20],s2[20];
int val;
printf(“Enter String 1\n”);
gets(s1);
printf(“Enter String 2\n”);
gets (s2);
val=strcmp(s1,s2);
if (val==0)
printf(“Two Strings are equal”);
else
printf(“Two Strings are not equal”);
getch();
20 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

}
Output:
Enter String 1
Computer
Enter String 2
Programming
Two Strings are not equal

2.8.1 String Arrays


They are used to store multiple strings. 2-D char array is used for string
arrays.
Declaration

char arrayname[rowsize][colsize];
E.g.
char s[2][30];
Here, s can store 2 strings of maximum 30 characters each.
Initialization
2 ways
1. Using string constants
char s[2][20]={“Ram”, “Sam”};
2. Using initialization list.
char s[2][20]={ {‘R’, ‘a’, ‘m’, ‘\0’},
{‘S’, ‘a’, ‘m’, ‘\0’}};
E.g. Program
#include<stdio.h>
void main()
{

21 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

int i;
char s[3][20];
printf(“Enter Names\n”);
for(i=0;i<3;i++)
scanf(“%s”, s[i]);
printf(“Student Names\n”);
for(i=0;i<3;i++)
printf(“%s”, s[i]);
}

2.9 Sorting
Sorting is the process of arranging elements either in ascending or in descending
order.
Sorting Methods
1. Selection Sort
2. Bubble Sort
3. Merge sort
4. Quick sort

1. Selection sort
It finds the smallest element in the list & swaps it with the element present at
the head of the list.
E.g.
25 20 15 10 5
5 20 15 10 25
5 10 15 20 25

22 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

2. Bubble Sort
In this method, each data item is compared with its neighbour element. If
they are not in order, elements are exchanged.
With each pass, the largest of the list is "bubbled" to the end of the list.
E.g.
Pass 1:
25 20 15 10 5
20 25 15 10 5
20 15 25 10 5
20 15 10 25 5
20 15 10 5 25
25 is the largest element
Repeat same steps until the list is sorted

3. Merge Sort:
• Merge sort is based on Divide and conquer method.
• It takes the list to be sorted and divide it in half to create two unsorted
lists.
• The two unsorted lists are then sorted and merged to get a sorted list.

23 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

4. Quick Sort
• This method also uses the technique of ‘divide and conquer’.
• Pivot element is selected from the list, it partitions the rest of the list into
two parts – a sub-list that contains elements less than the pivot and other
sub-list containing elements greater than the pivot.
• The pivot is inserted between the two sub-lists. The algorithm is recursively
applied to sort the elements.

24 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

Program:
#include <stdio.h>
void main()
{
int i, j, temp, n, a[10];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];

25 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

a[i] = a[j];
a[j] = temp;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
printf("The numbers arranged in descending order are given below \n");
for(i=n-1;i>=0;i--)
printf("%d\n",a[i]);
}
Output:
Enter the value of N
4
Enter the numbers
10 2 5 3
The numbers arranged in ascending order are given below
2
3
5
10
The numbers arranged in descending order are given below
10
5
3
2

26 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

2.10 Searching
Searching is an operation in which a given list is searched for a particular
value. If the value is found its position is returned.
Types:
1. Linear Search
2. Binary Search
1. Linear Search
The search is linear. The search starts from the first element & continues in a
sequential fashion till the end of the list is reached. It is slower method.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,m,c=0;
clrscr();
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
printf("Enter the number to be searched: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
if(a[i]==m)
{

27 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

printf("Element is in the position %d\n",i+1);


c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
getch();
}

Output:
Enter the size of an array: 4
Enter the elements of the array: 4 3 5 1
Enter the number to be search: 5
Element is in the position 3

2. Binary Search
• If a list is already sorted then we can easily find the element using
binary serach.
• It uses divide and conquer technique.
Steps:
1. The middle element is tested with searching element. If found, its
position is returned.
2. Else, if searching element is less than middle element, search the left half
else search the right half.
3. Repeat step 1 & 2.
Program:

28 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

#include<stdio.h>
void main()
{
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be searched: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else if(m<a[mid])
{
u=mid-1;
}
else
l=mid+1;
}

29 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])


lOMoARcPSD|20892706

CS8251 Programming in C UNIT II

if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
}
Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.
Example:
3 5 7 9 11
Search key=7 middle element=7
Searching element=middle element. So the element is found.
Search key=11
Middle element=7
Searching element>middle
So go to right half: 9 11. Repeat steps until 11 is found or list ends.

30 B. Shanmuga Sundari [Link]

Downloaded by AISWARYA MADHAVAN S ([Link]@[Link])

You might also like