Thread: C programming Homework binary,linear search.

  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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to put the random number generator code in a function, as specified in the requirements. However, the srand call should remain in the main function, called before the random number generator function.

    I'm a bit puzzled as to why you have two arrays though: if you're going to search both arrays, how do you know whether the key that is found is for one array or the other? Not that it cannot be done, but it is strange. Perhaps you're supposed to write the functions to search an array, then call it for each of the arrays in turn?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    Maybe, i am also confused too.That's why i am here and asking it.And also i forget to write the range of the arrays both of them between 1-100.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should clarify that with your teacher as it is the wording of the assignment itself that is confusing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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