Look for pair of integer

Please help me out with this problem......
Given a sorted array of 5 integers and a target integer value, your program needs to print out all pairs of integers which have sum equal to the target value. If there is no pair of integers to return, print out " pair of integers in the given array can be summed to the target value". The array of integers and the target value should be given from keyboard.
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
#include<iostream>
const int ARRSIZE = 5;
using namespace std;

int main ()
{
	int item[ARRSIZE], a;
	int sum, target;
	int counter;
	
	cout<< "enter five number\n";
	sum = 0;
	for(counter = 0; counter < ARRSIZE; counter++)
	{
		cout<< "Enter " << counter + 1 << " number: ";
		cin >> item[counter];
		sum = sum +item [counter];
	}
	cout << endl;

	cout <<"Enter a target number ";
	cin >> target;
	cout << endl;

	//cout << "The sum of the number is: " << sum << endl;
	do
	{


	}
	while(sum <=target);

I was able to get the sum of array but after that i am lost can anybody guide me thought this .
Thank you.
From what i understand you have an sorted array and you need to find the pairs that have the sum = to another number.
For example
item[0] = 1;
item[1] = 2;
item[2] = 3;
item[3] = 4;
item[4] = 5;

and target = 4;
You need to find the pairs of numbers that are = to 4; In this case they are 1 and 3(item[0] and item[2]). From what i can see you are just adding the numbers from item.
You would need to add the numbers 2 at a time and check if there sum is equal to target.

You can use nested loops to add the numbers 2 by 2 and check.
Example you ad 1 and 2, then 1 and 3, then 1 and 4, then 1 and 5, then 2 and 3, then 2 and 4 and so on.
When you find a pair you hold them in a new variable or something. Hope it helps.
Would it not be better, when searching for a pair that add up to the target value (if that is in fact what is being asked) to take an array item away from target value then do a search for the balance, then repeat etc...
Something like this...?

1
2
3
4
5
6
7
8
for(int i = 0; i < ARRSIZE; ++i)
{
    for(int j = i + 1; j < ARRSIZE; ++j)
    {
      if((item[i] + item[j]) == target)
        cout << "Pair: item[" << i << "]: "<< item[i] <<" + item[" << j << "]: " << item[j] << endl;
    }
}
Is this the sort of thing you are looking for:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

template <typename T>
void store(vector<T>& vec, T val)
{
	vec.push_back(val);
}

bool nocase_compare(string s1, string s2)
{
	transform(s1.begin(), s1.end(), s1.begin(), toupper);
	transform(s2.begin(), s2.end(), s2.begin(), toupper);
	return s1.compare(s2) == 0;
}

bool is_number(string s)
{
	bool ret(s.length());

	for (string::iterator itr = s.begin(); itr != s.end() && ret; ++itr)
		ret = isdigit(*itr);

	return ret;
}

template <typename T>
void locate_pairs(vector<T>& vec, const T target)
{
	for (vector<T>::iterator itr = vec.begin(); itr != vec.end(); ++itr)
	{
		if (target - *itr > 0)
		{
			T n1 = *itr;
			T n2 = target - *itr;

			vector<T>::iterator f = itr;
			f = find(++f, vec.end(), n2);

			while (f != vec.end())
			{
				cout << "Pair: " << n1 << " " << n2 << " for target: " << target << endl;
				f = find(++f, vec.end(), n2);
			}
		}
	}
}

int main()
{
	vector<int> v;
	string input;
	cout << "Enter a number or 'Y' to stop " << endl;

	do
	{
		cin >> input;
		if (is_number(input))
			store<int>(v, atoi(input.c_str()));
	} while (!nocase_compare(input, "Y"));

	cout << "Enter a target number: ";
	int target(0);
	
	do
	{
		cin >> input;
		if (is_number(input))
			target = atoi(input.c_str());
	} while (target == 0);

	locate_pairs<int>(v, target);

	return 0;
}
Thank You Very very much for your help.
i was finally was able to solve this problem.
again thank guysfor all of ur time and help
Topic archived. No new replies allowed.