Showing posts with label Fundamentals. Show all posts
Showing posts with label Fundamentals. Show all posts

C++ Operators

 

   :) Hello Reader..  Good  Day!/Morning!/Evening! to you. (According to the time you are reading this post). You are welcome to "The inspirational blog for programming", inspire++. Today we are going to make some cool , amazing & simple programs and spend some good time together. :) So.. Come with me for the next 20 minutes.

     We know how to create variables. Can you remember? Yes it is really simple. All you have to do is just three things.


  1. Decide what type of variable we want. (ex: It may be int , float , string , char or boolean)
  2. Leave a Space & Decide a name for our variable. (ex: name , age , height , etc)
  3. Finally, Assign a value to it. 
ex: int age;
     age = 25;
     Here first we decided the type of the variable ( int ), secondly we gave it a name ( age ), and finally we assigned a value to it. ( 25 ).

     Though we did this in 2 lines we could do this in a single line too.
ex: int age=25;     float height = 6.1; string name = "shehan"; 

     Now if we want to print those values , we can do that by using   cout   function,
ex: cout<< "My name is "<< name<<" and I'm "<<height<<" ft tall "<<endl;

     Ok. As you can see in above example , you can assign values to your variables. But I feel it is not enough. Don't you feel the same? If the only thing we can do with variables is just assigning values and that's all? I'm not happy with that..!! We want to do things with variables. Right?!

     So is it possible? If so.., then what  else we can do with variables? How can we do that?.. Well! that's where we need operators...

     " C++ Operators " are the bad guys in the language, who can get things done. They are really cool. They can easily manipulate any variable or values to do different operations.

     C++ has different types of operators to do different different things to our variables. 



  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators


     Let's think we want to add the values of  two variables. We can use + addition , which is an Arithmetic operator.

ex: int a=5; int b=10; int c;
       c=a+b;
       cout<<c<<endl;
     There are specific operator for each Arithmetic operation in C++.

  • +     -     Addition.
  • -      -     Substraction.
  • *     -     Multiplication.
  • /      -     Devision. 
  • %    -    Modulas operator
  • ++  -    Increment Operator
  • --     -    Decrement Operator
     Ok...   Now I want you to turn on your Code Blocks and Start to Code with me Some Programs..

Ok..!! 
Now... Let's do a cool exercise using operators. Now what I want you to do is create a simple calculator program. How to do that? Use the practice you have..

Here's what you need to do.

     In your program, you should take two integer numbers as inputs. You can do this using cin function. Then your program should return the addition, subtraction, multiplication, devision of those two numbers.

If you find that hard please follow this video.




Great.. :)  Now you know how to use arithmetic operators. In our next post we can discuss about other 4 types of operators. 

     Before I go, Here's a challenge for you, 
          create two integer variables in a program int a , b ; , Assign two different values to them,  now type a code to assign the value of  b to variable a and value of a to variable b, without using third variable.  If you can comment your answer down comments section. If  you couldn't I will reveal you the answer in next post. Until then bye bye. :)

C++ Data Types..

     

     Helllooo... Readers.... :) Welcome to inspire++. The blog that gives you sophisticated jokes about programming every week.!! ;)
     Hello people.. Did you guys do the homework I gave you in last post. Here I give you the answers.. Please check
 your answers with mine and try to find anything new to you.. 



     Right!! With the lesson from the last post we realized how to create variables. Today lets talk about types of variables which is more generally data types in c++. 
     Last time we learned how to store integer data in variables . Remember the int keyword.. There we talked about integer variables. There are several more data types in c++.Just  Like integers. 




2. Floating Point.

     So, what if we want to store a decimal number in a variable.. we can't store them in integer variables. So for that we have floating point variables. Floating point variables are used to store decimal point values. How to create them? Easy..!! Just like the way we create integers what we have to do is first, type keyword float, and then leave a space and then type a suitable name for your variable. For example float num1 ; . Don't forget the semicolon..!!!
     Right.. after that you can store any decimal value in it. num1 = 75.8262; . Easy!!  Try it your self.. 

3. Character Variables.
     Ok... after that we can talk about other data types. So.. instead of numbers what if we want to store any character value in a variable. There you have character variable. How to create them. Easy.. char space variable name. Ex:- char Letter;. Now you can store any character like A,B,C ... a,b,c... 1,2,3... But remember when you assign any character to your variable remember to use single quotes '.  Ex:-  Letter='A'; 

