# Thread: Help for school assignment

1. ## Help for school assignment

Code:
```I've been trying to resolve a problem that was given at school, but as a rookie in C, I've been having problems. Here's what's asked:
Create a function named intersection (void) that receives two vectors (int), the number of elements of the first vector (int), the number of elements of the second vector (int), and returns a third vector constituted by the elements that appear on both the first and second vector. The function should also return the number of elements that are present in the third vector.

Eg:
If the two vectors are: u= <1,2,3,4>, v<2,5,6,7>
It should return:
w=<2>, size= 1

Note: I’m sorry for the miswritten words…I’m not English, and am translating the problem from my birth language :/  And I'm using Visual C++2008

Here's what I've done so far :
#include <stdio.h>

void intersection (int *a, int n_a, int *b, int n_b, int *w, int *n_w)
{
int i;
int x;
x = 0;
for (i=0; i<n_a; i++)
{
if (a[i] == b[i])
w[i]=a[i];
else
0;
}
for (i=0; i<n_a; i++)
{
if (w[i] == x)
x++;
else
0;
}

}

So, when I compile the function, it has no errors or warnings, but when I try to submit it, it says "Compile Time Error". Can someone help me pointing out what's wrong?? Thank you very much. I used int *w, int *n_w to express the output arguments.```

2. You have to have a main function. Something like this:

Code:
```int main(void)
{
int a;
int b;
int w;
int n_a;
int n_b;
int n_w;

// Don't forget to actually populate your variables!

intersection(&a, n_a, &b, n_b, &w, &n_w);

printf("w=<&#37;d>, size=%d\n", w, n_w);
fflush(stdout);

return 0;
}```
Also, you'll need a prototype for your intersection function at the top of your source:
Code:
`void intersection (int *a, int n_a, int *b, int n_b, int *w, int *n_w);`

3. We haven't been using main in these exercises...It's only required that we send the function that's asked. So I figured the function would be wrong. I'll try what you said anyway, thank you so much!

4. That's all fine and good that you don't need to submit your main() to your teacher, but you won't be able to test your function in any compiler without giving it a main().

5. Right. I'll try it out then, maybe I'll figure out what's wrong.

6. for each element in vector 1, if it is in vector 2 add it to vector 3 and increment the count of items in vector 3.

7. Code:
`else 0;`
This is meaningless.

8. Originally Posted by MacGyver
Code:
`else 0;`
This is meaningless.
Yes, and most compilers will actually tell him that it is.

9. Code:
```How about this? Whats wrong here?

void intersection (int *a, int n_a, int *b, int n_b, int *w, int *n_w)
{
int i;
int j;
*n_w = 0;
for (i=0; i<n_a; i++)
{
for (j=0; j<n_b; j++)
if (a[i] == b[j])
w[i]=a[i];
n_w++;
}
}```

10. You're leaving "gaps" in your result vector w -- if a[i] does not appear anywhere in b, then w[i] doesn't get written to. So, you'll need to keep a separate count, maybe in the variable n_w, of where you should write to.