Read an integer array and then do linear searches.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <stdio.h> #define NMAX 10 int getIntArray(int a[], int nmax, int sentinel); void printIntArray(int a[], int n); int linear(int a[], int n, int who); int main(void) { int x[NMAX]; int hmny; int who; int where; hmny = getIntArray(x, NMAX, 0); printf("The array was: \n"); printIntArray(x,hmny); printf("Now we do linear searches on this data\n"); do{ printf("Enter integer to search for [0 to terminate] : "); scanf("%d", &who); if(who==0)break; where = linear(x,hmny,who); if (where<0){ printf("Sorry, %d is not in the array\n",who); }else printf("%d is at position %d\n",who,where); }while(1); } void printIntArray(int a[], int n) /* n is the number of elements in the array a. * These values are printed out, five per line. */ { int i; for (i=0; i<n; ){ printf("\t%d ", a[i++]); if (i%5==0) printf("\n"); } printf("\n"); } int getIntArray(int a[], int nmax, int sentinel) /* It reads up to nmax integers and stores then in a; sentinel * terminates input. */ { int n = 0; int temp; do { printf("Enter integer [%d to terminate] : ", sentinel); scanf("%d", &temp); if (temp==sentinel) break; if (n==nmax) printf("array is full\n"); else a[n++] = temp; }while (1); return n; } int linear(int a[], int n, int who) /* Given the array a with n elements, searches for who. * It returns its position if found, otherwise it returns * -1. */ { int lcv; for (lcv=0;lcv<n;lcv++) if(who == a[lcv])return lcv; return (-1); } |