4. Boolean Variables.
     This data type might be a little bit confusing to you.. Specially , Why we want such data type. 
     In this Data type we store true and false values. Why we want to store true or false value.? Believe me there are so many times we want to use true or false as output in programming. So how to create this?? Type bool , and then a space and the name you want to give to your variable. now in simple letters you can assign either true OR false to your variable.  

5. Void.
     Void is a data type which is not used to create variables. Really!! Yes.. At this point I have to remind you that Data types are used not only to create variables, but also they are used to create arrays , functions etc. So this data type "void" is use in function defining. 

      So above 5 are known as the primitive data types. You can use them to create certain variables. 
     Now I'm going to tell about another important non primitive data type. string...

6. string
     Imaging you want to store a word , or a sentence as the data. What are the options u have.? you type string And then a space and a name for your string variable, finally semi colon. And you can store data in them by variable name assignment operator and the word or the sentence you want. Remember here when assigning data use double quotation marks " ".




     Ok.. now you know something about variables. Now what I want you to do is to make a program to store several data in suitable types of variables  and print them. This is what you have to do.

     Store these data about a student named  Lio ,who is 20 years old , height about 1.8288 , lives in colombo got Grade A for his Mathematics exam.
     Create suitable variables to store his name , age , city , height in meters city and grade for maths. And get them as the output.

     Do this Exercise and I will give you the answers in my next post. :)
     Bye.. C U Soon.. 
Start from the First post <<<

C++ Variables


     Helloo.. My favorite blog readers.. Welcome to the newest post from ispire++ blog. This post is the next post of our fundamental of programming post series . So don't get confused this with our new post series OOP.
     I gave you some homeworks in my last post. Did you do those. Well if you did here are the answers for them.

Last posts Homework answers ;)









So... last week I promised you to start to talk about c++ variables. Here lets start it. Variables are a kind of structure we use to hold data in a program. 
     We know that when we run a program, it runs on our RAM.(Random Access Memory) . In a program, when we want to store some data(Temporarily, Not on hard disk), We create variables. 
      For example, let say we want to store the age of a person. What we have to do is create a variable to hold that data age of a person. Age is measured by years, so it is we can consider as an integer value. So what exactly we want to do here is, create a variable to hold an integer data. And store our data (age) in it. 
How do we do this.
     We want an integer variable, so we type int first,
     Then we put a space, and then we type a suitable name for the variable, ie:- age.
Ok, and finally as always we type semicolon ; . Right! We now have officially created the first variable we ever created in c++. Weldone guys.. :) 



     
     Ok.. now after we created the variable, lets store something in it. In this example age. Let say the age of the person is 23. So what we do is , in a new line we type the name of our variable, age , and then we type equal sign(in c++ it is known as assignment operator) after that we type the value we want 23 and finally semicolon;. 
     Congratulations you have assigned a value to your variable. Now let's check the variable. In a new line let's start a code to print the value. Type, cout<<age; and build and run your program.




     See the output! It says 23 right!! That's the value we assigned to our variable. See how easy to create variables, assign values to them and use them!! 

     Now I want you to create 5 variables about 5 subjects of a student naming English, Programming, Drawing, Electrical, Fluid. And assign the marks values as follows 80, 83, 75, 80, 60. And then in your program write a code to take them as output. Do this as a homework I'll give you the answers in our next post. 
     Ok.. here I end up this post. Hoping to see you in my next post soon.. Bye..!! :)

Next post>>>

Lesson no : 2 - ( COUT function & Escape Sequence )



            Hello… People with Happy Faces.. Just right after finishing the Exams and having a Leisure time.. :)

            How you doing guys.. Having fun..!! I know.. Batch trips , Vacation etc,etc.. Well… me too.. :)

           We had to pause our C++ programming post series because I had semester exams for almost an entire month from 24th of May to 17th of June.. And You know what.. the exam was my B'day gift from KDU.. Yeah people.. It was on my Birthday.. 24th of May.. Yey.. Happy birth day for me..!! :)

           However I put some posts to discuss the model papers before the exam..

            So.. Now we have enough time to continue our amazing post series ;) Let’s start again..

             Last time we ended our post with explaining the cout in c++. 


Cout is use to display or print anything on console.


