Question and code which ı wrote below.I wrote this code by using flow chart.Everything seems true but when ı compile tis code its answer wrong.Please help me.

Question: With 8 elements

a[]={1,2,3,4,5,6,7,8}

b[]={1,3,5,7,9,11,13,15} sets are given.Write a C++ code to find the sum of the numbers of intersection sets.

Answer:

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

int a[]={1,2,3,4,5,6,7,8};

int b[]={1,3,5,7,9,11,13,15};

int i;

int sum=0;

for(i=0;i<=7;i++)

{for(i=0;i<=7;i++){if(a[i]==b[i]){sum=sum+a[i];}}}

cout<<"Ortak Eleman Toplami : "<<sum<<endl;

return 0;

}

Question: With 8 elements

a[]={1,2,3,4,5,6,7,8}

b[]={1,3,5,7,9,11,13,15} sets are given.Write a C++ code to find the sum of the numbers of intersection sets.

Answer:

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

int a[]={1,2,3,4,5,6,7,8};

int b[]={1,3,5,7,9,11,13,15};

int i;

int sum=0;

for(i=0;i<=7;i++)

{for(i=0;i<=7;i++){if(a[i]==b[i]){sum=sum+a[i];}}}

cout<<"Ortak Eleman Toplami : "<<sum<<endl;

return 0;

}

This code only adds if a[i] = b[i].

When is that true?

i = 0:

a[i] = 1

b[i] = 1

a[i] == b[i], ADD VALUE

i = 1:

a[i] = 2

b[i] = 3

a[i] != b[i], DO NOT ADD VALUE

i = 2:

a[i] = 3

b[i] = 5

a[i] != b[i], DO NOT ADD VALUE

i = 3:

a[i] = 4

b[i] = 7

a[i] != b[i], DO NOT ADD VALUE

i = 4:

a[i] = 5

b[i] = 9

a[i] != b[i], DO NOT ADD VALUE

i = 5:

a[i] = 6

b[i] = 11

a[i] != b[i], DO NOT ADD VALUE

i = 6:

a[i] = 7

b[i] = 13

a[i] != b[i], DO NOT ADD VALUE

i = 7:

a[i] = 4

b[i] = 15

a[i] != b[i], DO NOT ADD VALUE

ONLY when i=1.

Your code is just doing entirely the wrong thing.

When is that true?

i = 0:

a[i] = 1

b[i] = 1

a[i] == b[i], ADD VALUE

i = 1:

a[i] = 2

b[i] = 3

a[i] != b[i], DO NOT ADD VALUE

i = 2:

a[i] = 3

b[i] = 5

a[i] != b[i], DO NOT ADD VALUE

i = 3:

a[i] = 4

b[i] = 7

a[i] != b[i], DO NOT ADD VALUE

i = 4:

a[i] = 5

b[i] = 9

a[i] != b[i], DO NOT ADD VALUE

i = 5:

a[i] = 6

b[i] = 11

a[i] != b[i], DO NOT ADD VALUE

i = 6:

a[i] = 7

b[i] = 13

a[i] != b[i], DO NOT ADD VALUE

i = 7:

a[i] = 4

b[i] = 15

a[i] != b[i], DO NOT ADD VALUE

ONLY when i=1.

Your code is just doing entirely the wrong thing.

Last edited on

Registered users can post here. Sign in or register to post.