I'm a beginner at C++; all my previous programming experience is mainly through PLCs in industrial maintenance.
I have to accept 10 integers and a target value, then print all the pairs that sum to the target.
The pairs cannot be of the same integer. For example, if the target value is 10 then we cannot have a pair of 5 + 5. This code basically executes well enough for me, however I get an overloaded int out at the end; I'm curious why that might be?
Also I understand there are much easier ways to accomplish this task. I feel pretty good with what I was able to come up with given the amount of time I have devoted so I certainly am fine with it, however I wouldn't mind seeing some examples how much more seasoned programmers would have attempted this, if anyone out there is bored.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int run, a, b, ints[11], out[45];
cout << "\n\t\tAfter inputting 11 integers, this program will evaluate\n\tthe first 10 integers, then will display the pairs of said integers\n\twhich when added together the total will equal the 11th integer.";
cout << "\n\n\tPlease enter 11 integers: \n\n";
for (run = 0; run <= 10; run++)
{
cout << "\t\t"; cin >> (ints[run]);
}
system("cls");
run = 0;
for (a = 0; a < 10 && run <= 10; a++)
{
for (b = 1; b < 10 && run <= 10; b++)
{
if (ints[10] == (ints[a] + ints[b]) && ints[a] != ints[b] && run <= 10)
{
out[run] = ints[a];
run++;
out[run] = ints[b];
run++;
}
}
}
cout << "\n\t\tPerforming critical calculations . . .\n\t\t";
system("pause");
cout << "\n\t\t";
if (*max_element(out, out + 10) == 0)
cout << "No sum of integers found to equal desired total. ";
else
{
cout << "Pairs of integers that equal the desired total are as follows; ";
for (int index = 0; index < 11; index++)
{
if (out[index] > 0)
{
cout << "(" << out[index++] << " , " << out[index] << ")";
if (index < 11)
cout << ", ";
else if (index == 11)
break;
}
}
}
system("pause");
return 0;
}
out
can get away with only 10 slots, given that there are (10 choose 2) = 45 possible pairs of inputs, and each pair consumes 2 slots inout
? Also, why did you misspellPreforming
[sic]? \$\endgroup\$