Input/Output stream in C++.

The C++ programming language does not define any statement to perform input and output operations. Instead, C++ has a separate input/output library that is part of the C++ standard library that provides primary input/output and many other facilities.


For the purpose of input and output, you will use the iostream (input-output stream) library which is a part of the C++ standard library. You need to include iostream header file on top of any C++ code file to use the properties define in this library. 

#include<iostream> //header file

int main()
{
   //include the rest of your code here
}


Note: A stream is a sequence of characters read from or written to an Input/Output device. 

  • When the direction of the flow of characters is from the device to the main memory then this process is called input.
  • When the direction of the flow of characters is from the main memory to the device then this process is called output


There are basically four standard objects present inside the iostream library and these are:

Objects Uses
std::cin (pronounced see-in) Standard input
std::cout (pronounced see-out) Standard output
std::cerr (pronounced see-err) Standard error
std::clog (pronounced see-log) General information

std::cout
cout is an object of the standard ostream (output stream) class that is defined in the iostream library. It is used to print the output on the display screen (console). The insertion operator (<<) to insert in the output stream to display the text to the console.
#include<iostream> //header file

int main()
{
    std::cout<<"Hello AlgoLesson"; //printing Hello AlgoLesson on console
    std::cout<<4;  //printing 4
return 0; }
Output:
Hello AlgoLesson4

std::cin
cin is an object of the standard istream (input stream) class that is defined in the iostream library. It is used to take input from the user using an input device like a keyboard. The extraction operator (>>) extracts the data from the cin object which is entered by the user. 
#include<iostream> //header file

int main()
{
    int x; //variable to hold input

    std::cout<<"Enter the value of x: ";
    std::cin>> x; //getting input from keyboard and store in x

    std::cout<<"Value of x: " << x ;
    return 0;
}
Output:
Enter the value of x: 7
Value of x: 7

std::cerr
cerr is a standard error stream that is part of the iostream class and is used to print the error messages. This is used when a person wants to show an error message immediately. 
#include<iostream> //header file

int main()
{
    std::cerr <<"This is an error message";
    return 0; 
}
Output:
This is an error message

std::clog
clog is a standard buffered error stream that is used to store general information or error messages. It is also part of the iostream library and is used to display error messages on the display screen.
#include<iostream> //header file

int main()
{
    std::clog <<"This is an error message log";
    return 0; 
}
Output:
This is an error message log

std::endl
endl stands for "end line" which is used to tell the console to move the cursor to the next line so we can print the output in a new separate line. You can also use '\n' which is a newline character instead of using endl.
#include<iostream> //header file

int main()
{
    std::cout <<"Welcome to AlgoLesson" << std::endl;
    std::cout <<"You are learning C++"; 
    return 0; 
}
Output:
Welcome to AlgoLesson
You are learning C++

Comments in C++ Programming.

Comments are the text that is added in C++ code to make it more readable and easy to understand. It is basically added for future reference so that whoever is going to work on that code will get additional information from comments that help them to understand the code. The compiler treats comments as white space and completely ignores them.


Two types of comments are available in the C++ programming language, single-line comments, and multi-line comments.


Single Line Comment.

