09 June, 2013

C++ Program To Print Transpose Of A Matrix.

This Post Contains A C++ Program To Print Transpose Of A Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Print Transpose Of A Matrix.

/* Declaration Of Header Files */
#  include  <iostream.h>
#  include  <conio.h>

/* Start Of Main Program */
void main()
{
    
       /* Declaration Of Variables */
       int  i,  j,  r  =  0,  c  =  0, t = 0;
       int  a [ 10 ][ 10 ];
       clrscr();

       /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

/* Source Code For Computing Transpose Of Matrix  If & Only If Rows & Columns Are Equal */
if(r==c)
{
   for(i=0; i<r; i++)
   {
      for(j=0; j<i; j++)
      {
         t=a[i][j];
         a[i][j]=a[j][i];
         a[j][i]=t;
      }
   }
/* Printing The Output Onto The Screen/Console */
   cout<<" \n After Transposing : ";
   for(i=0; i<r; i++)
   {
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         cout<<a[i][j]<<"\t";
      }
   }
}
else
{
   cout<<" \n Transpose Is Not Possible";
}
getch();
}
/* End Of Main Program */


Output :

Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  2  3
4  5  6
7  8  9

After Transpose :
1  4  7
2  5  8
3  6  9




08 June, 2013

Preface Of The Blog.

Dear Friends,
    
         We fell delighted to present you the 'Most Commonly Asked Programs In 'C' & 'C++' Language' blog based on C & C++ Programming language. It comes up as a genuine aid to boost up your programming skills. All you need to do is thoroughly understand all it's aspect & practice the techniques mention in this blog to use it. Since we all know that, 'Practice Makes The Human Perfect'.
   
         The very structure of programs in this blog, reveals a profound study of concerned concepts/features. We have done our level best to incorporate complete syllabus in compliance to concept wise program distribution. Rather, to ease the study process & keep you aptly guided, we have provided a wide variety of programs with correct source code, algorithm & output. The blog is prepared in such a way that, any learner irrespective of  his/her background in field of programming or even one's level of status i.e. [Beginner, Mediocre or even Professional] can understand & grasp the contents of it in a very easy way. A sincere effort has been made after referring to many renowned prescribed books & data materials.
  
         Dear programmers, you are kindly advised to analyze, write & compile each & every program present in this blog. Thereafter, get them thoroughly understood & verified by your parents, tutors, teachers & lecturers and please make sure that pay enough attention to their requisite instructions or clarification, cause it'll help you to overcome any problems, queries or doubts in near future. Learning programming or it's techniques is not just understanding it's concepts/features, But also rectifying your mistakes which you make while doing so. Also be keen vigilant to perfect the structure of programming,
concepts related to algorithms, psuedocodes & flowcharts to add more to your skills.
         
        Hard work, consistent practice & planned studies are the keystone to greater heights in success. Our little guidance, would help you make a clean sweep as far as a bright success is concerned.
     
         Despite the best efforts, it is possible that some unintentional errors might have occurred. We shall acknowledge with gratitude any such errors if pointed out. Any suggestions for the improvement of this blog will be gratefully appreciated & will be definitely eliminated without any delay.

         You just believe in us & make a perfect use of our blog and see how you get the desired improvements & perfection in your programming skills.
  
         We hope you will bag the cherished success.

                                                                                                                                                       Author.

Please Visit The Following Articles Of This Blog Before Proceeding Further.
These Links Will Be Beneficial For You & Will Guide You For Better Understanding Of The 'C' & 'C++' Language.




07 June, 2013

C++ Program To Perform Merge Sort.

This Post Contains A C++ Program To Perform Merge Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Merge Sort', 'Array', 'Function', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Perform Merge Sort.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Function Declaration */
void Merge(int c[], int r[]);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[10], b[10], c[20];
int i=0, j=0, x, y;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array A : ";
cin>>x;
cout<<" \n Enter "<<x<<" Values For Array A : ";
for(i=0; i<x; i++)
{
   cin>>a[i];
}

cout<<" \n Enter Dimension For Array B : ";
cin>>y;
cout<<" \n Enter "<<y<<" Values For Array B : ";
for(i=0; i<y; i++)
{
   cin>>b[i];
}

