Posts

Showing posts with the label learn programming

Stack and Queues

Image
What is Stack? The basis of stack is first in last out(FILO) Or last in first out (LIFO). To understand stack, consider example of plats. We place plats one over another. Let say we have 5 plats, named as p1, p2, p3, p4, and p5. Assume they are arranged in such a way that p2 is placed on p1, p3 is placed on p2, p4 on p3, and p5 on p4. As shown in the diagram.   Now you are clear with, what is stack. Now we will see where it is used and how we implement stack in c plus plus programming language. Why Stack? We see the functionality of forward and backward operations in our browsers. Similarly, we see functionalities like undo, redo in our text editor or any application like photo editors. The functionality of undo, redo is very common. It is used in many applications like in word, excel, photoshop, browsers, paint, and many other applications. Now if we think about what will be the best way to implement undo, redo functionality in our application, we will see the best data structure ...

C++ Programs Examples

  How to swap two variables in c++? We can swap two variables very easily in c++. There are two methods of swap. First method:    By using a temporary variable. Second method:    Without using temporary variable. Lets start with the first method. We use a third variable which is called temporary variable and represented as temp.  The code is very simple and is given below: #include<iostream> using namespace std; void main() { int a = 5, b = 10; cout << "a= " << a << "  " << "b= " << b << endl; int temp = a; a = b; b = temp; cout << "After swap:" << endl; cout << "a= " << a << "  " << "b= " << b << endl; system("pause"); }

C++ Programs Examples

 We can take input and output from user very easily. For input we use cin>>   and for output we use cout<< .  Assignment: Write a code/program that takes age of the user as input and print out  "You are age years old". For example if user enter his age 19 then your program should print "You are 19 years old". Dry Run: What is your age? 19 You are 19 years old.

Hello World Program

This is very simple program. You can write it easily. The exact code for this program is given below.  #include <iostream> using namespace std; void main() {   cout<<"Hello World!"<<endl;   system ("pause"); } Explanation: In the first line ,  #include <iostream>  it is c++ built in library. This library provides some functions that are very useful. You will understand it gradually, when you will study functions and classes in c++. As a beginner, it is enough for you to remember that it is a built in library and you have to include it in every code or program. The second line   using namespace std;  make it easy for you to use input stream cin>>  , output streams cout<<  , and other function like endl  easily. If you do not use it or if you want to skip it then you will have to write std::cout<< instead of only cout<<  , std::cin>> instead of cin>> and std::endl instead...

Data Types in C++

Data Type: Data type mean different types or form of data. Example: Integer, Float, Character, String etc are different data types. Their short form is used in programming to initialize any data member. Data Type                                                  Representation in C++ Integer                                                               int Float                                                                  float Character                              ...

Programming Fundamentals

  Logic building/ Problem Solving is the most important part of programming fundamentals. Here, I have some questions for you. Read them carefully and try to find their logic by yourself. If you cannot, don't worry answers will be given below each question. Let's start: Question no. 01: Suppose you have two containers; 1st container has black balls and red balls. Number of black and red balls in this container is unknown. There might be only black balls or only red balls or any number of black and any number of red balls in 1st container. 2nd container has unlimited red balls. Now the game is that you have to pick up two balls from the 1st container without seeing the balls, after picking out you can see them and follow the following instruction: i)  If both balls you picked up from 1st container are red, you have to place one red ball in the 1st container back. ii)  If both balls you picked up from 1st container are black, you have to place one red ball in the 1st contai...

C++ programming introduction

 My dear friend if you want to learn programming in C++, you are at a right place. I will teach you C++ programming. In this introduction, I would like to tell you some thing about myself. I am doing BS in Computer Science. I am super excited to teach you C++ programming. The first thing that you all need to know that programming is just fun. You do not need to worry about it. It is very simple and I will make it more simple for you. Hopefully, you guys will enjoy coding with me.