SlideShare a Scribd company logo
/* Write C programs that use both recursive and non recursive functions to perform

    the following searching operations for a Key value in a given list of integers :

    ii) Binary search*/



#include <stdio.h>

#define MAX_LEN 10



/* Non-Recursive function*/

void b_search_nonrecursive(int l[],int num,int ele)

{

    int l1,i,j, flag = 0;

    l1 = 0;

    i = num-1;

    while(l1 <= i)

    {

        j = (l1+i)/2;

        if( l[j] == ele)

        {

               printf("nThe element %d is present at position %d in listn",ele,j);

                        flag =1;

                        break;

        }

        else

                 if(l[j] < ele)
l1 = j+1;

                 else

                           i = j-1;

    }

    if( flag == 0)

    printf("nThe element %d is not present in the listn",ele);

}



/* Recursive function*/

int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)

{

    int m,pos;

    if (arrayStart<=arrayEnd)

    {

        m=(arrayStart+arrayEnd)/2;

        if (l[m]==a)

         return m;

        else if (a<l[m])

         return b_search_recursive(l,arrayStart,m-1,a);

        else

         return b_search_recursive(l,m+1,arrayEnd,a);

    }

    return -1;

}
void read_list(int l[],int n)

{

    int i;

    printf("nEnter the elements:n");

    for(i=0;i<n;i++)

       scanf("%d",&l[i]);

}



void print_list(int l[],int n)

{

    int i;

    for(i=0;i<n;i++)

       printf("%dt",l[i]);

}



/*main function*/

void main()

{

    int l[MAX_LEN], num, ele,f,l1,a;

    int ch,pos;



    clrscr();
printf("======================================================");

printf("ntttMENU");

printf("n=====================================================");

printf("n[1] Binary Search using Recursion method");

printf("n[2] Binary Search using Non-Recursion method");

printf("nnEnter your Choice:");

scanf("%d",&ch);



if(ch<=2 & ch>0)

{

    printf("nEnter the number of elements : ");

    scanf("%d",&num);

    read_list(l,num);

    printf("nElements present in the list are:nn");

    print_list(l,num);

    printf("nnEnter the element you want to search:nn");

    scanf("%d",&ele);




switch(ch)

{

    case 1:printf("nRecursive method:n");

            pos=b_search_recursive(l,0,num,ele);

            if(pos==-1)
{

                        printf("Element is not found");

               }

               else

               {

                        printf("Element is found at %d position",pos);

               }

               getch();

               break;



        case 2:printf("nNon-Recursive method:n");

               b_search_nonrecursive(l,num,ele);

               getch();

               break;

        }

    }

getch();

}
/* Write C programs that use both recursive and non recursive functions

to perform the following searching operation for a Key value in a given list of integers :

i) Linear search */



#include <stdio.h>

#define MAX_LEN 10



void l_search_recursive(int l[],int num,int ele);

void l_search(int l[],int num,int ele);

void read_list(int l[],int num);

void print_list(int l[],int num);



void main()

{

    int l[MAX_LEN], num, ele;

    int ch;



    clrscr();
printf("======================================================");

printf("ntttMENU");

printf("n=====================================================");

printf("n[1] Linary Search using Recursion method");

printf("n[2] Linary Search using Non-Recursion method");

printf("nnEnter your Choice:");

scanf("%d",&ch);



if(ch<=2 & ch>0)

{

printf("Enter the number of elements :");

scanf("%d",&num);

read_list(l,num);

printf("nElements present in the list are:nn");

print_list(l,num);

printf("nnElement you want to search:nn");

scanf("%d",&ele);



switch(ch)

{

    case 1:printf("n**Recursion method**n");

           l_search_recursive(l,num,ele);

           getch();

           break;
case 2:printf("n**Non-Recursion method**n");

                    l_search_nonrecursive(l,num,ele);

                    getch();

                    break;

        }

    }

    getch();

}

/*end main*/



