Write the required functions as described by the prototypes and comments. The fu
ID: 3862923 • Letter: W
Question
Write the required functions as described by the prototypes and comments.
The functions should be written below main.
Then, run the code when you are finished to see if you wrote the functions correctly.
#include <iostream>
using namespace std;
int sumDouble(int a, int b);
//Given two int values, return their sum.
//Unless the two values are the same, then return double their sum.
//sumDouble(1, 2) 3
//sumDouble(3, 2) 5
//sumDouble(2, 2) 8
int main()
{
int result;
bool answer;
string value;
cout << "***Testing sumDouble***"<< endl << endl;
result = sumDouble(1, 2);
cout << "Should print 3: " << result << endl;
result = sumDouble(3, 2);
cout << "Should print 5: " << result <<endl;
result = sumDouble(2, 2);
cout << "Should print 8: " << result << endl << endl;
cout << "***End of Tests***" << endl;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int sumDouble(int a, int b);
//Given two int values, return their sum.
//Unless the two values are the same, then return double their sum.
//sumDouble(1, 2) 3
//sumDouble(3, 2) 5
//sumDouble(2, 2) 8
int main()
{
int result;
bool answer;
string value;
cout << "***Testing sumDouble***"<< endl << endl;
result = sumDouble(1, 2);
cout << "Should print 3: " << result << endl;
result = sumDouble(3, 2);
cout << "Should print 5: " << result <<endl;
result = sumDouble(2, 2);
cout << "Should print 8: " << result << endl << endl;
cout << "***End of Tests***" << endl;
return 0;
}
int sumDouble(int a, int b) {
if( a != b) {
return a +b;
}
else{
return 2 * (a + b);
}
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
***Testing sumDouble***
Should print 3: 3
Should print 5: 5
Should print 8: 8
***End of Tests***
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.