C++ Program to illustrate reference variable [DEVCPP/GCC]


REFERENCE VARIABLE

C++ allows to create a reference to an existing variable. It provides a new name to an existing variable. Once a reference is created for a variable, either the variable name or reference name can be used to refer the variable.

The reference variable internally works as an constant pointer i.e. Once a reference is created for a variable, then it can't be changed. It is dereferenced internally without the use of dereferencing operator(*).

PROGRAM

// Program to illustrate reference variable

#include <iostream>

using namespace std;

int main()
{
       int num;

       cout<<"\nENTER A NUMBER: ";
       cin>>num;

int &RefToNum = num;

// RefToNum is a reference variable referring to num

       cout<<"\nVALUE OF NUM: "<<num;
       cout<<"\nVALUE OF REFERENCE TO NUM: "<<RefToNum;

       cout<<"\n\nSQUARING NUM...";

       num = num*num;

       cout<<"\n\nVALUE OF NUM: "<<num;
       cout<<"\nVALUE OF REFERENCE TO NUM: "<<RefToNum;

/* Both variable results in the same value as
they are operating on the same memory location */

       return 0;
}

OUTPUT

C++ Program to illustrate reference variable with output

C++ Program to illustrate reference variable


C++ Program to perform addition of two numbers using pointers [DEVCPP/GCC]

PROGRAM

//Program to perform addition of two numbers using pointers

#include <iostream>

using namespace std;

int main()
{
int x,y,sum;

cout<<"ENTER FIRST NUMBER: ";
cin>>x;
cout<<"ENTER SECOND NUMBER: ";
cin>>y;

int *p1=&x;
int *p2=&y;

sum= (*p1)+(*p2);

cout<<"SUM OF "<<x<<" AND "<<y<<" IS "<<sum;
return 0;
}

OUTPUT

C++ Program to perform addition of two numbers using pointers with output


C++ Program to perform addition of two numbers using pointers with output

C++ Introduction to Pointers


VARIABLES

A variable is an abstraction of a memory cell. These are named memory locations used to store a value. The address of a variable can be easily obtained by Address-of Operator(&).

 int num;                                                     
 cout<< &num;

POINTERS

Pointer is a powerful mechanism in C++ programming. Pointers are special variables that contains the address of another variable.

A pointer is declared as following:

 int *int_ptr;                                 
 char *char_ptr;                                                float *float_ptr;                                                        

After declaration, we can assign address of variables to a pointer.

 int num=10;                                                 
 int_ptr = &num;                                                               
 char ch='a';                                               
 char_ptr = &ch;                                                      
 float val=3.25;                                           
 float_ptr = &val;                                                        

A fundemental operation on pointers in Dereferencing i.e. referring to the object pointed by that pointer.

 cout<< *int_ptr;    //10                                     
 cout<< *char_ptr;   //a                                      
 cout<< *float_ptr;  //3.5  


C++ Introduction to Pointers


C++ Program to obtain Fibonacci series using recursion [DEVCPP/GCC]


PROGRAM

//Program to get the Fibonacci series using recursion 

# include <iostream>

using namespace std;

int fib(int n)
{
if(n==1||n==2)     //base condition
{
return 1;
}
else
        {
return fib(n-1)+fib(n-2);
         }
}

int main()
{
int i,num;
cout<<"ENTER THE NUMBER UPTO WHICH YOU WANT THE FIBBONACI SERIES TO          BE PRINTED:\n";
cin>>num;
cout<<"OUTPUT:\n";

for(i=1;i<=num;i++)
{
cout<<fib(i)<<" ";
}

return 0;
}

OUTPUT

C++ Program to obtain Fibonacci series using recursion with output

C++ Program to perform system shutdown and restart (Windows) [DEVCPP/GCC]

SYSTEM

This function invokes the command processor to execute a command.

PROGRAM

