Blog

  • Print Count Down Timer in C++

    In this tutorial, we will learn how to create a count down timer program in C++. To understand better, you may go through the following first.

    The program takes the value to start the countdown and then reverse the countdown from there. The while loop is used to reverse the entered value.


    Print Count Down Timer in C++

    #include <iostream>
    #include <Windows.h>
    using namespace	std;
    
    int main()
    {
        int	timer;
        
        cout << "Set the timer: ";
        cin	>> timer;
        
        while(timer > 0)
        {
            cout << "Time Remaining:" << timer <<endl;
            Sleep(1000);
            --timer;
        }
        
        cout << "\nTime Up!";
        
        return 0;
    }

    Output:

    Set the timer: 4
    Time Remaining: 4
    Time Remaining: 3
    Time Remaining: 2
    Time Remaining: 1

    Time Up!


  • C++ Program to Subtract Two Matrices

    In this tutorial, we will learn and write a C++ program to print the subtraction of two matrices. To understand the coding, you should have knowledge of the following topics in C++ programming:

    The program takes the user input for the number of rows and columns and then the elements for both the matrices.

    Lastly, subtract the matrices and store them in a separate array and display the result. Let us go through the program.


    C++ Program to Subtract Two Matrices

    During the subtraction of matrices, it is important that both of the matrices should have the same dimensions such as the 2×3 matrix can be subtracted only with a 2×3 matrix.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr1[50][50], arr2[50][50], sub[50][50];
      int row, col, i, j;
    
      cout << "Enter the no. of rows: ";
      cin >> row;
      cout << "Enter the no. of columns: ";
      cin >> col;
    
      // 1st matrix user input
      cout << endl << "Enter 1st Matrix elements: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          cin >> arr1[i][j];
    
      // 2nd matrix user input
      cout << endl << "Enter 2nd Matrix elements: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          cin >> arr2[i][j];
    
      // matrix adition
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          sub[i][j] = arr1[i][j] - arr2[i][j];
    
      // Displaying the result
      cout << endl << "Result of the subtraction of the matrices: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
        {
          cout << sub[i][j] << "  ";
          if (j == col - 1)
            cout << endl;
        }
    
      return 0;
    }

    Output:

    Enter the no. of rows: 2
    Enter the no. of columns: 3

    Enter 1st Matrix elements:
    6
    5
    4
    3
    2
    1

    Enter 2nd Matrix elements:
    3
    2
    3
    3
    1
    1

    Result of the subtraction of the matrices:
    3 3 1
    0 1 0


  • C++ Program to Find Smallest Element in an Array

    In this tutorial, we will learn and write a print the smallest element in an array in C++. To understand the coding, you should have knowledge of the following topics in C++ programming:


    1. C++ program to find the smallest element in an array

    The program takes user input for the size and the values of elements of an array. Lastly, displays the smallest element present in the array.

    The first element in an array is assumed to be the smallest element then iterating the array using for loop, we compare each of the elements present in an array. Whenever the element is found to be smaller then the value is inserted into the smallest variable.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int arr[50], size, smallest, i;
    
       cout << "Enter the size of an array: ";
       cin >> size;
    
       cout << "Enter " << size << " Array Elements:\n";
       for (i = 0; i < size; i++)
          cin >> arr[i];
    
       smallest = arr[0];
       for (i = 1; i < size; i++)
       {
          if (smallest > arr[i])
             smallest = arr[i];
       }
    
       cout << "\nSmallest Element in an array: " << smallest;
    
       return 0;
    }

    Output:

    Enter the size of an array: 5
    Enter 5 Array Elements:
    23
    65
    12
    19
    16

    Smallest Element in an array: 12


    2. Using Function

    The program logic is the same as the above only difference is we will create a separate function to search for the smallest element and return the value from that function.

    #include <iostream>
    using namespace std;
    
    //function to find smallest element
    int findSmallest(int a[], int s)
    {
      int i, small;
      small = a[0];
    
      for (i = 1; i < s; i++)
      {
        if (small > a[i])
          small = a[i];
      }
    
      return small;
    }
    
    //main function
    int main()
    {
      int arr[50], size, smallest, i;
    
      cout << "Enter the size of an array: ";
      cin >> size;
    
      cout << "Enter " << size << " Array Elements:\n";
      for (i = 0; i < size; i++)
        cin >> arr[i];
    
      smallest = findSmallest(arr, size);
    
      cout << "\nSmallest Element in an array: " << smallest;
    
      return 0;
    }

    Enter the size of an array: 5
    Enter 5 Array Elements:
    3
    56
    2
    98
    12

    Smallest Element in an array: 2


    3. Find Smallest Number in Array using Pointer in C++

    If you want to learn about the pointer and how it works then click the link below.

    Source code:

    #include<iostream>
    using namespace std;
    int main()
    {
        int arr[50], size, i, small;
        int *ptr;
        
        cout << "Enter the Size for an Array: ";
        cin >> size;
        
        cout << "Enter " << size << " Array Elements:\n";
        for(i=0; i<size; i++)
            cin>>arr[i];
            
        ptr = &arr[0];
        small = *ptr;
        
        while(*ptr)
        {
            if(small > (*ptr))
                small = *ptr;
            ptr++;
        }
        
        //Display
        cout << "\nSmallest Element: " << small;
        
        return 0;
    }

    Output:

    Enter the Size for an Array: 5
    Enter 5 Array Elements:
    12
    45
    11
    36
    30

    Smallest Element: 11


  • C++ Program to Find Largest Number in an Array

    In this tutorial, we will learn and write a print the largest element in an array in C++. To understand the coding, you should have knowledge of the following topics in C++ programming:


    1. C++ program to find the largest element in an array

    The program takes user input for the size and the values of elements of an array. Lastly, displays the largest element present in the array.

    The first element in an array is assumed to be the largest element then iterating the array using for loop, we compare each of the elements present in an array. Whenever the element is found to be smaller then the value is inserted into the largest variable.

    #include<iostream>
    using namespace std;
    
    int main()
    {
        int arr[50], size, largest, i;
        
        cout<<"Enter the size of an array: ";
        cin>>size;
        
        cout<<"Enter " << size << " Array Elements:\n";
        for(i = 0; i < size; i++)
            cin>>arr[i];
            
        //assuming first element to be largest
        largest = arr[0];
        
        for(i = 1; i < size; i++)
        {
            if(largest<arr[i])
                largest = arr[i];
        }
        
        //Display the result
        cout << "\nLargest Element in an array: " << largest;
        
        return 0;
    }

    Output:

    Enter the size of an array: 5
    Enter 5 Array Elements:
    25
    12
    58
    5
    12

    Largest Element in an array: 58


    2. Using Function

    The program logic is the same as the above only difference is we will create a separate function to search for the largest element and return the value from that function.

    #include<iostream>
    using namespace std;
    
    //function to find largest element
    int findLargest(int a[], int s)
    {
        int i, large;
        large = a[0];
        for(i=1; i<s; i++)
        {
           if(large < a[i])
               large = a[i];
        }
        return large;
    }
    
    //main function
    int main()
    {
        int arr[100], size, largest, i;
        
        cout<<"Enter the size of an array: ";
        cin>>size;
        
        cout<<"Enter " << size << " Array Elements:\n";
        for(i=0; i<size; i++)
            cin>>arr[i];
            
        largest = findLargest(arr, size);
        
        cout<<"\nLargest Element in an array: "<<largest;
        
        return 0;
    }
    

    Output:

    Enter the size of an array: 5
    Enter 5 Array Elements:
    53
    2
    70
    23
    11

    Largest Element in an array: 70


  • C++ Program to Find if an Array is an Identity Matrix

    In this tutorial, we will learn and write a C++ program to check whether a given matrix is an identity matrix or not. To understand the coding, you should have knowledge of the following topics in C++ programming:


    Identity Matrix:

    A square matrix is said to be an identity matrix if each entry on its diagonal is equal to 1 and the rest are 0. Example:

    Matrix of dimension 3 by 3:
    1 0 0
    0 1 0
    0 0 1


    Explanation: The program takes user input for the number of rows and columns of a matrix. The values are of each element are also taken from the user.

    Before entering the elements of the matrix, the program also checks for the matrix to be a square matrix. If it is a square matrix then the execution control moves to the next line but if not then the program is exited.

    The program checks the diagonals matrix for 1 and the rest for 0. If the conditions are found to be true then the entered matrix is identity matrix else not.

    Now let us go through a program for Identity Matrix in C++.


    C++ Program to Find if an Array is an Identity Matrix

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int i, j, row, col, arr[10][10], flag = 0;
    
      cout << "Enter the number of rows: ";
      cin >> row;
      cout << "Enter the number of column: ";
      cin >> col;
    
      //Checking for square matrix
      if (row != col)
      {
        cout << "\nMatrix is not a square matrix. Try Again!";
        exit(0);
      }
    
      cout << "\nEnter the elements of matrix:\n";
      for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
          cin >> arr[i][j];
    
      //Checking for diagonals and rest
      for (i = 0; i < row; i++)
      {
        for (j = 0; j < col; j++)
        {
          if ((arr[i][j] != 1) && (arr[j][i] != 0))
          {
            flag = 1;
            break;
          }
        }
      }
    
      //Display accordingly
      if (flag == 0)
        cout << "\nThe entered matirix is an identity matrix.\n";
      else
        cout << "\nThe entered matirix is NOT an identity matrix.";
    
      //Display the matrix
      for (i = 0; i < row; i++)
      {
        for (j = 0; j < col; j++)
          cout << arr[i][j] << " ";
        cout << "\n";
      }
    
      return 0;
    }

    Output:

    identity matrix in C++

  • C++ Program to Insert an Element in an Array

    In this tutorial, we will learn and write a program to insert an element in an array in C++. To understand the coding, you should have knowledge of the following topics in C++ programming:


    We will learn two ways to insert an element in an array in C++:

    1. Insert an element at the end of an array
    2. Insert an element at a specific position of an array

    1. Insert an element at the end of an array

    The program takes user input for the size and the values of elements of an array.

    Also takes the user inputs for the value of an element to be inserted at the end of an array. Lastly, displays the new array formed after insertion.

    //C++ Program to insert an Element in an array
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr[30], size, i, insElem, count = 0;
    
      cout << "Enter the size of an array (Max size: 30): ";
      cin >> size;
    
      cout << "Enter array elements:\n";
      for (i = 0; i < size; i++)
        cin >> arr[i];
    
      cout << "\nEnter element to be inserted: ";
      cin >> insElem;
      
      arr[i] = insElem;
      
      //Display new array
      cout << "New Array after Insertion:\n";
      for (i = 0; i < (size+1); i++)
          cout << arr[i] << " ";
    
      return 0;
    }

    Output:

    insert element in an array in C++

    2. Insert Element in Array at a Specific Position

    Here too, the program takes input from the user but in addition to the above, the position at which the element is to be inserted is also taken as user input.

    //C++ Program to insert an Element at any position
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr[30], size, pos, i, insElem, count = 0;
    
      cout << "Enter the size of an array (Max size: 30): ";
      cin >> size;
    
      cout << "Enter array elements:\n";
      for (i = 0; i < size; i++)
        cin >> arr[i];
    
      cout << "\nEnter element to be inserted: ";
      cin >> insElem;
    
      cout << "Enter the position: "; //position specified
      cin >> pos;
    
      for (i = size; i >= pos; i--)
        arr[i] = arr[i - 1];
    
      arr[i] = insElem;
    
      //Display new array
      cout << "New Array after Insertion:\n";
      for (i = 0; i < (size + 1); i++)
        cout << arr[i] << " ";
    
      return 0;
    }

    Output:

    Enter the size of an array (Max size: 30): 5
    Enter array elements:
    1
    2
    3
    4
    5

    Enter element to be inserted: 6
    Enter the position: 3
    New Array after Insertion:
    1 2 6 3 4 5


  • C++ Program to Delete an Element from an Array

    In this tutorial, we will learn and write code on deleting an element from an array in C++. To understand the coding, you should have knowledge of the following topics in C++ programming:


    Delete Element from Array in C++

    The program takes user input for the size of an array and for the elements of an array. Then the program will ask the user to enter a number from the array to be deleted.

    Lastly, after the deletion of an element, the new array is displayed.

    C++ Program to Delete an Element from an Array

    //C++ Program to Delete an Element in an array
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr[30], size, i, delElem, count = 0;
    
      cout << "Enter the size of an array (Max size: 30): ";
      cin >> size;
    
      cout << "Enter array elements:\n";
      for (i = 0; i < size; i++)
        cin >> arr[i];
    
      cout << "\nEnter element to be delete: ";
      cin >> delElem;
    
      for (i = 0; i < size; i++)
      {
        if (arr[i] == delElem)
        {
          for (int j = i; j < (size - 1); j++)
          {
            arr[j] = arr[j + 1];
          }
    
          count++;
          break;
        }
      }
    
      if (count == 0)
      {
        cout << "\nElement not found..!!\n";
      }
      else
      {
        cout << "\nElement deleted successfully..!!\n";
    
        //Display new array
        cout << "New Array after Deletion:\n";
        for (i = 0; i < (size - 1); i++)
          cout << arr[i] << " ";
      }
    
    
      return 0;
    }

    Output:

    delete array element c++

  • C++ Program to Add Two Matrices

    In this tutorial, we will write a C++ program to add two matrices using multi-dimensional arrays. To understand the coding, you should have knowledge of the following topics in C++ programming:

    The program takes the user input for the number of rows and columns then the elements for both the matrices.

    Lastly, add both the matrices and store them in a separate array and display the result. Let us go through the program.


    C++ Program to Add Two Matrices

    During addition or subtraction of matrix, it is important that both of the matrices have the same dimensions such as 2×3 matrix can be added to 2×3 matrix.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int arr1[50][50], arr2[50][50], sum[50][50];
      int row, col, i, j;
    
      cout << "Enter the no. of rows: ";
      cin >> row;
      cout << "Enter the no. of columns: ";
      cin >> col;
    
      // 1st matrix user input
      cout << endl << "Enter 1st Matrix elements: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          cin >> arr1[i][j];
    
      // 2nd matrix user input
      cout << endl << "Enter 2nd Matrix elements: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          cin >> arr2[i][j];
    
      // matrix adition
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
          sum[i][j] = arr1[i][j] + arr2[i][j];
    
      // Displaying the result
      cout << endl << "Result of sum of the matrices: " << endl;
      for (i = 0; i < row; ++i)
        for (j = 0; j < col; ++j)
        {
          cout << sum[i][j] << "  ";
          if (j == col - 1)
            cout << endl;
        }
    
      return 0;
    }

    Output:

    Enter the no. of rows: 2
    Enter the no. of columns: 3

    Enter 1st Matrix elements:
    2
    2
    2
    2
    2
    2

    Enter 2nd Matrix elements:
    3
    3
    3
    3
    3
    3

    Result of sum of the matrices:
    5 5 5
    5 5 5


  • Flowchart – Advantages and Disadvantages

    In this section, we will study the advantages of flowchart and also the disadvantages of flowchart. Let us start by understanding what is flowchart.

    A flowchart is a graphical representation of a programming language code so to relate the relations among functions. To simply the problem before coding, the programmer draws a building block with some specific geometric figures and relates the relationship.


    Fowchart Advantages

    • The flowchart helps the programmer to understand the logic of the program in a suitable way.
    • With the help of flowchart, problem can be analyzed in a very effective way.
    • Complex programs can be easily visualized with the help of the flowchart
    • Flowchart is a convenient and very efficient method of communication.
    • The flowchart is useful in troubleshooting and in debugging process.
    • It is a key for correct programming and initial building.

    Flowchart Disadvantages

    • Although the flowchart may give you better advantage, however it sometimes results in waste of time and slows the develpoment process.
    • Sometimes for a large program, the flowchart representation becomes too messy, clumsy and hard to understand for the programmer. It results in a waste of time and effort programmers put into.
    • If the programmer modify in a logic, this might lead to complete redraw of the flowchart. Hence, cost you a whole lot of time.

    You may look at this example to understand more:


  • Basic Applications Of Computer

    This article focuses on the basic applications of computers, listing their uses in different fields. We will go through the details of how they are useful in their respective fields.

    A computer is an electronic device through which we can perform various tasks. The following are some of the applications of computers:

    • Business application
    • Scientific Research
    • Education Purposes
    • Marketing
    • Banking
    • Communication
    • Entertainment purposes
    • Insurance
    • Engineering Design
    • Games
    • Health Care
    • Military Purposes

    Business Application:

    Nowadays, the application of computers in the business field is highly increasing because of their high speed of calculation of large amounts of data, accuracy, reliability, and versatility.

    All business organizations run their business through via the internet with the help of computers without any delay. business organizations use a computer for their Sales analysis, Budgeting, Managing huge databases and stocks, financial management, etc.


    Scientific Research:

    The computers that are used in scientific research has a great ability to analyze the huge amount of data at a speed which is not possible for the human to anticipate. These computers are so precise that they could determine the exact behavior of certain chemical compounds in a different environment.

    They are used in robotics research to control the robots for better use of it.


    Education Purposes:

    The of the benefit of having a computer is that you can educate yourself through various videos, articles, pdf, images, etc. Computers have dominant applications in the education field where you can enhance yourself in your particular fields through online learning or offline learning. It has a tool for the education system called Computer-Based Education(CBE).

    Nowadays, the scale of computer students are increasing rapidly, all school provide computer education class all over the world and has its massive use.


    Marketing:

    Uses of computers in marketing fields are mostly for the following:

    Advertising: With the use of computers, advertising professionals are able to enhance their skills in arts and graphics writing skills, print and disseminate product ads with the aim to sell more products.

    Home Shopping: With the help of computers home shopping/ delivery services have become more efficient and easy. People can view the product information that is provided through graphical catalogs manage via computers, access to the order of the products.


    Banking:

    Today almost all banking around the world totally depends on computers. Banks have the vast and continuous use of computers. For example, ATM Machine allows us not only to withdraw cash but to deposit cash get information on accounts services, check balance, transfer of money, etc.

    Banking provides the facilities to manage accounts online, banks use computers to interconnect with their respective branches and other branches, and many more.


    Communication:

    The application of computers on communication is a huge benefit for both individuals and also to any organization. Through communication, we can send our ideas, convey our thoughts, speech, etc. For example, through email, we are able to contact our friends in any part of the world rather than with pen and paper. We can contact through message or voice or through video.

    Some applications that are used in this category: E-mail, Usenet, Chatting, FTP, Video-conferencing, and Telnet.


    Entertainment purposes:

    All most all people use computers for entertainment purposes in the following field:

    • watching movies
    • listening to songs
    • photos
    • watching animations etc.

    Professional video editors use the computer to edit their videos to make them more attractive in the field of entertainment.


    Insurance:

    To keep the records in their database up-to-date, all the Insurance companies use computers. Also, the finance houses and stockbroking firms widely use the computer for their purposes. The insurance companies show the information of the clients to maintain their accounts such as the starting date of the policies, next due installment of a policy, maturity date, interests due, etc.


    Engineering Design:

    Engineers use a computer for their design the layout of their machine, architecture in their respective fields. CAD(Computer-Aided Design) provides the creation and modification of images at a very high speed. Some of the engineering fields that widely use computers are:
    Structural Engineering, Industrial Engineering, and Architectural Engineering.


    For Games purposes:

    Nowadays the gaming field is increasing rapidly that uses computers.
    From online games to offline games, kids, as well as adults, are seen as active. Big Gaming Companies organize gaming competitions that are possible only with computers. Some varieties of games include action games, racing games, puzzles, combat, and many more.

    Some examples of games that are highly played on computers are:
    Counter-Strike, PubG, Minecraft, Grand Theft Auto, Halo, etc.


    Health Care/ Hospitals:

    Computers have become an important part of hospitals, medical labs, and dispensaries. Hospitals use computers to keep the records of the patients and are stored in their database for future use, also to keep track of the medicines. Computerized machines are used in hospitals for CT scans, diagnosing, ECG, X-ray, ultrasound, etc.

    Some important healthcare field where computers are necessary are:
    Surgery, Diagnostic System, Patient Monitoring System, Pharmaceutical field.


    For Military purposes:

    The defense system of all countries uses computers to enhance their equipment for their defense systems. The designing of the weapon system, missiles, base to base communication system, keeping track/records of criminals, satellite communication are possible only through computers.