## C++: Basic Arithmetic

• 2,747 Views
• 25 Replies
Afro-Ninja
Afro-Ninja
• Member since: Mar. 2, 2002
• Offline.
Forum Stats
Moderator
Level 44
Game Developer
C++: Basic Arithmetic 2006-02-19 13:53:29

C++: Main

Basic Arithmetic
[also applicable to C]

C++, just like any other language, has a set of arithmetic operators for you to use in your
programs. Although basic, it's nice to become familiar with their implementations.

You basic operators consist of:

- Subtraction
* Multiplication
/ Division
% Modulus

These operators work much in the way you think they would. Take for example this sample program:

#include "stdafx.h" //this is for .NET, it is the basic equivalent of #include <iostream>

using std::cout;
using std::endl;

int main()
{
int num1 = 5;
int num2 = 4;

cout << num1+num2 << endl; //outputs 9
cout << num1-num2 << endl; //outputs 1
cout << num1*num2 << endl; //outputs 20
cout << num1/num2 << endl; //outputs 1
cout << num1%num2 << endl; //outputs 1

return 0;
}

The only operator that may throw you off here is the modulus (%) operator. Modulus returns the remainder of
a division result. Both of our numbers, num1 and num2, were declared as integers. Integers are whole numbers,
and do not use decimal points. Four into five goes one time, with one left over as the remainder. That is why
5/4 and 5%4 both output 1. Other modulus outcomes:

6 % 2 = 0
12 % 10 = 2
100 % 70 = 30

Integer vs. Float division
Notice that above that when we divided five by four we got a whole number as the result. That's because we were
working with two whole numbers. What if you want a more accurate result? Floating point division is the answer.
Let's take a look at another program

#include "stdafx.h"

//#using <mscorlib.dll>

using std::cout;
using std::endl;

int main()
{
float num1 = 5;
float num2 = 4;

cout << num1/num2 << endl; //outputs 1.25

int num3 = 5;
float num4 = 4;

cout << num3/num4 << endl; //outputs 1.25

return 0;
}

Notice now that num1 and num2 have both been declared as type 'float.' This allows for decimal point precision.
The answer is now 1.25 instead of one. Also notice that when one variable is an integer and one is a float, you
will still get an answer in float form. Lastly, keep in mind that modulus cannot be used with floating point numbers.

Storing the result
Storing the result is a simple process. In the previous examples, although we did calculations with num1-num4, none of
their original values were changed. To store the result just create an additional variable.

int num1 = 10;
int num2 = 5;

cout << answer << endl; //outputs 15

In this case num1 and num2 both remain their original values, but 'answer' now holds their sum. An important thing to keep in
mind is that doing any kind of floating point math and storing it in an integer will result in a truncated number.

float num3 = 20;
float num4 = 12;

cout << answer << endl; //outputs 1

Order of Operations
I'm not going to cover this in detail, but it's worth noting that all calculations in C++ follow the same order
of operations that you learned in school.

int answer = 5 + 6 * 3 / 2 + 4 * 2 - 3;

The results can of course be changed by adding parentheses

int answer = (5 + 6) * 3 / (2 + 4) * 2 - 3;

Changing the original values

The above examples are great if you just want to output an answer or store it in another variable, but what if you
want to change your original values? You can simply reassign a calculation into an existing variable

int num1 = 5;
num1 = num1 + 10

cout << num1 << endl; //outputs 15

This works, but is cumbersome. There is a shortcut avaiable:

int num1 = 5;
num1 += 10;
cout << num1 << endl;

See the "+=" there? You can use it for any of the other arithmetic operators

+=, -=, *=, /=, and %=

What it does is say "take the value on left, perform the given operation on it with the value on the right, and put it
back in inside the value on the left." These operators can come in very handy.

Inrementation and Decrementation.

There is an even quicker shortcut available for when you only want to increase or decrease your original variable by 1.