/* Source Code For Computing Merge Sort */
for(i=0; i<x; i++)
c[i]=a[i];
while(j<y)
c[i++]=b[j++];
Merge(c,x+y);           // Function Call.
getch();
}
/* End Of Main Program */

/* Function Definition */
void Merge(int c[], int r[])
{

/* Declaration Of Local Variables Of Function */
int k, p, q, fl, fu, sl, su, size, d[20];
size=1;
while(size<r)
{
   fl=0;k=0;
   while(fl+size<r)
   {
      sl=fl+size;
      fu=sl-1;
      su=(sl+size-1<r)?sl+size-1:r-1;
      for(p=fl, q=sl; p<=fu && q<=su; k++)
      {
         if(c[p]<=c[q])
         {
           d[k]=c[p++];
         }
         else
         {
           d[k]=c[q++];
         }
      }
      for(; p<=fu; k++)
      {
         d[k]=c[p++];
      }
      for(; q<=su; k++)
      {
         d[k]=c[q++];
      }
      fl=su+1;
   }
   for(p=fl; k<r; p++)
   {
      d[k++]=c[p];
   }
   for(p=0; p<r; p++)
   {
      c[p]=d[p];
   }
   size*=2;
}
cout<<"\n";
for(q=0; q<r; q++)
   cout<<c[q]<<"\t";
}
/* End Of Function */


Output :

Enter Dimension For Array A : 4
Enter 4 Values For A :
01  2  83  45

Enter Dimension For Array B : 8
Enter 8 Values For B :
65  64  37  82  19  10  25  63

After Merge Sort :
01  10  19  25  29  37  45  63  64  65  82  83




C++ Program To Perform Shell Sort.

This Post Contains A C++ Program To Perform Shell Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Shell Sort', 'Array', 'Nested Loops', 'Functions' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Perform Shell Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Function Declaration */
void ShellSort(int a[], int b[], int d, intx);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim, x, a[20], b[5];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array : ";
cin>>dim;
cout<<" \n Enter "<<dim<<" Values For Array : ";
for(i=0; i<dim; i++)
{
   cin>>a[i];
}
/* Source Code For Computing Shell Sort */
cout<<" \n Enter Number Of Increments : ";
cin>>x;
cout<<" \n Enter Increments Last 1 : ";
for(i=0; i<x; i++)
{
   cin>>b[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0; i<dim; i++)
{
   cout<<a[i]<<"\t";
}
cout<<" \n Increments Are : ";
for(i=0; i<x; i++)
{
   cout<<b[i]<<"\t";
}

ShellSort(a, b, dim, x);         // Function Call.

getch();
}
/* End Of Main Program */

/* Function Definition */
void ShellSort(int a[], int b[], int d, intx)
{

/* Declaration Of Local Variables Of Function */
int j, k , l, y, inc, span;
for(j=0; j<x; j++)
{
   span=b[j];
   for(k=span; k<d; k++)
   {
      y=a[k];
      for(l=k-span; l>=0&&y<a[l]; l-=span)
       a[l+span]=a[l];
      a[l+span]=y;
   }
}

/* Printing The Output Onto The Screen/Console */
cout<<" \n After Sorting Array Is : ";
for(j=0; j<d; j++)
{
   cout<<a[j]<<"\t";
}
}
/* End Of Function */
Output :

Enter Dimension : 10

Enter 10 Values For Array :
91  28  73  46  50  19  82  37  64  05

Enter Number Of Increments : 3

Enter Increments Last 1 : 4  2  1

Values Of Array Are :
91  28  73  46  50  19  82  37  64  05

Increments : 4  2  1

After Sorting Array Is :
05  19  28  37  46  50  64  73  82  91




C++ Program To Print An Array Using Pointer.

