IHYPRESS.NET > C PROGRAMMING
HOME | ASP | C | HTML/CSS | JAVASCRIPT | MATLAB | PERL | PHP | PYTHON | RUBY
C > 07. Arrays I (1D) [PROGRAM #08]
CODERESULT
<
/* THE BASIC SEARCH ALGORITHM FOR A 1-D ARRAY */
#include <stdio.h> int search (int arraytosearch[], int valuetosearch, int size) { int i, found; /* initialize found at -1, if value not found, stays at -1 */ found = -1; /* search until found or until end of array */ i = 0; while (found<0 && i<size) { if (arraytosearch[i] == valuetosearch) found = i; /* I have found it! */ else i = i + 1; } return (found); } int main (void) { int x[] = {12,67,56,60,88,34,123}; int value = 60; int pos, i; pos = search (x, value, 7); if (pos >= 0) printf ("%d was found at position %d.\n", value, pos); else printf ("%d was not found in the array.\n", value); return (0); }
Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
60 was found at position 3.
>
COPYRIGHT © 2019 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.
SHARE