IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
COne Dimensional Arrays : Searching a 1-D Array
<07.08>
/* 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); }
Hergestellt in Deutschland / Made in Germany
60 was found at position 3.
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland