--Originally published at BRENDA
This was a simpler task than the ones before because it was based on the program I had already done of Fun with Numbers, but instead of simply doing the operations inside main (), I created a function for each operation: sum, substraction, multiplication, division and residue. For this I learnt about the Mastery Topic #6 & #7, functions. I learnt how to do it by reading the book Ken provided (http://www.greenteapress.com/thinkcpp/thinkCScpp.pdf) and by the explanation Ken gave in class.
This is an example of how I created and named a function:
int mysum (int x,int y) {
return x+y;
}
And then I just put: cout<< mysum (a,b); inside main () to do the operation.
Here is the code:
#include <iostream>
using namespace std;
int mysum (int x,int y) {
return x+y;
}
int mysubstraction (int x,int y){
return x-y;
}
int times (int x,int y){
return x*y;
}
int divide (int x,int y){
return x/y;
}
int left (int x,int y){
return x%y;
}
int main ()
{
int a;
cout <<“Please enter A:”;
cin>>a;
int b;
cout <<“Please enter B:”;
cin>>b;
cout << ” A + B = “;
cout<< mysum (a,b);
cout << “\r\n A – B = “;
cout << mysubstraction (a,b);
cout <<“\r\n A * B = “;
cout << times (a,b);
cout << “\r\n A / B = “;
cout << divide (a,b);
cout <<“\r\nThe remainder of A/B is “;
cout << left (a,b) <<endl;
return 0;
}