help c++ Develop \"incrementally\" - follow the order of the FIXMEs! Incremental
ID: 3668345 • Letter: H
Question
help c++
Develop "incrementally" - follow the order of the FIXMEs!
Incremental Development Steps
(1) Write a stub for a function named add that takes 2 int parameters and returns their sum. See Figure 5.4.2 on how to write a stub.
(2) Write a stub for a function named subtract that takes 2 int parameters and returns their difference.
(3) Prompt the user for two integers and store them into separate variables. Then output the two integers on two separate lines.
Now test that your program compiles and gets the 2 integers from the user. You can also begin submitting this here in zyBook at this point to begin getting feedback at each step of the development process.
(4) After the call to the "add" function, write a similar call to the "subtract" function. Make sure that the first integer is the first argument and the second integer is the second argument.
(5) Write the full definition of the "add" function. Inside the function, add the two integer parameters and return their sum. Note: It is a function that takes two integer parameters (passed by value) and returns an integer.
(6) Output the result from adding the first and second integers.
(7) Write the full definition of the "subtract" function. Inside of the function, subtract the second integer parameter from the first parameter and return their difference. Note: It is a function that takes two integer parameters (passed by value) and returns an integer.
(8) Output the result from subtracting the second integer from the first integer.
Explanation / Answer
#include <iostream>
using namespace std;
int add(int a, int b){
return a+b;
}
int subtract(int a, int b){
return a-b;
}
int main() {
int num1 = 0;
int num2 = 0;
int sum = 0; //sum of numbers
int diff = 0; //difference of numbers
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2
cout << "First Integer: " << num1 << endl;
cout << "Second Integer: " << num2 << endl;
// Call add function
sum = add(num1, num2);
// FIXME (4) Call subtract function
diff = subtract(num1, num2);
// FIXME (6) Output result of num1 + num2
cout << num1 << "+" << num2 << "=" << sum << endl;
// FIXME (8) Output result of num1 - num2
cout << num2 << "-" << num2 << "=" << diff << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.