Searching Two-Dimensional Arrays - C Array

C examples for Array:Multidimensional Arrays

Introduction

The following program demonstrates how to search a two-dimensional array.

Demo Code

#include <stdio.h> 
int main() /* ww w  .ja  va  2s.  c om*/
{ 
   int iTwoD[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 
   int iFoundAt[2] = {0, 0}; 
   int x, y; 
   int iValue = 0; 
   int iFound = 0; 
   printf("\nEnter your search value: "); 
   scanf("%d", &iValue);  
   //search the 2-D array 
   for ( x = 0; x <= 2; x++ ) { 
      for ( y = 0; y <= 2; y++ ) { 
         if ( iTwoD[x][y] == iValue ) { 
            iFound = 1; 
            iFoundAt[0] = x; 
            iFoundAt[1] = y; 
            break; 
         }  //end if 
      }  //end inner loop 
   } //end outer loop 

   if ( iFound == 1 ) 
      printf("\nFound value in iTwoD[%d][%d]\n", iFoundAt[0], iFoundAt[1]); 
   else 
      printf("\nValue not found\n"); 
}

Result


Related Tutorials