//Program to perform system shutdown and restart in Windows OS

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
        int n;
        char ch;

        cout<<"\n****MENU****\n";

        cout<<"\n1. SHUTDOWN";
        cout<<"\n2. RESTART";
        cout<<"\n3. EXIT";
     
        cout<<"\n\nENTER YOUR CHOICE: ";
        cin>>n;

        switch(n)
        {
                case 1:
                {
                        system("C:\\Windows\\System32\\Shutdown /s /t 60");
                        cout<<"\nSYSTEM WILL SHUT DOWN IN 1 MINUTE!";

                        cout<<"\n\nDO YOU WANT TO ABORT SHUTDOWN(Y/N)?";
                        cin>>ch;

                        if(ch=='y' || ch=='Y')
                        {
                                system("C:\\Windows\\System32\\Shutdown /a");
                                cout<<"\nSYSTEM SHUT DOWN IS ABORTED!";
                        }

                        break;
                }

                case 2:
                {
                        cout<<"\nSYSTEM WILL RESTART IN 1 MINUTE!";
                        system("C:\\Windows\\System32\\Shutdown /r /t 60");

                        cout<<"\n\nDO YOU WANT TO ABORT RESTART(Y/N)?";
                        cin>>ch;

                        if(ch=='y' || ch=='Y')
                        {
                                system("C:\\Windows\\System32\\Shutdown /a");
                                cout<<"\nSYSTEM RESTART IS ABORTED!";
                        }

                        break;
               }

                case 3:
                {
                       exit(0);
                }

                default:
                {
                        cout<<"\nWRONG CHOICE!";
                        break;
                }
        }

        return 0;
}

OUTPUT

C++ Program to perform system shutdown and restart with outputC++ Program to perform system shutdown and restart with output



C++ Program to illustrate memory allocation with malloc, calloc and realloc [DEVCPP/GCC]

MALLOC

It requests a block of memory from the heap. If the request is granted, a block of required size is allocated and a pointer to the beginning of block is returned. The content of allocated block is not initialized with any default value i.e. It contains garbage value upon allocation.
ptr=(type*)malloc(size) 
CALLOC

It allocates contiguous blocks of requested size for an array and initializes all of its bits to 0. The effective result of calloc is the allocation of (No of Blocks * Size of each Block) bytes.
ptr=(type*)calloc(n,size)
REALLOC

It changes the size of previously allocated memory object pointed by given pointer and reallocates it with given size. The contents of memory object remains unchanged upto the minimum of old and new sizes. If the current memory object cannot be enlarged to satisfy the request, the function may move the memory block to a new location. The old memory object is then freed. If no memory object can be acquired to accommodate the request, the object remains unchanged.
ptr=realloc(ptr,newsize)
FREE

A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated making it available again for further allocations.
free(ptr)
PROGRAM

//Program to illustrate memory allocation with malloc, calloc and realloc with their initialization and   deallocation

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
       int i,n,r,*cal,*mal;

       cout<<"\nENTER NUMBER OF VALUES REQUIRED: ";
       cin>>n;

       mal=(int*)malloc(n*sizeof(int));

       cout<<"\nMALLOC INITIAL ADDRESS:"<<mal;
   
       cout<<"\n\nMALLOC INITIAL MEMORY CONTENTS\n";
       for(i=0;i<n;i++)
       {
            cout<<"M["<<i<<"]: "<<mal[i]<<endl;
       }

       free(mal);

       cal=(int*)calloc(n,sizeof(int));

        cout<<"\nCALLOC INITIAL ADDRESS:"<<cal;
        cout<<"\n\nCALLOC INITIAL MEMORY CONTENTS\n";

        for(i=0;i<n;i++)
        {
              cout<<"C["<<i<<"]: "<<cal[i]<<endl;
        }

  cout<<"\nALTER NUMBER OF VALUES REQUIRED: ";
  cin>>r;

  realloc(cal,r*sizeof(int));

  cout<<"\n\nCALLOC MEMORY CONTENTS AFTER REALLOCATION\n";
  for(i=0;i<r;i++)
        {
              cout<<"C["<<i<<"]: "<<cal[i]<<endl;
        }
     
        free(cal);
        return 0;
}

OUTPUT

C++ Program to illustrate memory allocation with malloc, calloc and realloc with output


C++ Program to find factorial of a number using recursion [DEVCPP/GCC]

RECURSION

Recursion is an approach in which the function definition refers to itself. Each time the function calls itself with the slightly simpler version of the original problem.

PROGRAM

//Program to find factorial of a number using recursion.

# include <iostream>
using namespace std;

int fact(int n)
{
if(n==0)   //Base condition 
return 1;
else
return n * fact(n-1) ;
}

int main()
{
int num;

       cout<<"Enter a number:";
cin>>num;

       cout<<"Factorial:"<<fact(num);
return 0;
}

OUTPUT


C++ Program to find factorial of a number using recursion with output

FIND US ON FACEBOOK!