This Post Contains A C++ Program To Print An Array Using Pointer With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays' & 'Pointers' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
  


  • A linear array is a list of finite number 'n' of homogeneous data elements.
  • The elements of array are referenced by index set consisting of 'n' consecutive numbers.
  • The elements of array are stored respectively in successive memory locations.
  • The number 'n' of elements is called as 'length' or 'size' of the array.
  • Length Of Array = UB - LB + 1.
          Where UB = largest index i.e. upper bound.
                     LB = smallest index i.e. lower bound.
  • The elements of array A may be denoted by subscript notation.
          A1A2A3,  .......... An
  • But in languages generally bracket notation is used.
          A[0], A[1], A[2], .......... A[n]

   
C++ Program To Print An Array Using Pointer.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, a[10], *ptr;
clrscr();

/* Asking For The Input From User */
cout<<" \n Please Enter 10 Numbers For An Array 'A' : ";
for(i=0; i<10; i++)
{
   cin>>a[i];
}

/* Printing The Output Onto The Screen/Console */
ptr=&a;                                 // Or You Can Also Denote ptr=&a[0];
cout<<" \n The Entered Numbers Are : \n ";
for(i=0; i<10; i++)
{
   cout<<*ptr<<"\t";
   ptr++;
}
getch();
}
/* End Of Main Program */


Output :

Please Enter 10 Numbers For An Array 'A' :
1  2  3  4  5  6  7  8  9  10

The Entered Numbers Are :
1  2  3  4  5  6  7  8  9  10

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

C++ Program To Print An Array Onto Screen/Console.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, a[10];
clrscr();

/* Asking For The Input From User */
cout<<" \n Please Enter 10 Numbers For An Array 'A' : ";
for(i=0; i<10; i++)
{
   cin>>a[i];
}

/* Printing The Output Onto The Screen/Console */

cout<<" \n The Entered Numbers Are : \n ";
for(i=0; i<10; i++)
{
   cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */


Output :

Please Enter 10 Numbers For An Array 'A' :
1  2  3  4  5  6  7  8  9  10

The Entered Numbers Are :
1  2  3  4  5  6  7  8  9  10




C Program To Perform Character Design : #04.

This Post Contains A C Program To Perform Character Design With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Nested For-Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Perform Character Design : #04.
/* Declaration Of Header Files */
#include <stdio.h>
#include <conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j;

clrscr();

for(i=5; i>=1; i--)
{
   for(j=1; j<=i; j++)
   {    
      printf("%d\t",j % 2);
   }
   printf("\n");
}

getch();

}
/* End Of Main Program */

Output:
1  0  1  0  1
1  0  1  0
1  0  1
1  0
1

C++ Program To Perform Character Design : #04.
/* Declaration Of Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j;

clrscr();

for(i=5; i>=1; i--)
{
   for(j=1; j<=i; j++)
   {    
      cout  <<  j % 2  <<  "\t";
   }
   cout  <<  "\n";
}

getch();

}
/* End Of Main Program */

Output:
1  0  1  0  1
1  0  1  0
1  0  1
1  0
1




C++ Program To Check Whether The User Entered Integer Is Palindrome Or Not.

This Post Contains A C++ Program To Check Whether The User Entered Integer Is Palindrome With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'While-Loop' & 'If...else' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Check Whether The User Entered Integer Is Palindrome Or Not.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int Num, Rem=0, Quo=0, temp=0;
clrscr();

/* Asking For The Input From User */
cout<<" \n Please Enter A Number : ";
cin>>Num;

/* Source Code For Checking Integer As Palindrome */
Quo=Num;
while(Quo!=0)
{
   Rem = Quo % 10;
   temp = (temp*10) + Rem;
   Quo = Quo / 10;
}
if(Num==temp)
{
cout<<" \n The Number "<<Num<<" Is Palindrome ";
}
else
{
cout<<" \n The Number "<<Num<<" Is Not Palindrome ";
}
getch();
}
/* End Of Main Program */

Output :

Please Enter A Number : 11

The Number 11 Is Palindrome

Please Enter A Number : 21

The Number 21 Is Not Palindrome




04 June, 2013

C Program To Perform Character Design : #03.

This Post Contains A C Program To Perform Character Design With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Nested For-Loops' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Perform Character Design :#03.
/* Declaration Of Header Files */
#include <stdio.h>
#include <conio.h>