/* Non-Recursive method*/

void l_search_nonrecursive(int l[],int num,int ele)

{

    int j, f=0;

    for(j=0;j<num;j++)

    if( l[j] == ele)

    {

        printf("nThe element %d is present at position %d in listn",ele,j);

        f=1;

        break;

    }

    if(f==0)

            printf("nThe element is %d is not present in the listn",ele);
}



/* Recursive method*/

void l_search_recursive(int l[],int num,int ele)

{

    int f = 0;



    if( l[num] == ele)

    {

        printf("nThe element %d is present at position %d in listn",ele,num);

        f=1;

    }

    else

    {

    if((num==0) && (f==0))

    {

        printf("The element %d is not found.",ele);

    }

    else

    {

        l_search(l,num-1,ele);

    }

    }

    getch();
}



void read_list(int l[],int num)

{

    int j;

    printf("nEnter the elements:n");

    for(j=0;j<num;j++)

       scanf("%d",&l[j]);

}



void print_list(int l[],int num)

{

    int j;

    for(j=0;j<num;j++)

       printf("%dt",l[j]);

}

More Related Content

What's hot

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2
Dr. Loganathan R
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2
Dr. Loganathan R
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
Sayantan Sur
 
Array menu
Array menuArray menu
Array menu
Sayantan Sur
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
Rumman Ansari
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
Divya Kumari
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
Divya Kumari
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Tomohiro Kumagai
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
Dr. Loganathan R
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
mohamed sikander
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
Rumman Ansari
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ Eli Diaz
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ Eli Diaz
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
argusacademy
 
Operators php
Operators phpOperators php
Operators php
Chandni Pm
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
LakshmiSamivel
 

What's hot (20)

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Array menu
Array menuArray menu
Array menu
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Operators php
Operators phpOperators php
Operators php
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 

Viewers also liked (10)

Android beginner getting started with android
Android beginner getting started with androidAndroid beginner getting started with android
Android beginner getting started with android
 
week-1x
week-1xweek-1x
week-1x
 
week-18x
week-18xweek-18x
week-18x
 
week-10x
week-10xweek-10x
week-10x
 
week-11x
week-11xweek-11x
week-11x
 
Part4 graph algorithms
Part4 graph algorithmsPart4 graph algorithms
Part4 graph algorithms
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
ch6
ch6ch6
ch6
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 

Similar to week-19x

a) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdfa) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdf
MAYANKBANSAL1981
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
Deyvessh kumar
 
C programs
C programsC programs
C programs
Koshy Geoji
 
Cpds lab
Cpds labCpds lab
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
Nivegeetha
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Pnno
PnnoPnno
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 

Similar to week-19x (20)

week-20x
week-20xweek-20x
week-20x
 
week-14x
week-14xweek-14x
week-14x
 
a) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdfa) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdf
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Tugas1
Tugas1Tugas1
Tugas1
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
C programs
C programsC programs
C programs
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
week-16x
week-16xweek-16x
week-16x
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Array list
Array listArray list
Array list
 
Pnno
PnnoPnno
Pnno
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 

More from KITE www.kitecolleges.com (20)

ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-5x
week-5xweek-5x
week-5x
 
week-6x
week-6xweek-6x
week-6x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-4x
week-4xweek-4x
week-4x
 
AIRBORNE
AIRBORNEAIRBORNE
AIRBORNE
 
ch2
ch2ch2
ch2
 
week-23x
week-23xweek-23x
week-23x
 
week-2x
week-2xweek-2x
week-2x
 
ch3
ch3ch3
ch3
 
Entity Classes and Attributes
Entity Classes and AttributesEntity Classes and Attributes
Entity Classes and Attributes
 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