You can print 
  • any text 
  • any value of a variable 
  • result of any operations with variables 

            Remember the last time, we printed your name and your address..?? This time lets discuss some techniques we can use to print text using cout. ( By print I meant displaying it on the console. Not actually printing it using a printer!!)

             When you print texts or words using cout, you can’t print some characters as output.

  • “ - double quotes 
  • ‘ – single quotes
  • \ - backward slash
  • ? – question marks

For example

Cout<<”Don’t come late. Did you miss the bus today? ”;

            If you try to print this you will probably end up in a error. Reason is that you have ? question mark and ‘ single quote to print.

            Now you have a problem , how to print “” or ? ‘ these.. Right..? Well, you have a way to do this. It is called "escape sequence" .

You can type any of those symbols right after a backward slash, then there will be no error.

Ex:-
cout<<” Don\’t come Late. Did you miss the bus today\? ”;

Cout<<” \”Here Im Using Double quotes with in double quotes.. ;)\” ”;



Here's a list of Escape Sequence.



Try this list in your code.

Specially this \t – tab and \b-backspace part.

Ok.. I will Stop this little post from here by leaving you some exercises. Please do these and I promise you to discuss them in my next post..



Ex 01: 

                **
               ****
              ******
             ********
            **********
           ************

Write a program to print above triangle.



Ex 02:



Try to print letters like this in the terminal.



P.S


     My Fellow readers. Thanks for staying with the blog. Now You can follow my blog by subscribing, using your email address. You can see it on top of the right side-bar. And please don’t be hesitate to ask any question about anything. See  you in my next post… Let's talk about variables in next post.. 

Next Post >>>

The first Program in C++. Using Code Blocks


Hello Again my Fellow Readers..!!
Hope you have installed Code Blocks and waiting to start your first C++ program and looking for me.. ;)
Sorry for the delay..! Let’s start quickly..
So if you opened a new project and opened it’s main.cpp file , now you can see the first program has already created…
Really..!!
Sure..  What you can actually see is bunch of lines of codes of a simple program. This line of code is known as the source code of your program. Means this is the set of instructions you gave to the program.

So what happened in this program.. Let’s check it out.. In the top tool bar you will see an icon with a cog wheel saying build.. You can build your program using it.. Click on it..
 (It will compile your program.. )  Then you see a green color arrow head icon next to build icon.. (Run) Click it and boom!! There is your first program.. It will open the command prompt and run your program in it.

This program will display the message “Hello World” in cmd. Press Enter and then the program will terminated.
            So what happened here was the source code was translated into machine code by a compiler. That’s what you done by building the program. Then you run the program.
So a compiler is a translator between programming language  and machine language.
There is another translator called interpreter. Both compiler and interpreter do the same thing but in different ways. A compiler translates the entire program at once. Interpreter does that by translating the codes line by line , consuming more time but, easier to identify errors more accurately than compilers.

Come back to your code, Select the code, and press delete. Right.. Now do exactly what I say..
1.     Type   #include<iostream>
     This is a header file, Also known as a preprocessor.
2.     Next line Type   include namespace std;
    
This part is essential to output a message in cmd. Don’t forget the semi-colon ; at the end of the line.
3.     Then type   main()
4.     On the next line type   {
     Open curly brace.
5.     Then type   cout<<”Your Name”;
      replace the Your name with your name  ;) (Within quotation marks)  ex:-    “Shehan”. Don’t forget the semi colon again.
6.     Then type    return 0;
7.     And finally type       }

Done…   Now build and run the program.    
This will be the output.


Right, now I want you to delete   cout<<”Your name”;   and  type  cout<<”Your_Name”<<endl;  and  cout<<”Your_Address”<<endl; in two different lines.
Build it.. and run it.. And see the result.


       I think you got the idea that cout<<” ” is use to display an outputs in your program.   
       endl; - is refer as end of line, which creates a new line, after that.

       So…!   Use cout, to display different outputs.
 Try to use it creatively.
See You in m next Post..!!!   Have fun..    R.S.
Next Post>>

Let’s Start Coding ( Introducing Code Blocks )

Let’s Start Coding.



Hello Guys..!! Welcome to engineers favorite programming blog.. ;)
Last week we started our little chat about programming…  Computers, codes, Instructions so and so..
            And remember the simple game which I introduced to you.. I hope you have enjoyed your first approach to the world of programming.. :)
            Last week We came up to a point..
