Thread: C programming Homework binary,linear search.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    7

    C programming Homework binary,linear search.

    Hello,I am new to the forum.i have an assignment and I could do only the half and need help to finish it.Here is the question:

    Write a C program which does the following;
    You are given two arrays
    int array_A[12];
    int array_B[8];
    Use random number generator to fill the arrays
    Display the content of the arrays
    Ask the user for a search key and also ask for the searching method
    If user choice is L or l, use Linear Search
    If user choice is B or b, use Binary Search
    Display the index of the search key entered by the user or display the error message if
    the search key is not found.
    You should have 5 separate functions for
    1. Random Number Generator
    2. Displaying the arrays
    3. Linear Search
    4. Bubble Sort
    5. Binary Search


    And here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int linears(int array_A[],int array_B[],int key);
    int binarys(int array_A[],int array_B[],int key,int low,int high);
    int bubles(int int array_A[],int array_B[]);
    
    
    
    
    int main() {
        int array_A[12]={};
        int array_B[8]={};
        int i;
        int key;
        int count1;
        int count2;
        char searchtype;
        srand(time(NULL));
        printf("Array A\n");
        for(i=0;i<12;i++)
        {
            array_A[i]=1+rand()%100;
            printf("%5d",array_A[i]);
        }
        printf("\nArray B\n");
        for(i=0;i<8;i++)
        {
            array_B[i]=1+rand()%100;
            printf("%5d",array_B[i]);
        }
        printf("\nEnter A number to search:\n");
        scanf("%d",&key);
        printf("\nEnter search type:\n");
        scanf(" %c",&searchtype);
        
        switch(searchtype) {
            case 'L':
            case 'l':
            count1=linears(array_A,array_B,key);
            if(count1!=-1)
            printf("Searched key%d located at%d",key,count1+1);
            else{
            printf("Searched key%d is not presented in the array.\n",key);
            }
            case 'B':
            case 'b':
            count2=binarys(array_A,array_B,key,low,high1,high2);
            if(count2!=-1)
            printf("Searched key%d located at%d",key,array_A,count2+1);
            else{
            printf("Searched key%d is not presented in the array.\n",key);
            }
            break;
            
        }
        
        
        return 0;
    }
    int linears(int array_A[],int array_B[],int key) {
        int i;
        int j;
        for(i=0;i<12;i++)
        {
            if(key==array_A[i])
            return i;
        }
        
        for(j=0;j<8;j++)
        {
            if(key==array_B[j])
            return j;
        }
        return -1;
    }
    Last edited by mkoruk; 12-18-2019 at 12:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear and binary search in c functions
    By Hana Nasser in forum C Programming
    Replies: 16
    Last Post: 03-15-2015, 09:03 PM
  2. linear and binary search!!
    By ari01 in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2014, 12:12 AM
  3. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  4. no. of comparisons in linear/binary search
    By alice in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-05-2004, 10:55 PM
  5. binary search or linear search
    By vajira in forum C Programming
    Replies: 0
    Last Post: 06-05-2003, 12:42 PM

Tags for this Thread