int num = 5;
num++;
num--;
cout << num << endl; //outputs 5

You can simply use ++ to increment by 1, or -- to decrement by 1. You can put them either before or after the value to be affected.
This is called prefixing and postfixing

num++; (postfixing)
++num; (prefixing)

In the case above, it doesn't matter which you use. Num will be increased by one in both instances. Here's where the difference comes:

int num1;
int num2 = 4;

num1 = ++num2;

cout << "num1 is " << num1 << " and num2 is " << num2 << endl;
//outputs 'num1 is 5 and num2 is 5'

In this example one will be added onto num2, and then assigned to num1. Both variables now contain the value 5

int num1;
int num2 = 4;

num1 = num2++;

cout << "num1 is " << num1 << " and num2 is " << num2 << endl;
//outputs 'num1 is 4 and num2 is 5'

But in this example num2 is assigned to num1, then incremented. num1 contains 4, num2 contains 5.

That wraps up my tutorial on basic arithmetic in C++. Feel free to leave comments and suggestions.

shazwoogle
shazwoogle
• Member since: Sep. 27, 2004
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++: Basic Arithmetic 2006-03-16 02:34:29

arrh thank you :D
theres only 1 thing i didnt get and that was there was some mention of .NET.. call me a noob but wtf is .NET, i see alot of thins like VB.NET and stufff... what does that mean?

Johnny
Johnny
• Member since: Apr. 17, 2004
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to C++: Basic Arithmetic 2006-03-16 06:22:48

At 3/16/06 02:34 AM, shazwoogle wrote: arrh thank you :D
theres only 1 thing i didnt get and that was there was some mention of .NET.. call me a noob but wtf is .NET, i see alot of thins like VB.NET and stufff... what does that mean?

It's basically a program that allows you to build and program C++ files. You know flash's actionscript window? It's kind of like that. It does the editing and color changing and all that good stuff. It also builds and compiles everything for you and is useful for quite a few different languages. I "think" the express version is free to use for a year. I'm not entirely sure, because my school just gives me a link to download, and poof, I have it.

White-Rhyno
White-Rhyno
• Member since: Apr. 28, 2003
• Offline.
Forum Stats
Member
Level 38
Blank Slate
Response to C++: Basic Arithmetic 2006-03-16 08:50:55

At 2/19/06 01:53 PM, Afro_Ninja wrote: #include "stdafx.h" //this is for .NET, it is the basic equivalent of #include <iostream>

Not really dude. Or at all.. stdafx.h is what Microsoft gives you when you choose a project with precompiled headers.

For those of you thant don't know, precompiled headers are basically a time saving feature implemented by some compilers. stdafx.h just contains commonly used, but not oft changed header files, like stdio.h or iostream. It's not .NET specific, and stdafx.h is a commonly used file name for precompiled headers, regardless of the platform. Precompiled headers are compiled to object code on the first build, then if nothing is changed, linked with each successive build. Speeds up build time since you don't have to keep rebuilding stuff that hasn't changed.

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Member
Level 19
Game Developer
Response to C++: Basic Arithmetic 2006-03-17 20:17:04

i dont understand what << endl does

0x41
0x41
• Member since: Dec. 30, 2004
• Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++: Basic Arithmetic 2006-03-17 20:34:45

At 3/17/06 08:17 PM, ImpotentBoy2 wrote: i dont understand what << endl does

File/Console output stream. Also left bit shift, but don't worry about that.

Johnny
Johnny
• Member since: Apr. 17, 2004
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to C++: Basic Arithmetic 2006-03-17 20:59:05

At 3/17/06 08:17 PM, ImpotentBoy2 wrote: i dont understand what << endl does

When you output it..., it's basically a line break. If you do

cout << "Hello "<<endl
<< "World";

The output will be

Hello
World

On 2 seperate lines, just like that.

0x41
0x41
• Member since: Dec. 30, 2004
• Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++: Basic Arithmetic 2006-03-17 21:07:50

At 3/17/06 08:34 PM, 0x41 wrote:
At 3/17/06 08:17 PM, ImpotentBoy2 wrote: i dont understand what << endl does
File/Console output stream. Also left bit shift, but don't worry about that.

Oops, didnt read the "endl" part, my bad, ignore my above post.

Afro-Ninja
Afro-Ninja
• Member since: Mar. 2, 2002
• Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to C++: Basic Arithmetic 2006-03-20 19:19:15

At 3/16/06 08:50 AM, White_Rhyno wrote:
At 2/19/06 01:53 PM, Afro_Ninja wrote: #include "stdafx.h" //this is for .NET, it is the basic equivalent of #include <iostream>
Not really dude. Or at all.. stdafx.h is what Microsoft gives you when you choose a project with precompiled headers.

For those of you thant don't know, precompiled headers are basically a time saving feature implemented by some compilers. stdafx.h just contains commonly used, but not oft changed header files, like stdio.h or iostream. It's not .NET specific, and stdafx.h is a commonly used file name for precompiled headers, regardless of the platform. Precompiled headers are compiled to object code on the first build, then if nothing is changed, linked with each successive build. Speeds up build time since you don't have to keep rebuilding stuff that hasn't changed.

ah.. right.. well I opened up stdafx.h and all I saw was

#include <iostream>
#include <tchar.h>

I thought it basically acted as a way to save space with all your include statements, I didn't know about the precompiled part. Err.. well.. if I left out the stdfx part and just did an include iostream, would that be precompiled as well?

