# Thread: Outputting numbers in arrays

1. ## Outputting numbers in arrays

Hi,
I need to write a program that has 2 arrays both with 6 numbers each and compares the two arrays to see if they have any matching numbers.
I have figured out how to output the amount of matching numbers but not the numbers themselves. Here is my function trying to do that...

Code:
```int numbers (int draw[], int entry[])
{
for(int i=0; i<MAXSIZE; i++)
{
for(int j=0; j<MAXSIZE; j++)
{
if(draw[i]==entry[j])
break;
}

}
}```
draw and entry are the names of the 2 arrays.
Any ideas of what is wrong?
Thanks

2. Once you have found a match, print the number out, or store it in another array.

If you will have larger arrays, you will find that sorting the arrays and then checking for matches in a single pass would be faster.

3. Thanks but I'm not really sure how to use print.

4. Originally Posted by rachael033
Thanks but I'm not really sure how to use print.
....

Use std::cout....

5. Do i use the print inside or outside my function?

And I have done my own homework this is just a bit of it I couldn't figure out which is the whole point of discussion boards.....

6. Do i use the print inside or outside my function?
If you have another array to store the matched numbers, then you can loop over that array and print from outside your function. If not, it is simpler to just print in your function, as soon as you determine a match.

7. So would it be something like this?

Code:
```int numbers (int draw[], int entry[])
{
for(int i=0; i<MAXSIZE; i++)
{
for(int j=0; j<MAXSIZE; j++)
{
if(draw[i]==entry[j])

break;

}
printf; draw[i];
}

}```

Sorry if that is totally wrong we just havn't learnt about how to use print!

8. Originally Posted by rachael033
So would it be something like this?

Code:
```int numbers (int draw[], int entry[])
{
for(int i=0; i<MAXSIZE; i++)
{
for(int j=0; j<MAXSIZE; j++)
{
if(draw[i]==entry[j])

break;

}
printf; draw[i];
}

}```

Sorry if that is totally wrong we just havn't learnt about how to use print!
Forget printf(). Use std::cout, as I said. If you see that draw[i] and entry[i] are equal, then you can print them right then and there like this:

Code:
`std::cout << "Matching element = " << draw[i] << std::endl;`

9. Ok thanks it seems to be kind of working but I am getting the right numbers printed out but then a big random number at the end.
I input 1 2 3 4 5 6 in both arrays so that should be the output but instead of...
1 2 3 4 5 6
i get
1 2 3 4 5 6 2293468

Here is the function...

Code:
```int numbers (int draw[], int entry[])
{

for(int i=0; i<MAXSIZE; i++)
{
for(int j=0; j<MAXSIZE; j++)
{
if(draw[i]==entry[j])
cout << draw[i] << " ";
}

}

}```
I output it like this..

Code:
`cout << numbers(draw, entry) << endl;`
Any reason why the extra number is showing up?
Thanks

10. When you call this:

Code:
`cout << numbers(draw, entry) << endl;`
You end up trying to call numbers() and then print it's return number.

That would be fine if you're returning a number that makes sense to your program, but if you look at your definition of the function, you declare it to return an int, but you're returning nothing, which is wrong.

I wrote multiple posts related to this subject, in detail for the x86 processor, but the important thing to understand is that your function will return a value no matter if you forget to return one or not (and assuming it compiles and runs). It just so happens that 2293468 was the number returned.

I don't think you should be printing the return value of numbers() anyway, but whatever you decide to do with it, you should explicitly return a value, or declare it to return void.

11. Ok I used void instead and it works, finally!
Thankyou so much for your help.
Sorry i seemed so clueless..