算法导论编程第2章 线性查找
2016-03-14 15:31
162 查看
#include <iostream> #define NIL -1 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int linearSearch(int val,int a[],int length); int main(int argc, char** argv) { int a[]={1,2,3,4,5,6,7,8,9}; int pos = linearSearch(7,a,9); printf("%d\n",pos); return 0; } /* 算法导论编程第2章 线性查找 2016年3月14日 15:29:57 */ int linearSearch(int val,int a[],int length){ int foundFlag=0; int returnPos; for(int i=0;i<length-1;i++){ if(val==a[i]){ foundFlag=1; returnPos=i; break; } } if(foundFlag==0){ return NIL; }else{ return returnPos; } }