I've got two columns of data.
int a[6]={1,3,5,8,10,12};
int b[3]={3,5,9};
int c[6]={0};
if a is equal to ANY of the data in b, then c= a*2
if a isn't equal to any of the data in b, then c=a.
Here is the answer I want
c[6]={1,6,10,8,10,12}
If I use two for loop, I get 18 values at last.....I don't know how to do it.
int a[6]={1,3,5,8,10,12};
int b[3]={3,5,9};
int c[6]={0};
for(int i=0;i<6;++i){
for(int j=0;j<3;++j){
if(a[i]==b[j]){
c[i]=a[i]*2;
}
else{
c[i]=a[i];
}
}
}
what makes you think that you get 18 values?
origianlly, I thought because of the two for loop,
for(int i=0;i<6;++i){
for(int j=0;j<3;++j){
6*3=18, so finally there should be 18 values, though I defined c[6]
But I tried, I am wrong. Anyway, I have no idea how to write this.
Last edited on
No, since you use i
and j
correctly for indexing. The index never exceed 6 or 3 respectively.
what you need is break;
after c[i]=a[i]*2;
otherwise you may store the wrong values