You won't know what check_add is returning ... unless you print it out! Make your penultimate line std::cout << check_add(ar,size,k);
If you want true or false instead of 1 or 0 then make that std::cout << std::boolalpha << check_add(ar,size,k);
Incidentally, your code won't work if k is twice one element in the array, since k-arr[i] = arr[i], so it may incorrectly report true. See what happens if you change k to 6.
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
What am i missing here?
Beyond the include files of "map" and "array" that yo do not use the first thing I noticed is that the function returns a value that is never used.
For the "main" function give this a try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main()
{
bool retVal{};
int ar[]{ 10, 15, 3, 7 };
int size{ 4 };
int k{ 17 };
retVal = check_add(ar, size, k);
std::cout << "\n " << std::boolalpha << retVal << '\n'; // <--- "boolalpha" prints the word not the number.
return 0; // <--- Not required, but makes a good break point.
}
From C++11 on the "=" in lines 4 - 6 are not needed just the {}s. Empty {}s will initialize the variable to (0) zero Or as shown you can put something between the {} to give the variable a value other than (0) zero.
Although the function "check_add" appears to work I believe it does not work well. One thought I had: what if there are two sets of numbers that add up to 17. The second for loop would only find the first match and leave the function. You would never know about the second set of numbers.
Then there is what lastchance said about "k".
One point I would make is try to give variable names something better than a single letter. A decent name makes the program easier to understand and that is one of the biggest points to make the code as easy to read and follow as possible. Not just for others, but mostly for your-self.
As I have read in the past a variable name should be a noun, sometimes a verb, that describes what it is or what it is used for.