int main() { int myVec[] = {2,1,4,3,5,4,2,34}; int found = linearsearch(myVec,sizeof(myVec)/sizeof(int),34); if(found != -1) printf("indice: %d\n",found); else printf("nao encontrado\n"); return 0; }
// return -1 : not found int linearsearch(int *v, int size, int element){ int returned; if(size <= 0) return -1; else if(*(v) == element) return 0; else{ returned = linearsearch(v+1,size-1,element); if(returned == -1) return -1; else return (1+returned); } }
int main() { int array[1000],number,i,leanght; scanf("%d",&leanght); for(i=0;i<leanght;i++) { scanf("%d",&array[i]); } scanf("%d",&number); if(linearsearch(array,leanght,number)) { printf("Not Found"); } else { printf("Found"); } return 0; }
int main() { int n,i,a[1000],key; printf(" Enter number of elements : "); scanf("%d",&n); printf(" Enter Array in sorted order : "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf(" Enter Element to search : "); scanf("%d",&key); printf(" Binary search : \n "); i=binarysearch(a,key,0,n); if(i==-1) printf(" Element not found \n"); else printf(" Element found at position %d \n\n",i+1); printf(" Linear search : \n"); i=linearsearch(a,key,n); if(i==-1) printf(" Element not found \n"); else printf(" Element found at position %d ",i+1); }
void synthesisPositions(int nx, int nt, int nxs, int nts, float dt, float *xsyn, int Nfoc, float *xrcv, float *xsrc, int *xnx, float fxse, float fxsb, float dxs, float dxsrc, float dx, int nshots, int *ixpos, int *npos, int *isxcount, int countmin, int reci, int verbose) { int i, j, l, ixsrc, ixrcv, dosrc, k, *count; float x0, x1; count = (int *)calloc(nxs,sizeof(int)); // number of traces that contribute to the integration over x /*================ SYNTHESIS ================*/ /* assuming all focal operators cover the same lateral area */ *npos=0; if (reci == 0 || reci == 1) { for (k=0; k<nshots; k++) { ixsrc = NINT((xsrc[k] - fxsb)/dxs); if (verbose>=3) { vmess("source position: %.2f in operator %d", xsrc[k], ixsrc); vmess("receiver positions: %.2f <--> %.2f", xrcv[k*nx+0], xrcv[k*nx+nx-1]); vmess("focal point positions: %.2f <--> %.2f", fxsb, fxse); } if ((NINT(xsrc[k]-fxse) > 0) || (NINT(xrcv[k*nx+nx-1]-fxse) > 0) || (NINT(xrcv[k*nx+nx-1]-fxsb) < 0) || (NINT(xsrc[k]-fxsb) < 0) || (NINT(xrcv[k*nx+0]-fxsb) < 0) || (NINT(xrcv[k*nx+0]-fxse) > 0) ) { vwarn("source/receiver positions are outside synthesis aperture"); vmess("xsrc = %.2f xrcv_1 = %.2f xrvc_N = %.2f", xsrc[k], xrcv[k*nx+0], xrcv[k*nx+nx-1]); vmess("source position: %.2f in operator %d", xsrc[k], ixsrc); vmess("receiver positions: %.2f <--> %.2f", xrcv[k*nx+0], xrcv[k*nx+nx-1]); vmess("focal point positions: %.2f <--> %.2f", fxsb, fxse); } //fprintf(stderr,"k=%d xsrc[k]=%f 0.999*fxsb=%f, 1.001*fxse=%f %f %f\n",k, xsrc[k], 0.999*fxsb, 1.001*fxse, fxsb, fxse); //if ( (xsrc[k] >= 0.999*fxsb) && (xsrc[k] <= 1.001*fxse) ) { if ( (ixsrc < nxs) && (ixsrc >= 0) ) { j = linearsearch(ixpos, *npos, ixsrc); if (j < *npos) { /* the position (at j) is already included */ count[j] += xnx[k]; } else { /* add new postion */ ixpos[*npos]=ixsrc; count[*npos] += xnx[k]; *npos += 1; } if (verbose>=3) { vmess("source position %d is inside synthesis model %f *npos=%d count=%d", k, xsrc[k], *npos, count[*npos]); vmess("ixpos[%d] = %d ixsrc=%d", *npos-1, ixpos[*npos-1], ixsrc); } } else { if (verbose>=2) { vwarn("source position %d is outside synthesis model %f ixsrc=%d", k, xsrc[k], ixsrc); } } } /* end of nshots (k) loop */ } /* end of reci branch */ /* if reci=1 or reci=2 source-receive reciprocity is used and new (reciprocal-)sources are added */ if (reci != 0) { for (k=0; k<nxs; k++) { /* check count in total number of shots added by reciprocity */ if (isxcount[k] >= countmin) { j = linearsearch(ixpos, *npos, k); if (j < *npos) { /* the position (at j) is already included */ count[j] += isxcount[k]; } else { /* add new postion */ ixpos[*npos]=k; count[*npos] += isxcount[k]; *npos += 1; } } else { isxcount[k] = 0; } } } /* end of reci branch */ if (verbose>=4) { for (j=0; j < *npos; j++) { vmess("ixpos[%d] = %d count=%d", j, ixpos[j], count[j]); } } free(count); /* sort ixpos into increasing values */ qsort(ixpos, *npos, sizeof(int), compareInt); return; }
void synthesisPositions3D(int nx, int ny, int nxs, int nys, int Nfoc, float *xrcv, float *yrcv, float *xsrc, float *ysrc, int *xnx, float fxse, float fyse, float fxsb, float fysb, float dxs, float dys, int nshots, int nxsrc, int nysrc, int *ixpos, int *npos, int reci, int verbose) { int j, l, ixsrc, iysrc, isrc, k, *count, nxy; float fxb, fxe, fyb, fye; if (fxsb < 0) fxb = 1.001*fxsb; else fxb = 0.999*fxsb; if (fysb < 0) fyb = 1.001*fysb; else fyb = 0.999*fysb; if (fxse > 0) fxe = 1.001*fxse; else fxe = 0.999*fxse; if (fyse > 0) fye = 1.001*fyse; else fye = 0.999*fyse; nxy = nx*ny; count = (int *)calloc(nxs*nys,sizeof(int)); // number of traces that contribute to the integration over x /*================ SYNTHESIS ================*/ for (l = 0; l < 1; l++) { /* assuming all focal operators cover the same lateral area */ // for (l = 0; l < Nfoc; l++) { *npos=0; if (reci == 0 || reci == 1) { for (k=0; k<nshots; k++) { ixsrc = NINT((xsrc[k] - fxsb)/dxs); iysrc = NINT((ysrc[k] - fysb)/dys); isrc = iysrc*nxs + ixsrc; if (verbose>=3) { vmess("source position: x=%.2f y=%.2f in operator x=%d y=%d pos=%d", xsrc[k], ysrc[k], ixsrc, iysrc, isrc); vmess("receiver positions: x:%.2f <--> %.2f y:%.2f <--> %.2f", xrcv[k*nxy+0], xrcv[k*nxy+nxy-1], yrcv[k*nxy+0], yrcv[k*nxy+nxy-1]); vmess("focal point positions: x:%.2f <--> %.2f y:%.2f <--> %.2f", fxsb, fxse, fysb, fyse); } if ((NINT(xsrc[k]-fxse) > 0) || (NINT(xrcv[k*nxy+nxy-1]-fxse) > 0) || (NINT(xrcv[k*nxy+nxy-1]-fxsb) < 0) || (NINT(xsrc[k]-fxsb) < 0) || (NINT(xrcv[k*nxy+0]-fxsb) < 0) || (NINT(xrcv[k*nxy+0]-fxse) > 0) || (NINT(ysrc[k]-fyse) > 0) || (NINT(yrcv[k*nxy+nxy-1]-fyse) > 0) || (NINT(yrcv[k*nxy+nxy-1]-fysb) < 0) || (NINT(ysrc[k]-fysb) < 0) || (NINT(yrcv[k*nxy+0]-fysb) < 0) || (NINT(yrcv[k*nxy+0]-fyse) > 0) ) { vwarn("source/receiver positions are outside synthesis aperture"); vmess("xsrc = %.2f xrcv_1 = %.2f xrvc_N = %.2f", xsrc[k], xrcv[k*nxy+0], xrcv[k*nxy+nxy-1]); vmess("ysrc = %.2f yrcv_1 = %.2f yrvc_N = %.2f", ysrc[k], yrcv[k*nxy+0], yrcv[k*nxy+nxy-1]); vmess("source position x: %.2f in operator %d", xsrc[k], ixsrc); vmess("source position y: %.2f in operator %d", ysrc[k], iysrc); vmess("receiver positions x: %.2f <--> %.2f", xrcv[k*nxy+0], xrcv[k*nxy+nxy-1]); vmess("receiver positions y: %.2f <--> %.2f", yrcv[k*nxy+0], yrcv[k*nxy+nxy-1]); vmess("focal point positions x: %.2f <--> %.2f", fxsb, fxse); vmess("focal point positions y: %.2f <--> %.2f", fysb, fyse); } if ( (xsrc[k] >= fxb) && (xsrc[k] <= fxe) && (ysrc[k] >= fyb) && (ysrc[k] <= fye) ) { j = linearsearch(ixpos, *npos, isrc); if (j < *npos) { /* the position (at j) is already included */ count[j] += xnx[k]; } else { /* add new postion */ ixpos[*npos] = isrc; count[*npos] += xnx[k]; *npos += 1; } // vmess("source position %d is inside synthesis model %f *npos=%d count=%d", k, xsrc[k], *npos, count[*npos]); } } /* end of nshots (k) loop */ } /* end of reci branch */ } /* end of Nfoc loop */ if (verbose>=4) { for (j=0; j < *npos; j++) { vmess("ixpos[%d] = %d count=%d", j, ixpos[j], count[j]); } } free(count); /* sort ixpos into increasing values */ qsort(ixpos, *npos, sizeof(int), compareInt); return; }