Page 1 of 1

Simple Arithmetic Program Rate Topic: -----

#1 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 199
  • Joined: 19-September 12

Posted 07 June 2013 - 08:51 AM

Today I will be presenting a nice simple little program that will conduct addition, subtraction, multiplication, and division on two integers that have been chosen by the user. I will take it step by step as to help beginners learn the process of design. I am by no means a professional but I wanted to contribute what I could to this forum. It has been more than helpful to me in my education and my development as a computer science student. Dream in Code RULES!!!!

Step 1:

I'm sure with most code editors the first program you will ever see is this fun little > 10 line chunk of code here:
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}



As simplistic as this "program" is, I thought this was the coolest thing ever when I saw it for the first time last semester. I made my computer talk with a pop up window on my desktop! What else could I make my computer do?

Step 2:

Now let's go a step further. Let's do some math!
- To begin take out your cout hello world line....
- Replace that line with
cout << "Enter two numbers you want to perform addition, subtraction, multiplication,  and division on:" << endl;



Not much has changed. You are still telling the your program to output text with "cout <<". Except this time, we are asking the user to do something. We want them to input two integers to begin our arithmetic. How do we save those responses? Well...instead of outputting information with cout....we could TAKE IN information with cin >>.
-Right below your new cout line, place this code.
double x, y;
cin >> x >> y;


- We have told our compiler that the user is inputting two double numbers, x and y.
- A double number is taken out multiple decimal places (great for division).
- Take note that the above is the same thing as:
double x;
double y;
cin >> x;
cin >> y;


- Notice how much more efficient the first code was. Why waste time typing all of that (2nd code)?

Step 3:
Now we need to perform our operations with the numbers provided to us. How do we add, subtract, etc? This is a good time to introduce functions. Think of a C++ program as a house; inside that house are rooms with shut bedroom doors and living rooms. Think of the living rooms as the main () function and the bedrooms as our other functions. Inside of those bedrooms there are instructions doing their own thing and don't care what their housemates are doing outside their room. Let's see what is going on in the addition room.
-Above the main function but below using namespace std; Type the following:
double add(double a, double b)
{
  double r;
  r = a + b;
  return (r);
}


- double is the "type" of our function
- add is simply the "name" of our function
- the items inside of the (....) are our parameters (allow us to pass arguments to the function when it is called)
What does that stuff inside the {} mean? Well, we are saying that a double (integer) r exists. We are also saying that r is equal to a + b. I.E one number plus another number. Finally, we are saying, "spit out the answer" with return r; when we ask for it. I bet you can think of what we are going to do for subtraction and the rest.

Step 4:

Below the addition function finish up by coding in the other functions:
double subtract(double a, double b)
{
	double r;
	r = a - b;
	return (r);
}

double multiply(double a, double b)
{
	double r;
	r = a * b;
	return (r);
}

double divide(double a, double b)
{
	double r;
	r = a / b;
	return (r);
}



-Inside all of these other rooms you may notice it is the same kind of setup as our addition. Only our logic (arithmetic) has changed.

Step 5:

Let's get some answers! Alright, now remember the metaphor about the bedrooms? Let's pretend we are writing down some numbers (x, y) on a piece of paper and sliding them under the door to our functions. Once inside the room our scribbled down numbers are being manipulated by the instructions. Keep in mind the final instruction in each "room" was a return statement. That means that the function will slip our paper under the door back to us in the living room with an answer when it is finished!
-Type this code inside our main function below our cin >> line where we first asked for our two numbers (x, y).
cout << "The result of addition is:" << [b]add(x, y)[/b] << endl;


-We called our add function by "calling its name from the living room". We also slid it some numbers under the door. Notice how our call is similar to "add (double a, double b)"? The function is using our new numbers (x, y) as (a, b) so that they may be manipulated.
- The process to call the other functions is the same.

Step 6:
The final program....
#include <iostream>

using namespace std;

double add(double a, double b)
{
	double r;
	r = a + b;
	return (r);
}

double subtract(double a, double b)
{
	double r;
	r = a - b;
	return (r);
}

double multiply(double a, double b)
{
	double r;
	r = a * b;
	return (r);
}

double divide(double a, double b)
{
	double r;
	r = a / b;
	return (r);
}

int main()
{
	cout <<
		"Enter two numbers you want to perform addition, subtraction, multiplication,  and division on:"
		<< endl;

	double x, y;
	cin >> x >> y;

	cout << "The result of addition is:" << add(x, y) << endl;

	cout << "The result of subtraction:" << subtract(x, y) << endl;

	cout << "The result of multiplication is:" << multiply(x, y) << endl;

	cout << "The result of division is:" << divide(x, y) << endl;

return 0; //This tells our program to GTFO we are done with it.
}



I hope I did not bore any viewers. The reason I drug this out so much was because, being a beginner, I know it is necessary for some people to need to see a program come together with explanations instead of slapping them with a finished product saying "here it is, learn" when they have no idea what is going on inside. Any thoughts from professionals are welcome and please feel free to add suggested changes as I'm sure somewhere it could have been coded more elegantly. Though, for a simple program, I feel it is sufficient to learn some basics.

Happy coding!

Is This A Good Question/Topic? 1
  • +

Page 1 of 1