Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
>The User will enter an array , its size and the value of k .
>The function will then check whether there exists two numbers whose sum equals k .
>if yes , true will be returned , if no , false will be returned .

>Here is the link to the code: beep1.cpp · GitHub[^]

What I have tried:

>Now I have been able to write the function however I have not been able to incorporate the false part .
>My code only returns TRUE if there exists such pairs in the integer array .
>I tried using bool and then using 'return true' and return 'false' but that didn't work

>I tried using #define function to set TRUE as 1 and FALSE as 0 but that also didn't work properly.
Posted
Updated 29-May-18 10:30am
v2
Comments
Patrice T 29-May-18 12:06pm    
And you have a question ?
Paste your code in question
Use Improve question to update your question.
CHill60 29-May-18 12:08pm    
I can't follow your link to your code. Post the relevent code here

THis is your homework, so we aren't going to do it for you - and there should be no need if you have written the function correctly.

The way I'd do it:
Declare two #defines:
C++
#define TRUE (1==1)
#define FALSE (1==0)
In the function, declare a integer:
C++
int isFound = FALSE;
Then use a loop to to see if you find the two numbers. If you do, set isFound to TRUE and use break to exit the loop.
At the end of the function, return the value of isFound
 
Share this answer
 
Comments
[no name] 29-May-18 12:41pm    
This is not my homework . I don't want you to do it for me . I did try using #define,but did not use any variable to store FALSE like isFound. Thank you .
You don't need to define values for TRUE and FALSE. You have tagged this question as being for C++ and the language has the keywords true and false defined in it and that is all you need.

Here is some pseudo code that should give you an idea.
C++
while( looping )
{
   if( array[m] + array[n] == valueK )
       return true;
}
return false;
The looping part should look like the loops for a bubble sort and check every possible pair of values and not the same one twice. Here is a "dumb, brute force" way to do that. I will leave it to you to figure out a more optimal way.
C++
// dumb loops - not recommended but the program can work with them.
int last = size - 1;
for( int m = 0; m < last; ++m )
   for( int n = 0; n < last; ++n )
   {
      if( n == m )
          continue;    // same index - skip this one
      // evaluate sum of values here
   }
If find that you can't figure out a better way to write the loops then look up some code on implementing a bubble sort. That should show you an example of better loops.
 
Share this answer
 
Comments
[no name] 30-May-18 1:25am    
I did try using return true and return false . At that time it didn't work . Now I tried it again and it worked. About the loop , yes my loop is indeed a dumb one . I will have optimize it . Thank you for your help .
[no name] 30-May-18 2:03am    
I rewrote the function and incorporated some of bubble sort into the second loop . Please have a look .

int two_sums(int str[],int k,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(str[i]+str[n-1-j]==k)

return true;
}

}
return false;
}
Rick York 30-May-18 3:32am    
You are close - here is how the bubble sort loop should look :
int last = n - 1;
    for( int i = 0; i < last - 1; ++i )
        for( int j = i + 1; j < last; ++j )
        {
        }
The key is the loop start and end values are staggered so i will never be the same as j.
KarstenK 30-May-18 2:26am    
if(str[i]+str[j]==k) is clearer. Handle the case (i == j) somehow.

Tip: make an output, when true for debugging.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900