Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

2 array compare in Dev c++

Status
Not open for further replies.

fawadbutt

Member level 3
Joined
Oct 29, 2010
Messages
61
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Location
Lahore, Pakistan, Pakistan
Activity points
1,754
hi ,
i am using Dev C++,
i write a program campare two array
int a[10],b[10];
get the array value from user
for Loop
cin>>a;

compare and display same intager form array a and b
can any help me for last part of program....
 

ok to compare to arrays of integer you can just make a loop as the size of the array and check them with each values......

example::
lets take same variable a and b

for(int i=0;i<(sizeof(a)/sizeof(a[0]);i++)
{
for(int j=0;j<sizeof(b)/sizeof(b[0]);j++)
{
if(a==b[j]) cout<<a; //or cout<<b;
}
}

note: i have used sizeof(a)/sizeof(a[0]) to just find the size of the array
 

thanx for replay,,,
i also use this solution,,,
but the problem is than i enter
for example
1 enter 1: 2
1 enter 2: 2
1 enter 3: 4

2 enter 1: 2
2 enter 2: 2
3 enter 3: 6

result is
2
2
.................................
i hope u understand.. answer is
2 but answer has

2
2
this is incorrent
.........
 

OK. This means the number should be displayed only once even if the array contains many more.In this case, you need to declare one dynamic array and other counter of integer type.
I think it would be better to give the solution than to describe it because that would be easy to understand.
So, I am making complete function for you. You just need to call the function from main.

void common_out(int *a,int *b,int size_a,int size_b)
{
int *c=(int*)malloc(sizeof(int)*1); //you can give the size of minimum of two array but dynamic allocation may save some space
int counter=0;
int i,j,k;

for(i=0;i<size_a;i++)
{
for(j=0;j<size_b;j++)
{
if(a==b[j])
{
for(k=0;k<counter;k++)
{
if(a==c[k]) break;
}
if(k==counter) //this means 'k' has completed all loops and did not match any number
{
cout<<a<<"\n";
counter++;
c=(int*) realloc(a,sizeof(int)*(counter+1));
}
}
}
}
free(c);
}

Now from main just call:

int main()
{
int a[2]={2,3},b[2]={2,2};

common_out(a,b,2,2);

getch();
return 0;
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top