Sunday, March 11, 2018

Addition of two Binary numbers using C

Addition of two Binary numbers using C 

The operation of adding two binary numbers is one of the fundamental tasks performed by a digital computer. The four basic addition operations are 0 + 0 = 0, 1 + 0 = 1, 0 + 1 = 1 and 1 + 1 = 10. In the first three operations, each binary addition gives sum as one bit , i.e. , either 0 or 1.But the fourth addition operation gives a sum that consists of two binary digits. In such result of the addition, lower significant bit is called as the sum bit, whereas the higher significant bit is called as the carry bit.


Binary Addition of Two Bits
0011
+ 0+ 1+ 0+ 1
011(carry) 1←0





C Program for addition of two binary Numbers:

#include <stdio.h>
#include <conio.h>

int main()
{
    long int binary1, binary2;
    int i = 0, remainder = 0, sum[20];
    printf("Enter any first binary number :");
    scanf("%ld", &binary1);
    printf("Enter any second binary number :");
    scanf("%ld", &binary2);

    while(binary1!= 0 || binary2 != 0)
    {
        sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
        remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2;
        binary1 = binary1 / 10;
        binary2 = binary2 / 10;
    }

    if(remainder != 0)
        sum[i++] = remainder;
    --i;
    printf("Sum of two binary numbers :");
    while(i >= 0)
        printf("%d", sum[i--]);

    return 0;
}


Sample Output:
 


Tuesday, December 26, 2017

DDA Algorithm

The Digital Differential Algorithm (DDA) is a scan-conversion line drawing algorithm. A line is sampled at unit intervals in one coordinate and the corresponding integer values nearest the line path are determined for the other coordinate.

Algorithm

 1. Get the end points (x1, y1) and (x2, y2) of a line.
 2. Set x = x1, y = y1.
 3. Plot (x, y).
 4. Compute dx = x2-x1 and dy = y2-y1.
 5. If |dy| &lt; |dx|
       steps = |dx|
    else
       steps = |dy|
 6. Compute delX = dx/steps
            delY = dy/steps
 7. Repeat following for <strong>steps</strong> times
       a. x = x + delX
       b. y = y + delY
       c. Plot (x, y) 

This program takes x-coordinate and y-coordinate of endpoints of line to be drawn. Also color of the line is passed.

     // DDA(Digital Differential Analyzer) Algorithm...
    #include <iostream>
    #include <conio.h>
    #include <graphics.h>
    #include <cmath>
    #define R(z) int(z+0.5)   // Macro to round floating numbers to integer

    using namespace std;

    void lineDDA(int x1, int y1, int x2, int y2, int col)
    {
        float x,y,delX,delY;
        int steps, dx, dy, k;

        x=x1;
        y=y1;
        putpixel(x, y, col);

        dx = x2-x1;
        dy = y2-y1;

        if(abs(dy)<abs(dx))
            steps = abs(dx);
        else
            steps = abs(dy);
        delX = (float)dx/steps;
        delY = (float)dy/steps;
        for(k=0; k<steps; k++)
        {
            x = x+delX;
            y = y+delY;
            putpixel(R(x), R(y), col);
        }
    }


    int main()
    {
        int gd = DETECT, gm;
        initgraph(&gd, &gm, "C:\\tc\\BGI");
        int x=getmaxx(), y=getmaxy();
        cout<<x<<y;
        lineDDA(0, 0, 630, 466, 1);
        lineDDA(0, 466, 630, 0, 2);
        lineDDA(630/2, 0, 630/2, 466, 3);
        lineDDA(0, 466/2, 630, 466/2, 4);

        getch();
        return 0;
    }

     

        Output:

       
          

Wednesday, June 14, 2017

Reverse String

Reverse String in C++

Here is the source code for C++ program that reverses the input string...

For example:  Input :"coder"
                      Output :"redoc"


Program

#include <iostream>
#include<cstdio>
using namespace std;

string FirstReverse(string str) {    //Function to reverse string
    
    string reverseString;

    for(long i = str.size() - 1;i >= 0; --i){
        reverseString += str[i];
    }

    return reverseString;

}

int main() {
string str;
cout<<"Enter some text: ";
    cin>>str;
    cout << FirstReverse(str)<<endl;   //function call
    return 0;

}



//int main portion can be written as: //As in coderbyte...

int main()
{
cout<<FirstReverse(gets(stdin))<<endl;

return 0;
}

Note: This is the first challenge program of coderbyte...
https://coderbyte.com


Monday, June 12, 2017

Regula Falsi Method

Regula Falsi Method

Regula Falsi Method is also known as "false position method" is a bracketing method used to solve equations of form f(x)=0Interpolation is the approach of this method to find the root of nonlinear equations by finding new values for successive iterations. In this method, unlike the secant method, one interval always remains constant.

I am gonna solve f(x)=x^2-4x-10 using this method in C++...

Algorithm:

  1. Start
  2. Read values of x0, x1 and e
    *Here x0 and x1 are the two initial guesses
    e is the degree of accuracy or the absolute error i.e. the stopping criteria*
  3. Computer function values f(x0) and f(x1)
  4. Check whether the product of f(x0) and f(x1) is negative or not.
    If it is positive take another initial guesses.
    If it is negative then goto step 5.
  5. Determine:
    x = [x0*f(x1) – x1*f(x0)] / (f(x1) – f(x0))
  6. Check whether the product of f(x1) and f(x) is negative or not.
    If it is negative, then assign x0 = x;
    If it is positive, assign x1 = x;
  7. Check whether the value of f(x) is greater than 0.00001 or not.
    If yes, goto step 5.
    If no, goto step 8.
    *Here the value 0.00001 is the desired degree of accuracy, and hence the stopping criteria.*
  8. Display the root as x.
  9. Stop

