union of two sets

Page 1 of 1

10 Replies - 195 Views - Last Post: 07 July 2012 - 08:55 AM Rate Topic: -----

#1 suyed  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is online

  • Code herder
  • member icon

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

Re: union of two sets

Posted 06 July 2012 - 11:48 PM

So what is your question?
Was This Post Helpful? 0
  • +
  • -

#3 suyed  Icon User is offline

  • New D.I.C Head

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

Re: union of two sets

Posted 06 July 2012 - 11:56 PM

View Postsuyed, 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..
please help me out..

#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;
}


View PostSkydiver, on 06 July 2012 - 11:48 PM, said:

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


View PostSkydiver, 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...

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1898
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 suyed  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1898
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 suyed  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 suyed  Icon User is offline

  • New D.I.C Head

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

Re: union of two sets

Posted 07 July 2012 - 08:40 AM

View Postaresh, 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..:/

Was This Post Helpful? -1
  • +
  • -

#10 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

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

Re: union of two sets

Posted 07 July 2012 - 08:45 AM

View Postsuyed, 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.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon

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

Re: union of two sets

Posted 07 July 2012 - 08:55 AM

View Postsuyed, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1