Function not returning bool value


Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17

CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<unordered_map>
#include<map>
#include <array>



bool check_add(int arr[],int sz, int k)
{
    std::unordered_map<int,int> hm;
    
    for (int i = 0; i<sz; i++)
    {
        hm[arr[i]]++;
    }
    
    for (int i = 0; i<sz; i++)
    {
          if(k>arr[i])
          {
             if(hm.find(k-arr[i]) != hm.end())
             {
                 return true;
             }
          }
    }
    return false;
   
}

int main()
{
    
    int ar[] = {10, 15, 3, 7};
    int size = 4;
    int k = 17;

    
    check_add(ar,size,k);
}

OUTPUT:

Program ended with exit code: 0


What am i missing here?
Last edited on
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.

You have a couple of unnecessary headers.


(Please use code tags.)
Last edited on
Hello Vijay0753,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

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.

Andy
Thank you so much guys!
Topic archived. No new replies allowed.