Program

#include <iostream>
#include<cmath>
#include<iomanip>

using namespace std;

double fun(double x)
{
  return(x*x-4*x-10);
}

const double TOL=1e-5;

int main()
{
    double x3,x1,x2,xs,f1,f2,f3;
    int iter=0;
    cout << "Enter brackting numbers x1, x2" << endl;
    cin>>x1>>x2;

    f1=fun(x1);
    f2=fun(x2);

    if((f1*f2)>0)
        {
            cout<<"\nDoesn't bracket...\n";
         }
    else
    {
        do
        {
            xs=x3;
            x3=x1-(f1*(x2-x1))/(f2-f1);
            f3=fun(x3);
            iter++;

            cout<<setprecision(10)<<setw(3)<<iter<<setw(15)<<x1<<setw(15)<<x2<<setw(15)<<fun(x3)<<endl;

            if((f1*f3)<0)

                x2=x3;
            else
                x1=x3;
                f1=fun(x1);
                f2=fun(x2);
        }while(fabs(fun(x3))>=TOL);//Terminating case
    }
    cout<<"\nThe root of the equation is :"<<x3;
    cout<<"\n\nf(x)="<<fun(x3);
    return 0;
}

OUTPUT




I have uploaded video on this topic :https://youtu.be/u-S4w4eclf0
Plz suscribe , like and donot forget to follow me in this blog...

Sunday, June 11, 2017

CodeBlocks Installation 

Code::Blocks is a free, open-source, cross-platform C, C++ and Fortran IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable

Code Blocks is cross platform ide i.e. it works in windows as well as linux and Mac OX but you should download corresponding setup files.

We will be downloading Mingw installer.  You can follow the link from my previous blog or watch the video below....


It is easy to install as you donot need to install any other compiler or so on.Just agree to all and press next...

If you encounter any problem during installation you can ask in COMMENT section...

C++ :Setup Environment

Setup Environment <For Windows>

If you are still willing to set up your environment for C++, you need following two softwares available on your computer.

Text Editor:

This will be used to type your program. Examples of few editors include Windows Notepad, Notepad++,Sublimetext Editor, etc.
The files you create with your editor are called source files and for C++ they typically are named with the extension .cpp, .cp, or .c.
Before starting your programming, make sure you have one text editor in place and you have enough experience to type your C++ program.

C++ Compiler:

This is actual C++ compiler, which will be used to compile your source code into final executable program.Most frequently used and free available compiler is GNU C/C++ compiler.

IDE:

IDE (Integrated Development Envionment is a software suite that consolidates the basic tools developers need to write and test software. Typically, an IDE contains a code editor, a compiler or interpreter and a debugger that the developer accesses through a single graphical user interface (GUI). An IDE may be a standalone application, or it may be included as part of one or more existing and compatible applications.

With IDE installed in your system you donot need  to install editor and complier saperately. Following are some of the famous IDE's for windows (most of them are also available for linux and Mac OX) along with their download links. 
These all IDE's are free.Personally, I prefer codeblocks. Dev C++ has many bugs. Be sure to download Codeblocks Mingw setup(other donot support C++). For eclipse you need to install JDK, plus download Mingw  complier saperately. As for Visual Studio I advise you to install only when you have sound knowledge on C++.


Plz :Donot hesitate to like, suscribe , follow and comment...
   I will come up with learning resources and codeblocks installation.
Plz write your queries in comment section. I will try my best to solve... 
Your feedbacks are always welcome...
Looking for your full support... 
 

Saturday, June 10, 2017

C++ :A brief Introduction

INTRODUCTION:

C++ (pronounced cee plus plus /ˈs plʌs plʌs/) is a general-purpose programming language. It has imperativeobject-oriented andgeneric programming features, while also providing facilities for low-level memory manipulation.

In 1998, C++ was developed by Bjarne Stroustrup at Bell Labs since 1979, as an extension of the C language as he wanted an efficient and flexible language similar to C, which also provided high-level features for program organization. C++ inherits most of C's syntax. The following is Bjarne Stroustrup's version of the Hello world program that uses the C++ Standard Library stream facility to write a message to standard output:
Above program prints 'Hello world!'

C++ is a superset of C, and that virtually any legal C program is a legal C++ program.C++ supports a variety of programming styles. You can write in the style of Fortran, C, Smalltalk, etc., in any language. Each style can achieve its aims effectively while maintaining runtime and space efficiency.

Object-Oriented Programming

C++ fully supports object-oriented programming, including the four pillars of object-oriented development:

  1. Encapsulation
  2. Data hiding
  3. Inheritance
  4. Polymorphism

It provides a lot of other features that are given below.




Use of C++

C++ is used by hundreds of thousands of programmers in essentially every application domain.
C++ is being highly used to write device drivers and other softwares that rely on direct manipulation of hardware under realtime constraints.
C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts.
Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++.

Addition of two Binary numbers using C

Addition of two Binary numbers using C  The operation of adding two binary numbers is one of the fundamental tasks performed by a digita...