I was trying to write a code for union and intersection of two sets but i got an error which states clrscr and exit are not declared in scope. I have included the header files but still i get this error. Can some one help me out in this?

Recommended Answers

All 5 Replies

Post your code.

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h>
void Union(int set1[10],int set2[10],int m,int n); 
void Intersection(int set1[10],int set2[10],int m,int n); 
void main() 
{ 
 int a[10],b[10],m,n,i,j; 
 int ch; 
//clrscr();
 printf("\nEnter the number of elements in first set:\n"); 
 scanf("%d",&m); 
 printf("\nEnter the elements:\n"); 
 for(i=0;i<m;i++) 
 { 
  scanf("%d",&a[i]); 
 } 
 printf("\nElement of First set:\n\n"); 
 for(i=0;i<m;i++) 
 { 
  printf("%d\t",a[i]); 
 } 
 printf("\nEnter the number of elements in second set:\n"); 
 scanf("%d",&n); 
 printf("\nEnter the elements:\n"); 
 for(i=0;i<n;i++) 
 { 
  scanf("%d",&b[i]); 
 } 
 printf("\nElement of second set\n"); 
 for(i=0;i<n;i++) 
 { 
  printf("%d\t",b[i]); 
 } 
 if(i<3)
 { 
  printf("\n\nMenu\n\n1.Union\n2.Intersection"); 
  printf("\n3.exit"); 
  printf("\nEnter your choice:\n"); 
  scanf("%d",&ch); 
  switch(ch) {
  case 1: 
   Union(a,b,m,n); 
   break; 
  case 2: 
   Intersection(a,b,m,n); 
   break; 
  case 3: 
   exit(0); 

  } 
  getch();
  return ch; 
 }
} 

void Union(int a[10],int b[10],int m,int n) 
{ 
 int c[20],i,j,k=0,flag=0; 
 for(i=0;i<m;i++) 
 { 
  c[k]=a[i]; 
  k++; 
 } 
 for(i=0;i<n;i++) 
 { 
  flag=0; 
  for(j=0;j<m;j++) 
  { 
   if(b[i]==c[j]) 
   { 
    flag=1; 
    break; 
   } 
  } 
  if(flag==0) 
  { 
   c[k]=b[i]; 
   k++; 
  } 
 } 
 printf("\nElement of resultant set\n\n"); 
 for(i=0;i<k;i++) 
 { 
  printf("\t%d",c[i]); 
 } 
} 
void Intersection(int a[10],int b[10],int m,int n) 
{ 
 int c[20],i,j,k=0,flag=0; 
 for(i=0;i<m;i++) 
 { 
  flag=0; 
  for(j=0;j<n;j++) 
  { 
   if(a[i]==b[j]) 
   { 
    flag=1; 
    break; 
   } 
  } 
  if(flag==1) 
  { 
   c[k]=a[i]; 
   k++; 
  } 
 } 
 if(k==0)
 {
  printf("\n\nResultant set is null set!\n");
 }else{
  printf("\nElement of resultant set\n"); 
  for(i=0;i<k;i++) 
  { 
   printf("\t%d",c[i]); 
  } 
 }
}

Change void main() to int main()
On line 35. if(i<3), if i is >=3 then you'll never enter that section of code. I assume that what you want to do is to keep displaying the menu until 3 is chosen. An easy way to do this would be:

printf("\n\nMenu\n\n1.Union\n2.Intersection"); 
printf("\n3.exit"); 
printf("\nEnter your choice:\n");

while (scanf("%d", &ch) && (ch != 3))
{
    switch(ch) {
    case 1: 
        Union(a,b,m,n); 
        break; 

    case 2: 
        Intersection(a,b,m,n); 
        break; 

    default:
        printf("\nInvalid choice!");
        break;
    }
    printf("\n\nMenu\n\n1.Union\n2.Intersection"); 
    printf("\n3.exit"); 
    printf("\nEnter your choice:\n"); 
}

return 0;

Thank you so much. I would like to know how to write code for Complement of two sets?

I am trying to write code like fietst the union and intersection should to done on the sets to the resulting answer i need to get a complement. I dont know how to implement can some one help?
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.