U E D R , A S I H C RSS

김태진/Search

Linear Continuous Search


, , 2 .
#include <stdio.h>

int linear_search(int a[], int size, int val);

int main()
{
    int arr[10]={5,8,13,2,4,15,13,17,18,20};
    int index, val;
    
    scanf("%d", &val);
    for(index=1;index>0;){
        index=linear_search(arr,10,val);
        if (index<0) printf("Not found.\n");
        else printf("%d is found in %d-th position in the array.\n", val,index);
    }
    return 0;
}

int linear_search(int a[], int size, int val)
{
    static int check=0;
    int i=0;

    for(;check<size;check++){
        if(a[check]==val){
            i=check; check+=1; 
            return i;
        }
    } 
    return -1;
}
, 함 check static variable . thanks to. 힌트 .

  • . . static variable ㅋㅋ -
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:51
Processing time 0.0208 sec