Introduction to Floats and Doubles: C++ Crash Course (Free Tutorial)

Have you ever needed to represent numbers with extreme precision in C++? Well, this is the article for you! In this tutorial, you will learn all about floats and doubles, which add decimal places onto the ends of integers. Thus, you can be more precise when trying to represent numbers.

If you are a beginner and want to learn to code from scratch, check out our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Let’s take a look at an example. If you feel like following along in real time, go on over to rextester.com. This site allows you to code in the main window, and test out your code instantly.

Start off by declaring an integer with a value of 5:

int i = 5;

In rextester.com, your main coding screen should look like this:

//g++ 4.9.3

#include <iostream>

int i = 5;
int main ()
{
std:cout << "Hello, world!\n";
}

In order to test our code, replace the "Hello, world!\n" with i. Scroll down to the bottom and press “Run It”.

You will see the number “5” appear at the bottom of the screen.

 

Now let’s see what happens when we try to add a decimal place onto our integer.

Change int i = 5; to int i = 5.5; and run the code.

As you can see, the console only prints out the decimal “5”. It ignores the decimal because integers are not designed to recognize the decimal place. This is where floats and doubles come in.

Below your integer, declare a float of the name j and a double of the name k:

float j;
double k; 

Give the float a value of 5.4, and the double a value of 5.3:

float j = 5.4;
double k = 5.3; 

This time, we want to output not just i, but j and k as well. To do so, add standards for the j and k. Note: add standard end alls to the end of each of the three lines in order to ensure that you start on a new line.

std:cout << i << std::endl; 
std:cout << j << std::endl; 
std:cout << k << std::endl;

As you can see by running the code, the double and the float both represent decimal places. They both recognize that there is one decimal place after the numbers.

Let’s see how many decimal places these two can recognize. As an example, change 5.4 to 5.444444444444 and 5.3 to 5.333333333333.

If you scroll down and run the code, you can see that the compiler only recognizes the code to 5 decimal places, and it’s the same for both floats and doubles. This is only characteristic of this compiler. In some other compilers, doubles can contain twice the number of decimal places as floats can. However, this is only necessary with extremely large data sets where you need extreme precision.

In everyday coding, the difference is pretty negligible.

Want to learn more? Sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes. Happy coding and see you soon!

Introduction to Strings: C++ Crash Course (Free Tutorial)

Welcome to one of the most important articles you’ll read as a beginner coder!  When it comes to coding, strings are a very important concept, no matter what programming language you’re using. Think of strings as a displayed message. This message can be virtually any sequence of characters from your keyboard, such as letters, numbers, and symbols.

If you are a beginner and want to learn to code from scratch, check out our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Let’s start off with a basic string. If you want to test out some coding in real time, hop over to rextester.com. This website will allow you to code in the main window, and test it out instantly. Type in the following:

std::string

Lets break down this piece of code. The std:: means that the program is drawing from the standard C++ library. You’ll have to use this syntax every time you declare a string. The string means that this particular piece of code is going to be a string.

At this point, we really haven’t done much. We should give the string a name:

std::string my_string

The string now has a name! Now, this still doesn’t actually do anything yet. The compiler will just know that a certain string, my_string, exists. It can now be referred to later in the code.

In order for this string to be really useful, we have to assign in a value. Let’s do that.

std::string my_string = "mammoth interactive";

Now, the string has the attached value of “mammoth interactive”. Any time my_string is printed, it will print the phrase “mammoth interactive”. Congratulations! You just declared your first string.

In rextester.com, the main coding screen should have something like this in it:

//g++ 4.9.3

#include <iostream>\

int main ()

{

std:cout << "Hello, world!\n";

In order to test our string, replace the "Hello, world!\n" with my_string. Scroll down to the bottom and press “Run It”.

You will see the text “mammoth interactive” come out.

Want to learn more? Sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes. Happy coding!

Black Girls Code officially funded!

A few months ago we were approached by Udemy to donate our Stencyl course to an organization called Black Girls Code. ” Black Girls Code was founded to bring computer programming education content to young girls of color (Black, Latina and Native American) ages 7-17 to become builders and leaders of technological innovation and their own futures”. We are happy to announce that the indiegogo campaign was completely funded. Our Stencyl course was the 500 dollar benchmark for pledging. We here at Mammoth Interactive love to see people learn. We were glad that we were able to make a difference and help out this organization.  You can see the link here.

http://www.indiegogo.com/projects/black-girls-code-summer-of-code-2013-the-remix

And you can visit their website here.

http://www.blackgirlscode.com/