Pages

Tuesday, 8 August 2017

2d Array Compare

Q : Write a function that returns 1 if the two matrices passed to it as argument are equal and 0
otherwise.

Solution : 


#include<stdio.h>
#include<conio.h>
#define ROW 2
#define COL 2
int compare(int a1[ROW][COL],int a2[ROW][COL]);
void main()
{
     int ar1[ROW][COL],ar2[ROW][COL],r,c;
     clrscr();

     printf("\nFirst Array Input\n");
     for(r=0;r<ROW;r++)
     {
           for(c=0;c<COL;c++)
           {
                printf("Enter Number>> ");
                scanf("%d",&ar1[r][c]);
           }
     }
     printf("\nSecond Array Input\n");
     for(r=0;r<ROW;r++)
     {
           for(c=0;c<COL;c++)
           {
                printf("Enter Number>> ");
                scanf("%d",&ar2[r][c]);
           }
     }
     if(compare(ar1,ar2)==1)
           printf("\nIdentical");
     else
           printf("\nNot Identical");
     getch();
}

int compare(int a1[ROW][COL],int a2[ROW][COL])
{
     int r,c,flag=1;
     for(r=0;r<ROW;r++)
     {
           for(c=0;c<COL;c++)
           {
                if(a1[r][c]!=a2[r][c])
                {
                      flag=0;
                      break;
                }
           }
           if(flag==0)
                break;
     }

     if(r==ROW)
           return 1;
     else
           return 0;

}

No comments:

Post a Comment