Finding the Sum to Pairs [solved]

I was recently asked this question and  was stumped on the possibilities of this answer so for my satisfaction .. I came up with an answer.
Question : Find The Value Pairs in this Integer Array that add up to the given Sum. Write a Boolean Function for this.

static Boolean HasMatches(int Sum, int[] numbers)
{
Boolean _hasmatch = false;
Double total = 0;
for (int i = 0; i < numbers.Length – 1; i++)
{
int item1 = Convert.ToInt32(numbers[i]);
int item2 = Convert.ToInt32(numbers[i + 1]);
total = item1 + item2;
Console.WriteLine(“Totals [” + item1 + “+” + item2 + “]=” + total.ToString());

if (total == Sum)
{
_hasmatch =true;
}

}
return _hasmatch;
}

private void button1_Click(object sender, EventArgs e)
{
int[] numbers = {-1, 0 ,4, 136, 6, 1, 2, 3, -2,142};
if( HasMatches(140,numbers))
{
button1.Text=”TRUE”;
}
else
{
button1.Text=”FALSE”;
}

}

Advertisements

One comment

  1. I think the biggest frustration to these puzzles sometimes is how the answers are right in front of you and we tend over think the solution when dot net makes it so easy for us !!

    Matt G.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s