Tuesday, December 28, 2010

set operatoin A - B | subtraction of two arrays

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for set operatoin A - B | subtraction of two arrays
*/

#include<stdio.h>
#define TRUE 0
#define FALSE 1
void main()
{


int
n1 = 0 , i =0 , n2=0 , j = 0 , flag =FALSE , no1[100] , no2[100];

int
result[200];
int
r1 = 0 ;

printf("How many numbers you want to scan in first array ? ");
scanf("%d",&n1);
if
(n1 < 0 || n1 > 100 )
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n1 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no1[i]);
}
}


printf("How many numbers you want to scan in second array ? ");
scanf("%d",&n2);
if
(n2 < 0 || n2 > 100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n2 ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no2[i]);
}
}



for
(i = 0 ; i < n1 ; i++)
{


flag = FALSE ;
for
(j = 0 ; j< n2 ; j++ )
{


if
(no1[i] == no2[j] )
{

flag = TRUE;
}
}


if
(flag == FALSE)
{

result[r1++] = no1[i];
}
}




// now display the array
printf("\nTHe array (A-B) is : ");
for
(i = 0 ; i < r1 ; i++ )
{


printf(" %d," , result[i] );
}

printf("\b \n");
}

No comments:

Post a Comment