# union of two sets

Page 1 of 1

## 10 Replies - 195 Views - Last Post: 07 July 2012 - 08:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285012&amp;s=730df6407dc1301041a324a554ae9afd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 suyed

Reputation: -1
• Posts: 5
• Joined: 06-July 12

# union of two sets

Posted 06 July 2012 - 11:47 PM

```#include<stdio.h>
#include<conio.h>
int main()
{
int A[5],B[5],C[10];
int i,k,j;
int flag;
clrscr();

printf("enter 5 elements of A");
for(i=0;i<5;i++)
{
scanf("%d",&A[i]);
}
printf("\nenter elements of B");
for(j=0;j<5;j++)
{
scanf("%d",&B[j]);
}

k=0;
for(i=0	;i<5;i++)
{
C[k]=A[i];
k++;
}
for(j=0;j<5;j++)
{
flag=0;
for(i=0;i<5;i++)
{
if(B[j]==A[i])
{
flag=1;
break;
}
}
if(flag==0)
{
C[k]=B[j];
k++;
}
}
for(i=0;i<k;i++)
printf("a union b is %d\n",C[k]);
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: union of two sets

### #2 Skydiver

• Code herder

Reputation: 1898
• Posts: 5,691
• Joined: 05-May 12

## Re: union of two sets

Posted 06 July 2012 - 11:48 PM

### #3 suyed

Reputation: -1
• Posts: 5
• Joined: 06-July 12

## Re: union of two sets

Posted 06 July 2012 - 11:56 PM

suyed, on 06 July 2012 - 11:47 PM, said:

sir,i m a beginner and has jus started learning c
i cant get a correct output for dis code.. i m getin some garbage values..

```#include<stdio.h>
#include<conio.h>
int main()
{
int A[5],B[5],C[10];
int i,k,j;
int flag;
clrscr();

printf("enter 5 elements of A");
for(i=0;i<5;i++)
{
scanf("%d",&A[i]);
}
printf("\nenter elements of B");
for(j=0;j<5;j++)
{
scanf("%d",&B[j]);
}

k=0;
for(i=0	;i<5;i++)
{
C[k]=A[i];
k++;
}
for(j=0;j<5;j++)
{
flag=0;
for(i=0;i<5;i++)
{
if(B[j]==A[i])
{
flag=1;
break;
}
}
if(flag==0)
{
C[k]=B[j];
k++;
}
}
for(i=0;i<k;i++)
printf("a union b is %d\n",C[k]);
return 0;
}
```

Skydiver, on 06 July 2012 - 11:48 PM, said:

So what is your question? i cant get the correct
output

Skydiver, on 06 July 2012 - 11:48 PM, said:

i cant get correct output..

sorry i m using dis web for the first so getting difficult to use it bt i will learn it slowly...

### #4 Skydiver

• Code herder

Reputation: 1898
• Posts: 5,691
• Joined: 05-May 12

## Re: union of two sets

Posted 07 July 2012 - 12:00 AM

Take a closer look at your line 45. The last entry you put it C was at C[k-1], so C[k] is uninitialized. But then, why are you looping on i, but displaying C[k]?

This post has been edited by Skydiver: 07 July 2012 - 12:01 AM

### #5 suyed

Reputation: -1
• Posts: 5
• Joined: 06-July 12

## Re: union of two sets

Posted 07 July 2012 - 12:17 AM

sir i replaced k by i in my line 45 and now i got my desired output..
i just did on a trial and error basis i actually didnot understand what actualy has been happening over that last for loop ..
can u explain me

This post has been edited by jimblumberg: 07 July 2012 - 05:28 AM
Reason for edit:: Removed unecessary quote tags

### #6 Skydiver

• Code herder

Reputation: 1898
• Posts: 5,691
• Joined: 05-May 12

## Re: union of two sets

Posted 07 July 2012 - 12:23 AM

Let me bounce the question back to you: Explain what your code does in lines 11-14 and 16-19. Why are you doing those loops? Where is the input going into in each iteration?

Now, go back and look at your lines 44-45. How is this similar or different to lines 11-14, 16-19?

### #7 suyed

Reputation: -1
• Posts: 5
• Joined: 06-July 12

## Re: union of two sets

Posted 07 July 2012 - 12:43 AM

compiler stores a integer values in a variable i which is again required to be of integer type.
i then increaments its value by 1 and checks the condition whether it is less than 5, if it holds true, it agains goes inside the loop and performs the statements till 1<5 ie 1=4.
when i=5 it does nt go inside the loop instead it performs the next statement after the for loop.

This post has been edited by jimblumberg: 07 July 2012 - 05:29 AM
Reason for edit:: Removed unecessary quote tags

### #8 aresh

• It's a 16-Bit World!

Reputation: 269
• Posts: 3,821
• Joined: 08-January 12

## Re: union of two sets

Posted 07 July 2012 - 12:47 AM

First of all, please stop quoting and then changing the contents. It is irritating. And if you didn't understand the logic, how did you make it ?? Strange.

### #9 suyed

Reputation: -1
• Posts: 5
• Joined: 06-July 12

## Re: union of two sets

Posted 07 July 2012 - 08:40 AM

aresh, on 07 July 2012 - 01:17 PM, said:

i kno d logic and i m new to c so i m not very clear wid all d fundas of c and its programing..
u must be master in c but i m not and so i hav joined dis ..
dis i my first day and a very first topic so pls,,,
if u dont wanna clr my doubts its fine bt dont try to act oversmart wid me..

### #10 aresh

• It's a 16-Bit World!

Reputation: 269
• Posts: 3,821
• Joined: 08-January 12

## Re: union of two sets

Posted 07 July 2012 - 08:45 AM

suyed, on 07 July 2012 - 12:47 PM, said:

i just did on a trial and error basis i actually didnot understand what actualy has been happening over that last for loop

As far as I am concerned, this means that you did not understand the logic.

### #11 jimblumberg

Reputation: 3044
• Posts: 9,279
• Joined: 25-December 09

## Re: union of two sets

Posted 07 July 2012 - 08:55 AM

suyed, on 07 July 2012 - 10:40 AM, said:

i kno d logic and i m new to c so i m not very clear wid all d fundas of c and its programing..
u must be master in c but i m not and so i hav joined dis ..
dis i my first day and a very first topic so pls,,,
if u dont wanna clr my doubts its fine bt dont try to act oversmart wid me..

Since the OP doesn't seem to want to behave I'm going to close this topic.

Jim