Answer on Question#37765- Programming, C++
1. Write a function that, using default arguments, allows user to take the sum of anywhere between 2 and 4 integers.?
Solution.
#include <iostream>
using namespace std;
int sum(int a, int b, int c = 0, int d = 0)
{
return a + b + c + d;
}
int main(int argc, char *argv[])
{
int no[4] = {0};
cout << "Enter 2 to 4 numbers: " << endl;
for (int i = 0; i < 4; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> no[i]; //Ask value array no[]
}
cout << "Answer: " << sum(no[0], no[1], no[2], no[3]) << endl;
system("pause");
return 0;
}