week-19x

  • 1. /* Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : ii) Binary search*/ #include <stdio.h> #define MAX_LEN 10 /* Non-Recursive function*/ void b_search_nonrecursive(int l[],int num,int ele) { int l1,i,j, flag = 0; l1 = 0; i = num-1; while(l1 <= i) { j = (l1+i)/2; if( l[j] == ele) { printf("nThe element %d is present at position %d in listn",ele,j); flag =1; break; } else if(l[j] < ele)
  • 2. l1 = j+1; else i = j-1; } if( flag == 0) printf("nThe element %d is not present in the listn",ele); } /* Recursive function*/ int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a) { int m,pos; if (arrayStart<=arrayEnd) { m=(arrayStart+arrayEnd)/2; if (l[m]==a) return m; else if (a<l[m]) return b_search_recursive(l,arrayStart,m-1,a); else return b_search_recursive(l,m+1,arrayEnd,a); } return -1; }
  • 3. void read_list(int l[],int n) { int i; printf("nEnter the elements:n"); for(i=0;i<n;i++) scanf("%d",&l[i]); } void print_list(int l[],int n) { int i; for(i=0;i<n;i++) printf("%dt",l[i]); } /*main function*/ void main() { int l[MAX_LEN], num, ele,f,l1,a; int ch,pos; clrscr();
  • 4. printf("======================================================"); printf("ntttMENU"); printf("n====================================================="); printf("n[1] Binary Search using Recursion method"); printf("n[2] Binary Search using Non-Recursion method"); printf("nnEnter your Choice:"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("nEnter the number of elements : "); scanf("%d",&num); read_list(l,num); printf("nElements present in the list are:nn"); print_list(l,num); printf("nnEnter the element you want to search:nn"); scanf("%d",&ele); switch(ch) { case 1:printf("nRecursive method:n"); pos=b_search_recursive(l,0,num,ele); if(pos==-1)
  • 5. { printf("Element is not found"); } else { printf("Element is found at %d position",pos); } getch(); break; case 2:printf("nNon-Recursive method:n"); b_search_nonrecursive(l,num,ele); getch(); break; } } getch(); }
  • 6. /* Write C programs that use both recursive and non recursive functions to perform the following searching operation for a Key value in a given list of integers : i) Linear search */ #include <stdio.h> #define MAX_LEN 10 void l_search_recursive(int l[],int num,int ele); void l_search(int l[],int num,int ele); void read_list(int l[],int num); void print_list(int l[],int num); void main() { int l[MAX_LEN], num, ele; int ch; clrscr();
  • 7. printf("======================================================"); printf("ntttMENU"); printf("n====================================================="); printf("n[1] Linary Search using Recursion method"); printf("n[2] Linary Search using Non-Recursion method"); printf("nnEnter your Choice:"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("Enter the number of elements :"); scanf("%d",&num); read_list(l,num); printf("nElements present in the list are:nn"); print_list(l,num); printf("nnElement you want to search:nn"); scanf("%d",&ele); switch(ch) { case 1:printf("n**Recursion method**n"); l_search_recursive(l,num,ele); getch(); break;
  • 8. case 2:printf("n**Non-Recursion method**n"); l_search_nonrecursive(l,num,ele); getch(); break; } } getch(); } /*end main*/ /* Non-Recursive method*/ void l_search_nonrecursive(int l[],int num,int ele) { int j, f=0; for(j=0;j<num;j++) if( l[j] == ele) { printf("nThe element %d is present at position %d in listn",ele,j); f=1; break; } if(f==0) printf("nThe element is %d is not present in the listn",ele);
  • 9. } /* Recursive method*/ void l_search_recursive(int l[],int num,int ele) { int f = 0; if( l[num] == ele) { printf("nThe element %d is present at position %d in listn",ele,num); f=1; } else { if((num==0) && (f==0)) { printf("The element %d is not found.",ele); } else { l_search(l,num-1,ele); } } getch();
  • 10. } void read_list(int l[],int num) { int j; printf("nEnter the elements:n"); for(j=0;j<num;j++) scanf("%d",&l[j]); } void print_list(int l[],int num) { int j; for(j=0;j<num;j++) printf("%dt",l[j]); }