/* Start Of Main Program */
int main()
{

 /* Declaration Of Variables */
 int num, row = 1, col = 0, space = 0;

 /* Asking For The Input From User */
 printf(" \n Enter Number of Rows For Design : ");
 scanf("%d",&num);

 /* Source Code For Computing ----- */
 for(; num>=1; num--, row++)
 {
  for(space = row; space>1; space--)
     printf(" ");

  for(col=1; col<=num; col++)
    printf("#");

  printf("\n");
 }

 getch();

 return 0;

}
/* End Of Main Program */

Output:
Enter Number of Rows For Design :  5
# # # # #
 # # # #
  # # #
   # #
    #

C++ Program To Perform Character Design :#03.
/* Declaration Of Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
int main()
{

 /* Declaration Of Variables */
 int num, row = 1, col = 0, space = 0;

 /* Asking For The Input From User */
 cout  <<  " \n Enter Number of Rows For Design : ";
 cin  >>  num;

 /* Source Code For Computing ----- */
 for(; num>=1; num--, row++)
 {
  for(space = row; space>1; space--)
     cout  <<  " ";

  for(col=1; col<=num; col++)
    cout  <<  "#";

  cout  <<  "\n";
 }

 getch();

 return 0;

}
/* End Of Main Program */

Output:
Enter Number of Rows For Design :  5
# # # # #
 # # # #
  # # #
   # #
    #




C++ Program To Perform Heap Sort.

This Post Contains A C++ Program To Perform Heap Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Heap Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Heap Sort.
 
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
void hsort(int x[], int n);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim;
int a[20];
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;

cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
   cin>>a[i];
}

cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

hsort(a, dim);        // Call To The Function.

cout<<" \n After Heap Sort : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

getch();
}
/* End Of Main Program */
/* Source Code For Computing Heap Sort */
/* Start Of Heap Sort Function */
void hsort(int x[], int n)
{
int i, s, f, elt, ivalue;
for(i=1;i<n;i++)
{
   elt=x[i];
   s=i;
   f=(s-1)/2;
   while(s>0  && x[f]<elt)
   {
      x[s]=x[f];
      s=f;
      f=(s-1)/2;
   }
   x[s]=elt;
}
for(i=n-1;i>0;i--)
{
   ivalue=x[i];
   x[i]=x[0];
   f=0;
   if(i==1)
   {
      s=-1;
   }
   else
   {
      s=1;
   }
   if( (i>2) && (x[2]>x[1]) )
   {
      s=2;
   }
   while( (s>=0) && (ivalue<x[s]) )
   {
      x[f]=x[s];
      f=s;
      s=2*f+1;
      if( (s+1<=i-1) && (x[s]<x[s+1]) )
      s=s+1;
      if(s>i-1)
      s=-1;
   }
   x[f]=ivalue;
}
}
/* End Of Heap Sort Function */

 
Output :

Enter Dimension :  5

Enter Values For Array :
3  1  5  2  4

After Heap Sort :
1  2  3  4  5




C++ Program To Perform Quick Sort.

This Post Contains A C++ Program To Perform Quick Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Quick Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Quick Sort.


/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
int dim, a[10];
void qsort(int, int);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, l, r;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;

cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
   cin>>a[i];
}

cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

l=0;
r=dim-1;
qsort(l, r);
cout<<" \n After Quick Sort : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

getch();
}
/* Source Code For Computing Quick Sort */
void qsort(int left, int right)
{
int i, j, p, temp;
if(right>left)
{
   i=left;
   j=right;
   p=a[left];
   while(right>left)
   {
      do
      {
          i++;
      }
      while( (a[i]<=p) && (i<=right) );
      while( (a[j]>=p) && (j>left) )
      {
          --j;
      }
      if(j>i)
      {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;
      }
   }
   temp=a[left];
   a[left]=a[j];
   a[j]=temp;
   qsort(left, j-1);
   qsort(i, right);
}
}
/* End Of Main Program */

 
Output :
Enter Dimension :  5

Enter Values For Array :
3  1  5  2  4

After Quick Sort :
1  2  3  4  5




Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive