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
CMulti-Dimensional Arrays : Searching a 2-D Array
<09.01>
/* Problem: Searching for a value inside 2-D Array */
#include <stdio.h> #define MAXSIZE 100 int search2d (int array[][MAXSIZE], int value, int size, int *foundcol) { /* foundrow will return the row where the value was found or -1 if value not found */ /* *foundcol will be returning the column where the value was found. It remains undefined if not found */ int i, j, foundrow; /* initialize found at -1, if value not found, stays -1 */ foundrow = -1; /* search until found or until end of array */ for (i=0; i<size; ++i) for (j=0; j<size; ++j) { if (array[i][j] == value) { foundrow = i; /* I have found it! */ *foundcol = j; } } return (foundrow); } int main (void) { int x[100][100], value, row, col, size, i, j; value = 99; /* the value I am searching */ /* reading size from file */ scanf ("%d", &size); /* fills array from file using redirection */ for (i=0; i<size; ++i) for (j=0; j<size; ++j) scanf ("%d", &x[i][j]); /* calling the search function */ row = search2d (x, value, size, &col); if (row >= 0) printf ("%d found at row %d / column %d.\n", value, row, col); else printf ("%d not found in the array.\n", value); return (0); }
Hergestellt in Deutschland / Made in Germany
99 was found at row 3 / column 1.
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland