Saturday, March 31, 2012

Arithmetic

Hello to all the bots that live in my little blog and hello to Petteri my second nonbot to post a comment.

Just a quick recap:

  • I'm learning C++ because of an event that took place in my life which made me look back at the past ten years. That made me think of what I could have done in those ten years.
  • I've been at this for a little over a month.
  • I'm using this blog as a personal/public journal on what I've learned so far. My hope is that friendly netizens will pop in from time to time to offer a word of advice or just shoot the breeze about coding.
  • My ultimate goal is to not give up. This blog worked for helping me to stay focused with finishing a game. I'm hoping it will help keep me on track with learning C++ and in ten years time I can look back at these first blog posts and think what a NOOB I was.
  • Lastly, this blog will help me review what I've learned.

Now onto the source code: Arithmetic

#include <iostream>
using namespace std;

int main()
{
        cout << "5 + 5 = " << 5 + 5 << endl;
        cout << "5 - 5 = " << 5 - 5 << endl;
        cout << "5 * 5 = " << 5 * 5 << endl;
        cout << "5 / 5 = " << 5 / 5 << endl;


       cout << "5.0 / 6.0 = " << 5.0 / 6.0 << endl;
       cout << "5 / 6 = " << 5 / 6 << endl;


        cout << "5 + 5 * 5 = " << 5 + 5 * 5 << endl;
        cout << "(5+5) * 5 = " << (5 + 5) * 5 << endl;


        cout << "5 % 5 = " << 5 % 5 << endl;


        system("PAUSE");
        return 0;
}

This bit of source code will display:

5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1
5.0 / 6.0 = 0.833333
5 / 6 = 0
5 + 5 * 5 = 30
(5 + 5) * 5 = 50
5 % 5 = 0

First thing to note is that there are no new lines between any of the arithmetic problems even though I put spaces between some of them. Example:

        cout << "5 + 5 = " << 5 + 5 << endl;
        cout << "5 - 5 = " << 5 - 5 << endl;
        cout << "5 * 5 = " << 5 * 5 << endl;
        cout << "5 / 5 = " << 5 / 5 << endl;


       cout << "5.0 / 6.0 = " << 5.0 / 6.0 << endl;

       cout << "5 / 6 = " << 5 / 6 << endl;


Displays:

5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1
5.0 / 6.0 = 0.833333
5 / 6 = 0

That might not seem like a big deal, but if you don't tell the compiler that you want a space then it will not create any new spaces. One way to put a space between the lines would be:

         cout << "5 / 5 = " << 5 / 5 << "\n" << endl;

This would display:

5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1

5.0 / 6.0 = 0.833333
.
.
.


The reason for this is that \n character.

\n = new line
Couple of things to remember for \n is that it needed to be put between the " ". If you don't then you will get a compiler error. Also, it will go to a new line wherever you put it. For example:

       cout << "5 / 5 \n = " << 5 /5 << "\n" << endl;

      cout << "5.0 / 6.0 = " << 5.0 / 6.0 << endl;

Displays:

5 / 5
 = 1

5.0 / 6.0 = 0.833333

I created 3 new lines in that one line of code. One after 5/5, another after the sum of 5/5 and another with endl;
It might seem like a no brainier, but it takes a while to get used to. The compiler does not care what you intend to do, it only does exactly what you say to do. I've created a few programs where the text was messed up because I forgot to add \n.

Arithmetic expressions:

The compiler also does not care what you type in between the quotes. I could have typed "This is a sandwich  = " << 5 / 5 << endl; and it would have displayed

This is a sandwich = 1

Now after the quotes I had another funnel << and an arithmetic problem. You just need to type the math problem. You don't need any quotes. As a matter of fact you can't use them. Just type the problem in and the compiler will solve it for you and display it. The arithmetic characters are:

+  add
-  subtract
* multiply
/ divide
% modulus

Using those will express a value. Example: The expression 5 + 5 expresses the value 10.

The operators themselves are simple enough .Nothing surprising there except for %
That's a modulus. A modulus gives you the remainder when dividing two numbers. For example:

5 % 5 has a remainder of 0 so the answer is 0
9 % 5 has a remainder of 4 so the answer is 4

There are all kinds of ways I don't understand the modulus and how to use it so I'm not going to pretend to. I do know that when creating random numbers I need to use it, but random is a whole other type of beast. I'll blog about that in a later blog.

What I found most interesting about how C++ does math problems is that if you do not put a ".0" then it will not return any number after the decimal point. It will just return an integer. You need a .0 to get any kind of floating point. A floating point is a number with a fractional part. More on floats and integers later.

In the last example I am adding and multiplying the same numbers but in different orders. Just remember these order of operations

multiplication, division and modulus have equal precedence. They are evaluated first regardless of their position. In other words, they are evaluated before addition and subtraction.

To get around this you can use parentheses. Example (5 + 5) * 5 equals 50 because you first add 5 and 5. Then multiply it. Without the parenthesis you would multiply 5 and 5 and then add 5 which would be 30.

That does it for today.

Recap of what was learned:
  • \n creates a new line
  • Type the arithmetic problem out without quotes
  • + - * / % are the arithmetic operators you use to get an expression
  • Expression is something that evaluates to a single value.
  • To get a floating point expression you need to add .o to the number
  • Order of operations. * / % expresses before + -
  • Use parenthesis to get lower precedence operators to express a value first. Keep this in mind because it will become very important later on when learning more about C++
To the few netizen travelers and all the little bots that live in the void that is my blog, see you next time.

Same Rat-Time.
Same Rat-Channel

 

1 comment:

  1. I like the new look of the place, ratling. I find having a blog home to decorate with one's inner workings to be very helpful, no matter what you feel like talking about. All this math is way beyond me, of course, but I see that it's a great river to float your own boat, and even productive in the real world. Keep at it, skav and best of luck to you.

    ReplyDelete