Saturday, 29 November 2014

GCC Compiler

GCC:

About: The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain. The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL). GCC has played an important role in the growth of free software, as both a tool and an example.

Link:download

Installing TDM GCC Compiler

Click on Create and uncheck "check for updates" if you dont have active internet connection

Choose the edition of installation based on your OS(32-bit or 64-bit). If you are not sure what to choose go for 32-bit

Choose Your Installation directory

Installation Completed

Compiling Programs


we can compile our C or C++ program using two ways


1) Without any IDE(using your favorite text editior and cmd)
2) With an IDE.

Without IDE(complicated)


step1:: Write your c or c++ code using any text editor but save it with .c or .cpp extension.

step2:: Before Compiling set path to the directory of gcc bin folder in your sys variables. In my case it is C:\TDM-GCC-32\bin

step3:: Now open your command prompt or cmd. Change the directory in which your c or c++ file is saved.

step4:: To compile a C file use command " gcc filename.c " To compile a C++ file use command " g++ filename.cpp" (if you have errors it will show and correct the source code)

step5:: After Successfull Compilation, Simply type " a " to Run the program.

With IDE.(using Code::Blocks)


About: Code::Blocks is a free and open source, cross-platform IDE which supports multiple compilers including GCC, Clang and Visual C++. It is developed in C++ using wxWidgets as the GUI toolkit.

Link:download

Installing Code::Blocks

Starting Page of Code::Blocks

After Installing Code::Blocks open it.
It will Automatically detect TDM GCC compiler

Create a new file using file->new->file

Choose File type(c or c++)

Give Filename with Full Path
Creating first.c file

first.c

Sample Program

Errors and warnings are shown at the bottom in logs and others section

Creating second.cpp file

Sample c++ Program
Click F9 to build and run

Output

Sunday, 16 November 2014

Complex Arithmetic

/* Complex Numbers
* **Addition
* **Subtraction
* **Multiplication
* **Division
******* *Turbo C++ **16/11/2014 ****Hemanth
*/
#include<iostream.h>
#include<conio.h>
class Complex
{
             float x,y;
                     void addition(Complex &,Complex &);
                     void subtraction(Complex &,Complex &);
                     void multiplication(Complex &,Complex &);
                     void division(Complex ,Complex);
                     void display(Complex &);
Complex(int a)
{
                      //No Default Values
}
Complex()
{
                 //OverLoading Constructor To Take Values from User
                cout<<"Give REAL part of Complex Number\n"<<endl;
               cin>>x;
               cout<<"Give IMAGINARY part of Complex Number\n"<<endl;
               cin>>y;
}
};
void Complex::addition(Complex &a,Complex &b)
{
                x=(a.x+b.x);
               y=(a.y+b.y);
}
void Complex::subtraction(Complex &a,Complex &b)
{
                 x=(a.x-b.x);
                y=(a.y-b.y);
}
void Complex::multiplication(Complex &a,Complex &b)
{
               x=(a.x*b.x)-(a.y*b.y);
              y=(a.x*b.y)+(a.y*b.y);
}
void Complex::division(Complex a,Complex b)
{
                 b.y=(-1*b.y)                     //Taking Conjugate of num2 and performing multiplication
                Complex numerator(2);
                numerator.multiplication(a,b);
               x=numerator.x/((b.x*b.x)+(b.y*b.y));
               y=numerator.y/((b.x*b.x)+(b.y*b.y));
}
void Complex::display(Complex &result)
{
             cout<<result.x;
            if(result.y<0)
                    cout<<"-i"<<-1*result.y;
           else
                   cout<<"+i"<<result.y;
}
int main()
{
        clrscr();
        int option;
        Complex result(1);
        cout<<"\t\tArithmetic Operations On Complex Numbers\n\n";
        cout<<"Enter 1st Complex Number\n"<<endl;
       Complex num1;
       cout<<"Enter 2nd Complex Number\n"<<endl;
       Complex num2;
       do{
         clrscr();
        cout<<"\t\tNUM1: ";
        num1.display(num1);
        cout<<"\t\tNUM2: ";
        num2.display(num2);
        cout<<"\n\n\t1.Addition\n\n\t2.Subtraction\n\n\t3.Multiplication\n\n";
         cout<<"\t4.Division\n\n\t5.Exit\n\nEnter Your Choice:";
        cin>>option;
        switch(option)
        {
        case 1:
              result.addition(num1,num2);
              cout<<"\nAddition is ";
              result.display(result);
             break;
       case 2:
            result.subtraction(num1,num2);
            cout<<"\nSubtraction is ";
            result.display(result);
            break;
       case 3:
           result.multiplication(num1,num2);
          cout<<"\nMultiplication is ";
          result.display(result);
         break;
       case 4:
         result.division(num1,num2);
         cout<<"\nDivision is ";
         result.display(result);
        break;
    }
    getch();
  } while(option!=5);
return 0;
}
            

Saturday, 15 November 2014

Guess Game with C++

                     //   Having Problems Executing Program....Comment below

/*            A simple GUESS GAME
*            Compiled Using Turbo C++
*            14/11/2014           ***Hemanth
*/

#include<iostream.h>            
#include<conio.h>   
#include<stdlib.h>
int main()
{
            int guess,Random,i;
            clrscr();
            randomize();
           Random=random(101);
           cout<<"\t\tGUESS GAME\n\t\tguess a number between 0 and 100\n"<<endl;
          for(i=1;i<=5;i++)
         {

                   cout<<"\nEnter your guess"<<endl;
                  cin>>guess;
                  if(guess==Random)
                {
                    cout<<"YOU WON!"<<endl;
                    getch();
                    exit(0);
               }
               if(guess>Random)
                        cout<<"\nSorry,Wrong Guess! Number is less than "<<guess;
              if(guess<Random)
                        cout<<"\nSorry,Wrong Guess! Number is greater than "<<guess;
             cout<<"\tChances Left:"<<5-i<<endl;
        }
 cout<<"\nYour 5 chances are completed.YOU LOST!"<<endl;
 cout<<"\ncorrect GUESS is "<<Random;
 getch();
 return 0;
}