that probably sounds newby : (

White-Rhyno
White-Rhyno
• Member since: Apr. 28, 2003
• Offline.
Forum Stats
Member
Level 38
Blank Slate
Response to C++: Basic Arithmetic 2006-03-20 22:53:08

At 3/20/06 07:19 PM, Afro_Ninja wrote: I thought it basically acted as a way to save space with all your include statements, I didn't know about the precompiled part. Err.. well.. if I left out the stdfx part and just did an include iostream, would that be precompiled as well?

Y'know, that's an interesting question. I don't use precompiled headers myself, though I probably should. It would just be a compiler flag anyway, so I suppose if you flagged iostream as a precompiled header then it would, y'know, precompile it.

Claxor
Claxor
• Member since: Oct. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to C++: Basic Arithmetic 2006-03-21 09:48:45

Okay, here goes a noob question

What does the << endl; part do at the end of the couts?

mike
mike
• Member since: Feb. 24, 2000
• Offline.
Forum Stats
Supporter
Level 21
Programmer
Response to C++: Basic Arithmetic 2006-03-21 10:31:00

<< endl puts a newline onto the output stream (short for 'endline', so the following text is on the next line. :)

Claxor
Claxor
• Member since: Oct. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to C++: Basic Arithmetic 2006-03-21 10:53:35

At 3/21/06 10:31 AM, mwelsh wrote: << endl puts a newline onto the output stream (short for 'endline', so the following text is on the next line. :)

Thanks :D

CaptinChu
CaptinChu
• Member since: Sep. 11, 2005
• Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to C++: Basic Arithmetic 2006-04-15 20:58:05

At 3/21/06 10:53 AM, Claxor wrote:
At 3/21/06 10:31 AM, mwelsh wrote: << endl puts a newline onto the output stream (short for 'endline', so the following text is on the next line. :)
Thanks :D

Another way to do it would be:

std::cout "Hello\n"
std::cout "World."

The '\n' creates a new paragraph. (Or, as my book described it, white space.)

seel
seel
• Member since: Jun. 27, 2005
• Offline.
Forum Stats
Member
Level 21
Musician
Response to C++: Basic Arithmetic 2006-05-19 11:23:03

At 4/15/06 08:58 PM, CaptinChu wrote:
At 3/21/06 10:53 AM, Claxor wrote:
At 3/21/06 10:31 AM, mwelsh wrote: << endl puts a newline onto the output stream (short for 'endline', so the following text is on the next line. :)
Thanks :D
Another way to do it would be:

std::cout "Hello\n"
std::cout "World."

The '\n' creates a new paragraph. (Or, as my book described it, white space.)

Or you could fuck the std:: and use "using namespace std" under the includes and just write cout, and you forgot the << to.. then again \n is better to use than <<endl, cuz it just save you some lines of code and it does the same, start a new line. And you dont have to start a new cout for a new line, just do cout << "i\nlike\njuicie\ncocks." .

errr.. okay just ignore that last output XD And go emo on me if i'm wrong.
fr334rtisT
fr334rtisT
• Member since: Feb. 21, 2009
• Offline.
Forum Stats
Member
Level 02
Programmer
Response to C++: Basic Arithmetic 2009-02-26 21:42:31

``+=, -=, *=, /=, and %=``

These are called Assignment Operators

These are operators that assign the same variable + or - or * or / or % with something else.

val*= 20 its the same as val = val * 20

That's pretty much what it does...

Just clarifying the name of these operators.

Thanks!
fr334rtisT

DearonElensar
DearonElensar
• Member since: Jun. 10, 2002
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++: Basic Arithmetic 2009-02-26 21:47:13

At 5/19/06 11:23 AM, seel wrote:
At 2/26/09 09:42 PM, fr334rtisT wrote:

See the problem there? we *really* don't like this kind of behavior.

fr334rtisT
fr334rtisT
• Member since: Feb. 21, 2009
• Offline.
Forum Stats
Member
Level 02
Programmer
Response to C++: Basic Arithmetic 2009-02-26 21:52:54

At 2/26/09 09:47 PM, DearonElensar wrote:
At 5/19/06 11:23 AM, seel wrote:
At 2/26/09 09:42 PM, fr334rtisT wrote:
See the problem there? we *really* don't like this kind of behavior.

What kind of behavior are you talking about, I'm just saying the things as they are suppose to be said, the Right way.

If you tell me what kid of behavior you are talking about I would "behave" taking your terms as "kind" of"behavior" I suppose to have, I'm just commenting to a post here and I am having a "wrong behavior" just because I'm saying the things as they had to be.

pardon me then...

DearonElensar
DearonElensar
• Member since: Jun. 10, 2002
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++: Basic Arithmetic 2009-02-26 21:54:30

At 2/26/09 09:52 PM, fr334rtisT wrote: What kind of behavior are you talking about, I'm just saying the things as they are suppose to be said, the Right way.

Posting stuff that is not needed at all on posts that are 3 years old.
Mainly the bumping of 3 year old dead topics is really annoying.

fr334rtisT
fr334rtisT
• Member since: Feb. 21, 2009
• Offline.
Forum Stats
Member
Level 02
Programmer
Response to C++: Basic Arithmetic 2009-02-26 22:00:46

At 2/26/09 09:54 PM, DearonElensar wrote:
At 2/26/09 09:52 PM, fr334rtisT wrote: What kind of behavior are you talking about, I'm just saying the things as they are suppose to be said, the Right way.
Posting stuff that is not needed at all on posts that are 3 years old.
Mainly the bumping of 3 year old dead topics is really annoying.

Well, Why if its really "annoying" as you said, why still here and not in the archive or why isn't a "closed Post" ?

So even if its 1000 years old, the bad denomination of a concept should be Kemp as is?

Pardon me then, I dare to say again...

DearonElensar
DearonElensar
• Member since: Jun. 10, 2002
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++: Basic Arithmetic 2009-02-26 22:05:09

At 2/26/09 10:00 PM, fr334rtisT wrote: Well, Why if its really "annoying" as you said, why still here and not in the archive or why isn't a "closed Post" ?

So even if its 1000 years old, the bad denomination of a concept should be Kemp as is?

Pardon me then, I dare to say again...

There is no need to close stuff because pretty much anyone here knows to not be a jackass and bump 3 year old posts.

fr334rtisT
fr334rtisT
• Member since: Feb. 21, 2009
• Offline.
Forum Stats
Member
Level 02
Programmer
Response to C++: Basic Arithmetic 2009-02-26 22:15:15

At 2/26/09 10:05 PM, DearonElensar wrote:
At 2/26/09 10:00 PM, fr334rtisT wrote: Well, Why if its really "annoying" as you said, why still here and not in the archive or why isn't a "closed Post" ?

So even if its 1000 years old, the bad denomination of a concept should be Kemp as is?

Pardon me then, I dare to say again...
There is no need to close stuff because pretty much anyone here knows to not be a jackass and bump 3 year old posts.

Well, for your information I'm like kind of new around here, at least with this account ...

I don't see any annoyance in commenting in post that had been inactive for long time of period, In the contrary, keeping it alive its like a " New Beginning" , Well for me its as new as the Presidents of the United States...

If you tell me the "real" problem of why posting in active-open post here in the forum, I will understand the meaning.

DearonElensar
DearonElensar
• Member since: Jun. 10, 2002
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++: Basic Arithmetic 2009-02-26 22:21:23

At 2/26/09 10:15 PM, fr334rtisT wrote: Well, for your information I'm like kind of new around here, at least with this account ...

I don't see any annoyance in commenting in post that had been inactive for long time of period, In the contrary, keeping it alive its like a " New Beginning" , Well for me its as new as the Presidents of the United States...

If you tell me the "real" problem of why posting in active-open post here in the forum, I will understand the meaning.

This has nothing to do with being new here, it is considered rude anywhere i have ever gone to bump 3 year old posts with nothing new *at all*
Furthermore there is no need to "keep it alive", in fact in that parallel you try to make it would be asking Bill Clinton for president, *there is nothing new about what you did*
But then again, it's clear you decided to be a jackass and continue to be on, using faux intellectual reasons to justify what you do instead of listening what others say so this is useless anyway.

fr334rtisT
fr334rtisT
• Member since: Feb. 21, 2009
• Offline.
Forum Stats
Member
Level 02
Programmer
Response to C++: Basic Arithmetic 2009-02-26 22:28:46

At 2/26/09 10:21 PM, DearonElensar wrote:
At 2/26/09 10:15 PM, fr334rtisT wrote: Well, for your information I'm like kind of new around here, at least with this account ...

I don't see any annoyance in commenting in post that had been inactive for long time of period, In the contrary, keeping it alive its like a " New Beginning" , Well for me its as new as the Presidents of the United States...

If you tell me the "real" problem of why posting in active-open post here in the forum, I will understand the meaning.
This has nothing to do with being new here, it is considered rude anywhere i have ever gone to bump 3 year old posts with nothing new *at all*
Furthermore there is no need to "keep it alive", in fact in that parallel you try to make it would be asking Bill Clinton for president, *there is nothing new about what you did*
But then again, it's clear you decided to be a jackass and continue to be on, using faux intellectual reasons to justify what you do instead of listening what others say so this is useless anyway.

Ok. Ok. I get it! I just wanted to say that Those are actually "Assignment Operators" that
s all! Then you start saying that behavior it shouldn't be used here and I said what?! I didn't so anything wrong!

Man, I don't want to start a riot or anything, I just wanted to add that simple thing as that those are "Assignment Operators"

That's all!

djconnect
djconnect
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++: Basic Arithmetic 2009-02-26 22:50:26

Sticky?

I am a hacker of a Nu-Aira

djconnect
djconnect
• Member since: Jan. 13, 2007
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++: Basic Arithmetic 2009-02-26 22:52:37

Sorry, I meant a link should be added to this from the new to programming page.

I am a hacker of a Nu-Aira