Look for pair of integer

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.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include 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...?

 ``12345678`` ``````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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include #include #include #include using namespace std; template void store(vector& 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 void locate_pairs(vector& vec, const T target) { for (vector::iterator itr = vec.begin(); itr != vec.end(); ++itr) { if (target - *itr > 0) { T n1 = *itr; T n2 = target - *itr; vector::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 v; string input; cout << "Enter a number or 'Y' to stop " << endl; do { cin >> input; if (is_number(input)) store(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(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.