A computer program is a list of instructions that tells a computer what to do.. Everything a computer does is done by using  computer programs. A computer program is written in a programming language.”
Starting from that point,
            A program is a written thing, in a computer (list of instructions). To write something we need a language.
            OK..!!  We know some languages.. right..! we can use them to write a program.. (English or sinhala or tamil ) easy..!!
            But, unfortunately computers can-not understand human languages.
Computers, use 
very complicated language to operate which is really hard for us (human) to use. ( :D You know computers, they are geniuses) (ex : Machine Language -Binary)

            So in order to talk to a computer , and to make things done by him, we need an intermediate language. And create a program (List of instructions) using that language. Those kind of languages are known as programming languages.

 “FORTRAN” was the first programming language, created in 1953..

There are thousands of programming languages in the world.
·         Java.
·         PHP. ...
·         JavaScript. ...
·         Python. ...
·         Objective-C. ...
·         Ruby. ...
·         Perl. ...
·         C, C++ and C

So what you have to do is pick a language and learn to code using it.

            As our language, I Chose C++ to begin our journey..
C++ was a creation of Mr. Bjarne Stroustrup in 1979 from Bell Labs. See that bald guy here, that's him.


As the name stands for C++ is an enhancement to the C language 1983.

            We can make c++ programs by just using a compiler and note pad, Save the written program in “.cpp” extension and compile it and run it in command prompt. Or by using the terminal in Linux or Ubuntu You can do the same process..,
            But, That is a lot to do..
            To make the process easier, We can use an IDE (integrated development environment).
You might have heard about it already CODE BLOCKS. A Famous cross platform IDE. You can download it free from here http://www.codeblocks.org
Just download it, Run the setup and and you are ready to start programming.


            When you first opened the Code Blocks here's what you have to do,
1.    Open the Code Blocks.

2.    Select new Project.

3.    Select Console Program

4.    Select C++

5.    Give a name for Your Project.

6.    And Finish Wizard.

Then you will end up in this interface.


On the left side of the screen, there is a panel choose projects. And There will be your project in a tree view. Click on it (+ Sign).

Then You will see a file called main.cpp, Double click on it.


On the code screen a Sample program (Hello World) will appear, You can start program by changing it as you wish..  :) This is really Easy..!!
Try it now.. 
I will see you in my next post to tell you how to make your First C++ Program...!

Let’s start at the very beginning.


When you think about the word “Programming” what comes to your mind.??..  A computer , Bunch of codes , a geeky thing which makes you  headache..
Yeah.. I know right.. ;)  But wait….!



What if I tell you that programming is not as hard as you think , or better what if it is the most easiest and fun thing to learn among your engineering subjects… ( You kidding me.. right..!!)  No…
Let me explain….

Think about your alarm clock for a minute.. At night you set your alarm (for a specific time you want) and go to bed, on next morning the alarm rings on time, you hear it , you snooze it and you sleep again… ;) right..
In that scenario you gave instructions to the alarm clock by setting the alarm and the clock worked according to your instructions..

The concept behind computer programming is same..  You give instructions to the computer and computer works according to those instructions and that set of instruction is called a computer program..


A software is a collection of such programs made by computer programmers.
So simply.. “A computer program is a list of instructions that tell a computer what to do.. Everything a computer does is done by using a computer program. A computer program is written in a programming language.”
Easy.. right..!! :) 
See.. That’s what I told you… I hope you’ve already started to feel it… You like it..? Follow me.. There is a lot to tell you.. 
See you in next post.. Bye..!


P.S

Hey there.. Here’s a bonus for you.. I’m hoping to tell you about variables, control structure, looping, arrays, functions in future…

  Those words might be new to you.. But eventually you will realize how easy those concepts are.. And I’m going to tell you an extra easier and fun way to understand those concepts..

This is a simple puzzle game called light-bot.


Lightbot is great fun and does a surprisingly thorough job of introducing some pretty complex principles of programming.

You don’t want any programming knowledge to play this game, But at the end of the game you will end up having a good understanding about all the basic concepts of programming. And you will receive a certificate like this


for completing a basic programming course .

Hope you will enjoy.. See you soon.. Happy weekend..!! 
Next Post>>>

Life of a Systems Engineer

  Hello, my dear readers. ✌😀✋ I'm talking to you after a very long time. And I am thrilled to talk-to and hear-from you after all thi...