C++ Learning Programming
Sunday, 27 July 2014
Jamaican Translator
Check out this cool Jamanica translator translators at http://translator.bizjm.com/
Saturday, 22 September 2012
C++ Relational and Equality Operators
Today will be talking about relational and equality operators, how they work.
Okay so let's get started. first will will run throught the different relational and equality operators.
So relational and equality operators are symbol that are use to do comparisons and when comparison are done it evaluates as a true or a false for example 1<2 will be true because indeed 1 is less than 2. well that how it works basically not hard really. so here are some more
Here is the output:
Please note that your calculation can be complex as you want for example (((4-1)+(5*6)) < (2*4)) this works. you can also compare things that have been compared as well example ((4<10) == (4<4)) this works as well. So there is endless possibilities when using relational operators
So no that you guys understand how these operators work we are going to use variables with these operators now to see how we use them in the real world
And the output is shown below:
Pretty simple right? an this is the end of my post see you on at the next post and please remember to keep your mind active playing games here are some:
Okay so let's get started. first will will run throught the different relational and equality operators.
So relational and equality operators are symbol that are use to do comparisons and when comparison are done it evaluates as a true or a false for example 1<2 will be true because indeed 1 is less than 2. well that how it works basically not hard really. so here are some more
- == operator means equivalent or equal to
- (1==1) means that 1 is the equivalent of 1 or what I would say 1 is equal to 1 and this is true
- (1==2) this will return false because 1 is not equal to 2
- (7==6) false as well
- < operator means less than
- (2 < 4) 2 is less than 4 so this is true
- (5 < 4) this is false because 5 is not less than 4
- (10 < 10) this is also false because 10 not less than 10
- > operator means greater than
- (2 > 1) is true because 2 is greater than 1
- (1 > 2) is false because 1 in not greater than 2
- (5 > 5) is also false because 5 is not greater than 5
- != means not equal to or not equivalent to
- (1 != 2) is true because 1 is not equal to 2
- (1 != 1) this is false because 1 is equal to 1
- (3 != 4) is also true
Here are some more complex ones
- ((2 *2) < 2) is false because 2 x 2 = 4 and 4 is not less than 2
- ((2*2) == (2*2)) this is true because 2 x 2 = 4 and 4 is indeed equal to 4
- (2 > (1*1)) this is true because 2 is greater than 1 (1 x 1 = 1)
- (10%2 != 0) this is false because 10 mod 2 give a remainder of 0 and 0 is equal to 0
Okay I think I have bored you enough so lets use the complier now to do it.
Please note that 1 is true and 0 is false in the complier.
#include<iostream> using namespace std; int main () { //a simple example of how it work //this display 1 if true or 0 if false cout << (2==2)<<endl; // this is true cout << (1==2)<<endl<<endl<<endl; // this is false //some true examples cout << "(2 < 4) = " <<(2<4)<<endl; cout << "(6 > 5) = " <<(6>5)<<endl; cout << "(4 == 4) = "<<(4==4)<<endl; cout << "(2 != 5) = " <<(2!=5)<<endl<<endl; //some false example cout << "(5 < 4) = " <<(5<4)<<endl; cout << "(4 > 5) = " <<(4>5)<<endl; cout << "(3 == 4) = "<<(3==4)<<endl; cout << "(5 != 5) = " <<(5!=5)<<endl<<endl; //some more complex examples cout << "((4 * 3) < 4) = " <<((4*3)<4)<<endl; cout << "((9 - 1) > (4+2)) = " <<((9-1)>(4+2))<<endl; cout << "((10 % 2) == 0) = "<<((10%2)==0)<<endl; cout << "((2 / 2) != 5) = " <<((2/2)!=5)<<endl; system("pause>nul"); }
Please note that your calculation can be complex as you want for example (((4-1)+(5*6)) < (2*4)) this works. you can also compare things that have been compared as well example ((4<10) == (4<4)) this works as well. So there is endless possibilities when using relational operators
So no that you guys understand how these operators work we are going to use variables with these operators now to see how we use them in the real world
#include<iostream> using namespace std; int main () { //declaring varibles int num1 = 0; int num2 = 0; //simple example cout << (num1 < num2)<<endl; cout << (num1 < 30)<<endl<<endl; //assign new varible to num1 and num2 num1 = 3; num2 = 6; cout <<"("<<num1<<" < "<<num2<<") = "<<(num1<num2)<<endl;//num1 is 3 and //num2 is 6 so 3 < 6 is true cout <<"("<<num2<<" > "<<num1<<") = "<<(num2>num1)<<endl;//this is true num1 = 31; cout <<"("<<num1<<" == "<<31<<") = "<<(num1==31)<<endl;//this is true cout <<"("<<num1<<" != ("<<num2<<"*4)) = "<<(num1!=(num2*4))<<endl;//this is true system("pause>nul"); }
Pretty simple right? an this is the end of my post see you on at the next post and please remember to keep your mind active playing games here are some:
Wednesday, 19 September 2012
Postings
Hello everyone having been posting for a will I don't really see the signification, of my blog so i kind stop production useless you want me to continue all i need is just one one person so say this blog is helping them and I will continue when i get free time from school classes
Thursday, 23 August 2012
C++ Arithmetic Operators
In C++ Arithmetic Operators are symbols that are use to carry out mathematical operation, these symbols include (+, -, %, /, *) and they are used for addition, subtraction, getting a remainder, divisions and multiplication.
That is basically they work I'm not sure if i can break it down any further than showing you the code. but i will show you example how modulo works or alternative to get modulo. and this is shown in the code below as I said modulo is just the remainder of from a division so that what we are going to do find the remain of a division it may seem useless just because i am not using variables.
The theory behind this is pretty simple think about it you have 5 and 2 you want the remainder you know it is 1 right so you probably would say hey the remainder is just the difference of the total that is the 5 minus how much times 2 go in 5 and then that times two to find out exact value that it stops at which is 4 because 2 goes in to five 2 time so 2*2 equal 4 and then 5-4 = 1 easy right?
Okay so now i show how to manipulate variables and operators like in line 17 in the above code.
Here is the senearo person want you to calculate the pay for a week for a person who work 40 hours for the week at a rate of dollars per hour in order to do this you need to multiple the hours work by the pay rate.
Hope you understand the above, okay so now same seniro but this time the person have sales that he made for the week the commission is 20% of all his sale that means for every thing he sells he get 20% of the price of the item he sold 29 items and the cost of a item is $10 okay so here we go
Now that we are finish with that I have to explain more on line 23 I this line notice I coded pay and then say pay is equal to pay + something what it means that if pay were 20 already and I wanted to add on more on i would have to assign pay to itself and then add on the extra and thats one way to do it the other way is shown below
operator alone I can use all of the to do it what this simply means is
I could similarly do this and get the same result and i can not just do it with this
Nothing different its just faster to do it that way but you have to get use to it first.
Please note a variable's value can be changed after declaring it unless of course you put the key const example
And if you have problem with the problem solving and critical thinking games can help I grew up of puzzle games and they have made me a good problem solver here are some games you can try out, im not sure if those work on other devices such as mobile or iPad but you can see anyway
These are just some of the great games they have out there the more you play pass them the more you can overcome problems you become a better thinker basically. One of my all time favorite puzzle game is armadillo run this game was worth buying you can check out the demo this one really will get you thinking.
In this lesson you will learn how to all of them and will we be using variables a lot in these examples but no in all of them to show you the many ways you can use them.
First we will use them without variables so I am going to print out result of using all the operators this is shown below i won't be comment on every line of code as before to speed up things.
#include<iostream> using namespace std; int main() { //addition cout <<"Add 5 and 2: " << 5 + 2<<endl; //subtraction cout <<"Minus 5 and 2: "<< 5 - 2<<endl; //division cout << "Divide 5 and 2: " << 5 / 2<<endl; //multiplication cout << "Multiple 5 and 2: " << 5 * 2<<endl; //modulo cout << "Modulo 5 and 2: remainder " << 5 % 2<<endl; system("pause>nul");//doesn't print press any key to continue... when it pause }
That is basically they work I'm not sure if i can break it down any further than showing you the code. but i will show you example how modulo works or alternative to get modulo. and this is shown in the code below as I said modulo is just the remainder of from a division so that what we are going to do find the remain of a division it may seem useless just because i am not using variables.
#include<iostream> using namespace std; int main() { //modulo cout << "normal modulo 5 and 2: remainder " << 5 % 2<<endl; //alternative modulo cout << "alernative mod 5 and 2: remainder " << 5 - (5/2)*2<<endl; //with varibles to make it easier to undestand //declare varibles int num = 5; int num1 = 2; cout <<"alernative with varibles mod 5 and 2: remainder " << num -(num/num1)*num1; system("pause>nul");//doesn't print press any key to continue... }
The theory behind this is pretty simple think about it you have 5 and 2 you want the remainder you know it is 1 right so you probably would say hey the remainder is just the difference of the total that is the 5 minus how much times 2 go in 5 and then that times two to find out exact value that it stops at which is 4 because 2 goes in to five 2 time so 2*2 equal 4 and then 5-4 = 1 easy right?
Okay so now i show how to manipulate variables and operators like in line 17 in the above code.
Here is the senearo person want you to calculate the pay for a week for a person who work 40 hours for the week at a rate of dollars per hour in order to do this you need to multiple the hours work by the pay rate.
#include<iostream> using namespace std; int main() { double hoursWorked = 40.5;//he worked 40 hours double payRatePerHour = 20.3;//pay rate is $20.5 a hour //we use double datatype because of decimals cout << "His Pay is $" << hoursWorked * payRatePerHour<<"\n"; //you could also do this //save pay in a new varible double pay = hoursWorked * payRatePerHour; //display varible cout << "His Pay is $" << pay<<endl; //you can even do this to show how much the person work //and the pay rate cout << "\nWorked: "<<hoursWorked<< "\nPay Rate: " << payRatePerHour << "\nPay: $" << pay<<endl; system("pause>nul");//doesn't print press any key to continue... }
Hope you understand the above, okay so now same seniro but this time the person have sales that he made for the week the commission is 20% of all his sale that means for every thing he sells he get 20% of the price of the item he sold 29 items and the cost of a item is $10 okay so here we go
#include<iostream> using namespace std; int main() { double hoursWorked = 40.5;//he worked 40 hours double payRatePerHour = 20.3;//pay rate is $20.5 a hour //we use double datatype because of decimals // and always use double when its money too double numberOfItemsSold = 29;//amount of items sold double priceOfItem = 10;//cost for the item cout << "His Pay is $" << hoursWorked * payRatePerHour + 0.20*(numberOfItemsSold*priceOfItem)<<"\n"; //you could also do this //save pay in a new varible double pay = hoursWorked * payRatePerHour; //so we have the pay without the item so we just add commission to it pay = pay + 0.20*(numberOfItemsSold*priceOfItem); //display varible cout << "His Pay is $" << pay<<endl; //you can even do this to show how much the person work //and the pay rate cout << "\nWorked: "<<hoursWorked<< "\nPay Rate: $" << payRatePerHour <<"\nCommission: $"<<0.20*(numberOfItemsSold*priceOfItem) << "\nPay: $" << pay<<endl; system("pause>nul");//doesn't print press any key to continue... }
pay += 0.20*(numberOfItemsSold*priceOfItem); //Or with different operator different result tho pay *= 0.20*(numberOfItemsSold*priceOfItem);
I could similarly do this and get the same result and i can not just do it with this
pay = pay + 0.20*(numberOfItemsSold*priceOfItem); //Or with different operator different result tho pay = pay * 0.20*(numberOfItemsSold*priceOfItem);
Please note a variable's value can be changed after declaring it unless of course you put the key const example
#include<iostream> using namespace std; int main() { int a = 10; cout << a <<endl; a = 2; cout << a <<endl; a = a*a;//a square //Or a *= a; cout << a; //you can't change if declared as a constant const int b = 100; //this mean its a read only type if you try //change it complier will give an error //example b = 50 is an error because it read only type cout << b; system("pause>nul"); }
These are just some of the great games they have out there the more you play pass them the more you can overcome problems you become a better thinker basically. One of my all time favorite puzzle game is armadillo run this game was worth buying you can check out the demo this one really will get you thinking.
Monday, 20 August 2012
Variable Datatypes in C++
Variable Names and Values
a = 20;//variable is called 'a' and the value of that variable is '20' number = 32;//variable is called 'number' and the value of that variable is '32' anything = 12;//variable is called 'anything' and the value of that variable is '12'
So the above example is a basic idea of what a variable is, So a variable has a name, a value and Datatype (N.B datatype in not in the above example) but we will soon get into that for now we are focusing on the name and value of the variable.
A variable can have any name that you choose as long as its not from a keyword of that programming language, for example in c++ we just learnt about the cout function we can not have a variable that looks like the below
cout = 12;//cout is a keyword so you can't use it as a variable name
If you do the above you will get a syntax error that mean something is wrong with your code basically as we go along you learn more about other errors
Something to note is that in C++ like in many programming language variable names are case sensitive or the Capital and common letters matter if a variable is called 'name' it's a totally different variable from 'Name' see example below
name = 1; Name = 2;
So if I decide to display the value in 'name' it will show 1, but if I decide to display the value in 'Name' it will show 2. This is what I mean when i say two totally different variables some example are below to show that if any letter is change to upper case(Capital letter) or lower case(common letters) it make the variables different
name = 1; Name = 2; nAme = 3; naMe = 4; namE = 5; NAME = 6; NaMe = 7;
All the above variables are all different hope you finally get the concept and in C++ our variable name SHOULD be lower case, good programming practice not because it will gives you errors (which it wont if its not lower case) but just the fact that you will find out everything will move smoother and faster when everything is uniform and it only looks professional.
So you next question is probably, can I have a space in my variable's name? The answer is simply NO what we do is use camel case, for example say the name of the variable say the variable name is 'date minutes' what we do is shown below
dateMinutes = 12;//value has no specific significant purpose in this example
Variables Datatype
Datatype this is basically the different classifications of the type of values a variable can hold. for example. int a; means that variable 'a' will and can only store integer values, what are integers? basically these are positive and negative whole numbers eg -1,0,1,2,3,4,5,6. look at the examples below for a better understanding and how datatype are setup.
#include<iostream>//iostream hearder file using namespace std;//get all library in the header file int main() { //int has a value that //ranges -2147483648 to 2147483647 //whole numbers only int a = 1; //double has a value that //ranges +/- 1.7e +/- 308 (~15 digits) //contains decimal double c = 2.333; //float has a value that //ranges +/- 3.4e +/- 38 (~7 digits) //contains decimal but less whole numbers //than double float b = 0.333333; //bool short for boolean can //only hold values true or false or 0 or 1 bool d = true; //char short for character can //hold only one character at a time //and char values are enclosed by single quotas char e = 'a'; //string is more of a class than a datatype //but what use string is to do is to store text //and string values are enclosed by double quotas string f = "Hello World"; cout << "a = " << a <<endl; cout << "b = " << b <<endl; cout << "c = " << c <<endl; cout << "d = " << d <<endl; cout << "e = " << e <<endl; cout << "f = " << f <<endl; system("pause"); }
Okay so I may have gone a head too far a beginner so i'm gonna try to fix it, So okay the = means your going to assign a value to a variable in the above code line 10 variable a is being declared, that mean your creating a variable in memory to hold some data in this instance that the datatype is int so it will store integer values and you will notice that a values is being assign to a that value is 1, now at line 38 this is where the variable a is send to be displayed, by using cout. A example of display a variable is just below to help clarify things.
#include<iostream>//iostream hearder file using namespace std;//get all library in the header file int main() { //declaration of a string //with value "HellWorld" string anyText = "Hello World"; //declaration of a string //with value "How are you?" string camelCaseText = "How are you?"; cout << anyText <<endl; //with no space between the two varibles cout << anyText << camelCaseText <<endl; //with space between the two varibles cout << anyText << " " << camelCaseText <<endl; //with endl between the two varibles cout << anyText << "\n" << camelCaseText <<endl; system("pause"); }
The code explains it self hope you learn something from my lesson.
Friday, 17 August 2012
Learning C++
What is C++?
A C++ program is a collection of commands, which tell the computer to do "something". This collection of commands is usually called C++ source code, source code or just code. Commands are either "functions" or "keywords". Keywords are a basic building block of the language, while functions are, in fact, usually written in terms of simpler functions you'll see this in our very first program, below. if you confused don't worry it will all be clear soon because thankfully, C++ provides a great many common functions and keywords that you can use and are easy to learn such if for and while its basically English in it's imperative form.
Getting started
If you chose to get Dev C++ when you install it it should look like the below picture.
After we have finish install the IDE its time to code. we'll be covering the basics for now these include:
1. Data types
2. Output and input in C++
3. While, do while, for loops
4. Arrays
5. Pointers
7. Functions
8. Classes
And will also take suggest on topic to go through as well
Getting started
To get you started you need a IDE in layman's term this is the thing that you going to use to run and edit your code. Now there are many IDE out there you may pick one of your choice but for now i will be using Dev c++ as my ID you may try Code::Blocks if you want. if you don't have already one i suggest you using Dev ++ click here to download. If you're on Linux, you can use g++, and if you're on Mac OS X, you can use XCode, and by the time were your finish you feel like a programmer before you know it.
If you chose to get Dev C++ when you install it it should look like the below picture.
![]() |
1. Data types
2. Output and input in C++
3. While, do while, for loops
4. Arrays
5. Pointers
7. Functions
8. Classes
And will also take suggest on topic to go through as well
Output in C++
For this tutorial we will be using cout as the output function in C++
So first we need to include our input out put library like this
Next we need to specify what exactly we will be using in thing library
or if you want to use everything in the library or libraries you could also use
So now you will learn a bit about functions that is the main function of you c++ program a function is a set of instructions that you can call any place the main function in executed when a program runs but enough of that just know that you will need it for these examples for now later we will talk more about functions
Sorry guys forgot to mention the importance of your semicolons ; this is what tell the complier that you have finished a statement of you miss out you semi colons your program simple wont run
This is you main function
Within the { and } your instructions will go in there.
So a simple program displaying Hello World would look something like
If you look at line 7 you notice cout this is the keyword we use when we want to display something from the istream library in c++ the << is telling the complier that there is something im going to be output next and "Hello World" is what is going to be output
if want run it go in dev C++ go to File > New > Source File
A blank page should pop up like below this is where you type you code
When your finish it should look like the this
You can run this by pressing F9 on your keyboard or going to Execute > Compile & Run on the menu bar
Now when you run it will notice a problem the Hello World and the Press any key yo continue... is joined and is on the same line
But we would prefer to see this
Well this can be easily achieved for us luckily there are 2 ways we can achieve this
1. is to use the endl this mean enter a new line and is use like shown below
and you cout also do this
</ br>
Or
2.
You could use the end line character '\n' as show below
and you cout also do this
</ br>
If you decided to do it the first way remember to specify that you will be using endl this is in the code below on line 4
Here is the code using \n instead
Here another thing you can do you can join text together with the << as shown below this is called concatenation
So first we need to include our input out put library like this
#include<iostream>
Next we need to specify what exactly we will be using in thing library
using std::cout;
using namespace std;
So now you will learn a bit about functions that is the main function of you c++ program a function is a set of instructions that you can call any place the main function in executed when a program runs but enough of that just know that you will need it for these examples for now later we will talk more about functions
Sorry guys forgot to mention the importance of your semicolons ; this is what tell the complier that you have finished a statement of you miss out you semi colons your program simple wont run
This is you main function
int main(){}
So a simple program displaying Hello World would look something like
#include<iostream>//input out put library using std::cout; // specfiy to use cout the library int main() // main function of you c++ { cout << "Hello World"; // output Hello World in console system("pause"); // pause program }
if want run it go in dev C++ go to File > New > Source File
A blank page should pop up like below this is where you type you code
When your finish it should look like the this
You can run this by pressing F9 on your keyboard or going to Execute > Compile & Run on the menu bar
Now when you run it will notice a problem the Hello World and the Press any key yo continue... is joined and is on the same line
1. is to use the endl this mean enter a new line and is use like shown below
cout << "Hello World "<<endl;
cout << "Hello World "; cout << endl;
cout << "Hello World \n";
cout << "Hello World "; cout << "\n";
If you decided to do it the first way remember to specify that you will be using endl this is in the code below on line 4
#include<iostream>//input out put library using std::cout; // specfiy to use cout the library using std::endl; // enter new line int main() // main function of you c++ { cout << "Hello World"<<endl; // output Hello World in console system("pause"); // pause program }
Here is the code using \n instead
#include<iostream>//input out put library #include<iostream>//input out put library using std::cout; // specfiy to use cout the library int main() // main function of you c++ { cout << "Hello World \n"; // output Hello World in console system("pause"); // pause program }
#include<iostream>//input out put library using std::cout; // specfiy to use cout the library int main() // main function of you c++ { cout << "Hello "<<"World \n"; // output Hello World in console system("pause"); // pause program }
Subscribe to:
Posts (Atom)