Single-line comments in C++ start with two forward slashes (//) and any text written after this till the end of the line is considered a single comment.  

Example of single-line comment in C++:

//This is a single-line comment

//initializing an integer variable with value 5
int a = 5;

int a = 10; //updating value of a


Multi-line Comment.
The multi-line comment starts in C++ start with /* and ends with */ and any text between them are considered white space and ignored by the compiler. If you start a multi-line comment by using /* but forget to close it then everything after /* will get commented on.

Example of Multi-line comment in C++:
/*Below code will print the message "Hello World on the 
console using cout function which stands for character function.
*/
std::cout<<"Hello World";

Single-line comments are mostly used for writing short comments and Multi-line comments are used for writing longer comments.

Introduction to C++ Programming.

Introduction to C++ Programming.

The C++ program was developed by Bjarne Stroustrup in 1979 at Bell Lab which is basically an extended version of the C program. After the release of C++ in 1998 by ISO five major updates have been made in the language (C++ 03, C++11, C++14, C++17, C++20), and each time more new functionality has been added.


The initial version of C++ was called "C with Classes" having extra features like data abstraction, object-oriented programming, and generic programming. 


Usage of C++ Programming Language.

  • It is used for making Video Games.
  • It is used for making High-Performance Financial Applications.
  • It is used for making Embedded Software.
  • It is used in Artificial Intelligence and Neural Networks.
  • It is used for Developing a Web Browser.
  • It is used for building Operating Systems.


C++ is a high-level programming language and a single statement of C++ may compile into many machine language instructions. Let's understand the basic syntax of C++ programming in detail by using below simple example. 

#include<iostream>

int main()
{
    std::cout<<"Hello AlgoLesson";
    return 0;
}
Output:
Hello AlgoLesson

Let's understand each line of the above code one by one:

Line 1: It is a Preprocessor directive telling that you have included iostream header file which is part of the C++ standard library. The iostream library allows us to read and write from or to the console.

Line 2: It is a blank space that will be ignored by the compiler. It is not necessary to give this blank space but it helps read the code more efficiently. 

Line 3:  In this line, you are defining the main function and every C++ program must have this special main function. Whenever you run the program, all the statements inside the main function will execute in sequence order and the program gets terminated when the last statement of the main function has been executed.

Line 4 and Line 7: These two lines indicate the opening and closing brackets that define the function body and tell the compiler which are lines that belong to the main function.

Line 5: This statement helps you to print your output on the console (cout stands for character output) and the << operator is used to send the output character to the console. In our case, we are sending "Hello AlgoLesson" to the console.

Line 6: It is a return statement which is the last statement of the program that sends a value back to the operating system in order to indicate whether the program has been successfully executed or not. 

What is Syntax Error in Programming?

The syntax is basically specific rules that you need to follow for writing code and different programming languages have their own syntax. You need to write each statement by following these rules in order to write a good program. 

For example, all statements in C++ programming must end with a semicolon. If you make any mistake in the syntax of the program then while running the code your compiler will send you a syntax error with a short message describing the possible cause of the error. 

Syntax Error in C++ Programming
C++ Syntax Error when you miss a semicolon

Two-Dimensional Array in C++.

You can divide Array Data Structure into two categories, one-dimensional array, and multi-dimensional array. In this article, I am going to discuss the simplest multi-dimensional array which is a Two-Dimensional Array. 

What is a Two-Dimensional Array?

A two-Dimensional Array is the simplest form of a Multi-Dimensional Array which you can consider as an array of arrays and visualize in the form of a table with rows and columns to store data. A two-Dimensional Array is also called Matrix having m number of rows and n number of columns and you can store m x n data values of the same type.

Below is an example of a Two-Dimensional Array:
Two Dimensional Array in C++

How To Declare a Two-Dimensional Array?

To declare a 2D array in C++ programming language, you first need to write the data type of the array followed by the array name, and as it is 2D-array so you will need two subscripts (square brackets [ ][ ]) to define the size of the array. 

syntax: data_type name_of_array[size1][size2]...[sizeN];

For example:
int arr[4][5]     //Two Dimensional Array
size of arr[4][5] = 4x5 = 20 elements we can store in this array

int arr[4][5][6]  //Three Dimensional Array
size of arr[4][5][6] = 4x5x6 = 120 elements we can store in this array


How To Initialize a Two-Dimensional Array?

You can initialize a 2D array just like a 1D array but here you need to take care that the position of the data value should now exceed the number of rows and columns. Let's understand this with one example:

Example of 2D Array: int arr[4][5];

In the above example, the value 4 in the first bracket is defining the number of arrays present and the second value 5 in the second bracket defines the number of elements you can store in each array.

int arr[4][5] = {{3, 4, 6, 9, 5},
                 {1, 2, 3, 5, 8},
                 {7, 0, 1, 3, 5},
                 {2, 4, 9, 0, 3}};

How To Traverse Two-Dimensional Array?

Traversing is a process of visiting each element of the given array at least one time and to perform this operation on Two Dimensional Array you need some basic information like the number of rows and columns. You have to run two nested loops in which the first loop is m times which is the number of rows and the second inner loop will run n times which is the number of columns. 

C++ program to Traverse 2D Array:
//C++ program to traverse 2D Array 
#include<iostream>
using namespace std;

int main(){
    int arr[4][5] = {{2, 3, 5, 6, 8},
                     {1, 2, 4, 8, 3},
                     {0, 4, 2, 9, 1},
                     {5, 4, 3, 2, 1}};

    cout<<"Displaying 2D Array: "<<endl;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 5; j++){
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
Output:
Displaying 2D Array: 
2 3 5 6 8
1 2 4 8 3
0 4 2 9 1
5 4 3 2 1

There are several coding problems that you can solve based on 2D Array and I am sharing few of them below:

Next:

One-Dimensional Array in C++.

One-Dimensional Array in C++.

Array Data Structure is a Linear Data Structure that can store elements of similar data types in a contiguous memory location. When you want to use an Array data structure to store your data values, you need to declare the size of the array in advance. 

There are two types of Array data structures that is One-Dimensional and Multi-Dimensional Data Structures. In this article, I will discuss One-Dimensional data structure in detail.

What is a One-Dimensional Array?

In a One-Dimensional Array, elements are stored under a single variable name in a linear manner one after another in a continuous memory location. It allows random access to any element by using the index value of that particular element.

A one-Dimensional Array is the simplest form of Array Data Structure in which data manipulation and access are much easier compared to other data structures. It is also used for creating other data structures like stacks, queues, trees, and graphs. 

How To Declare a One-Dimensional Array?

To define a 1D array in C++ programming, you first have to write the data type of the array followed by the array name and inside subscript (square bracket) you have to define the size of the array. 

Key points to note about array declaration
  • If you have created integer-type data then you can store only integer-type values.
  • The size of an array must be known in advance and you cannot change this at run time. 
  • You can only use positive integer values to define the size of the array and floating or negative values are not allowed.

How To Initialize One-Dimensional Array?

There are multiple ways to initialize a 1D array in C++ and here I will discuss a few of them one by one.

1. Declared the data type and size of the array and you can initialize the array element inside curly brackets. The number of Elements in the brackets must be less or equal to the size of the array.

int arr[5] = {3, 4, 6, 9, 2};


2. No need to declare the array size in advance you can directly initialize the array elements inside curly brackets. The size of the array is going to depend upon the number of elements present in the brackets.

int arr[] = {23, 5, 6, 12, 1};


3. Declare the array with the number of elements you want to store in that array and then use the index value to initialize the element one by one.

int arr[5];

arr[0] = 13;
arr[1] = 5;
arr[2] = 6;
arr[3] = 7;
arr[4] = 12;


4. First declare the array with the number of elements you want to store, and then you run a for-loop and initialize the array by taking input value from the user.

int arr[6];

for(int i = 0; i < 6; i++){
   cin >> arr[i];
}


(getButton) #text=(How To Pass Array to Function in C++) #icon=(link) #color=(#2339bd)

How To Traverse One-Dimensional Array?

Traversing is a process of visiting each element of the array at least once. To perform this operation we simply use a for loop that is going to run n times where n is the number of elements present in the array.


C++ Program to Traverse One-Dimensional Array

#include<iostream>
using namespace std;

int main(){
    //Initialising 1D Array
    int arr[] = {31, 5, 9, 11, 2, 20};
    //No. of elements present in the array
    int n = sizeof(arr)/sizeof(arr[0]);

    //Traverse array elememts
    for(int i = 0; i < n; i++){
        cout<<arr[i]<<" ";
    }
}

Output:

31 5 9 11 2 20

You can use the sizeof() operator to find the number of elements present in an Array. Below is the syntax for calculating this. 

int num = sizeof(name_of_array)/sizeof(name_of_array[0])


Next:

How To Delete Your Google Account Permanently?

If you use multiple Google Accounts then there might be a chance that you have a few Google Accounts that you are not using anymore but still, exists. Google gives you the option to delete your Account and d permanently and if you want you can also save your existing data stored in different services of Google like Gmail, Google Drive, YouTube, Google Maps, etc.

Follow the steps given below to Delete your Google Account:

  • Open your Google Account in your browser. 
  • Click on Data & Privacy option from the left panel and scroll down on that page to move to the More Options section.
Google Account
  • You see the option to Delete your Google Account, click on it and it will redirect you to the next page. 
  • Google will give you the option to Download all your data related to that Google Account. Click on Download your data to choose that option.
Option to Delete Google Account


  • Scroll completely down to acknowledge and check both the check-box and finally click on Delete Account.
Deleting Google Account Forever
  • You will see one last message from Google saying "Your Google Account and all its data have been deleted". You will also get one last chance to recover your account if you change your mind at the last moment. 
Message after Deleting Google Account

So this is the complete process of deleting your Google Account permanently forever. But make sure to check your Gmail Account and Google Drive first before deleting the Account for downloading all required data. 

Programming Languages Introduction.

A computer program is basically an application or software that we use on our device and it contains a set of instructions given to our computer to perform tasks by following our instructions. The process of writing instructions for the application using any programming language is known as Programming which is done by a Software Engineer or Programmer.


The physical computer is made by combing several hardware components and when the programmer loads the program into the memory and computer starts executing the program sequentially and the application starts performing tasks based upon the instruction given in the program this process is known as the execution of the program.  


What is Machine Language?

Don't get surprised if I tell you now that a computer cannot understand C++ Programming language or any other programming language. Now the first question that will come to your mind is that then what is the use of learning a programming language. 


Any computer can understand only Machine language that is formed by using only binary digit that is 0 and 1. Back in time when the computer was newly invented then programmers needed to write all computer instructions using this machine code and each instruction was composed of 0s and 1s. 


It was a very difficult and time-consuming task to create a program for the computer using machine code and a program written for one computer was not capable of running on another computer because the CPU had different instruction sets.


What is Assembly Language?

Reading, writing, and understanding Machine code was very hard for a normal human being so programmers come up with a better alternative known as Assembly Languages that are readable by humans. In Assembly Language, the instructions are written using simple text and short abbreviations that make it easier to read and write than in Machine language.


However, the computer still cannot understand Assembly Languages so to make the computer understand our instructions we need something that can translate our Assembly code into Machine code and that is done by Assembler.


Assembly Language is a low-level language in which even for doing a simple task we need to write a lot of instructions and a program written for one CPU is not compatible to run on any other CPU having a different instruction set.


What are High-Level Languages?

Programming Languages such as C, C++, Java, and JavaScript are known as High-Level Languages. The program written using these languages are easier to understand and they are portable in nature which means that programmers don't have to worry about the system on which the program is going to run. 


However, we need to translate the high-level language into a language the computer can understand and this is done by using a compiler and interpreter.

A compiler is a program that read and translate source code into machine code. It produce a stand-alone code executable code that can run. (alert-success)

An interpreter is used to directly execute the program without compiling the source code. Interpreter are flexible but less efficient because it is needed every time you run the program. (alert-success)

Programming Languages like C and C++ need compilers to run their code whereas scripting languages like JavaScript need interpreters to run their code and high-level language like Java need both. Programs are compiled to run on a different system you don't need to change your code written in any programming language to run on a different system 

How To Create a Invisible Folder on Windows Desktop?

Creating a Folder on Windows PC with just one right click is a quick and easy task. You have created many folders on your Windows PC to store your files and information. Now it might be possible that you have to share your desktop with your friends or family for some work and they can get easy access to your folders and information but what if the folders that you are creating are not going visible to them. 

You can create invisible folders on your Windows system to store your important information by following the below steps. 

1. Right-click on your desktop and select New > Folder to create a folder. 
Invisible Folder on Windows

2. Right-click on the newly created folder and select Rename option. From your keyboard press Alt + 255 to rename the folder with the invisible character symbol in the text. You can see a folder with a blank name is created on your system. 
Create a Invisible Folder on Windows
Note: If Alt + 255 does not work for your PC then try fn + Alt + 255 for renaming the folder. (alert-success)
3. Right-click on the folder and select Properties > Customize > Change icons...
Create a Invisible Folder on Windows

4. You can see many icons available to choose from but you need to choose one blank (Invisible) icon and then click on Apply
Invisible Folder on Windows

Refresh the Page once and you will find that your Invisible folder is now created and you can use that folder just like any normal Windows Folder to store your data and information. 

How To Make Folder Visible Again?

To make the Invisible Folder folder visible again, right-click on the folder and select Properties > Customize > Change icons... Now replace the icon back with the regular Folder icon and click Apply.

To rename the folder, right-click on the folder and select Rename then give any name you want to give to your folder and press Enter.

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson