Thursday 14 May 2015

Program to find missing elements in an array in C


#include<stdio.h>
int main( )
{

        int a[ ]={2,4,7};    //array to be tested
        int n=sizeof(a)/sizeof(a[0]);    //length of array
        int i,j,match;

        for(i=1;i<=9;i++)    //here we are testing the missing elements from 1 to 9
        {
                 match=0;
                 for(j=0; j<n | | (match==0);j++)    //it will loop until the array ends or we found a match
                           if(a[j] = = i)
                                  match=1;
   
                  if(match==0)   //no match found
                         printf("%d  ",i);
         }
     
          return 0;
}

No comments:

Post a Comment