Thursday, October 20, 2011

C++ program to print matrix difference


//C++ program to subtract two matrices
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][4],matrix2[3][4],matrix3[3][4];
int i,j;
cout<<"\nEnter numbers to fill matrix 1\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix1[i][j];
  }
}
cout<<"\nEnter numbers to fill matrix 2\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix2[i][j];
  }
}

cout<<"Original Matrix 1:\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix1[i][j]<<" ";
  }
 cout<<"\n";
}
cout<<"\n\nOriginal Matrix 2:\n";

for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix2[i][j]<<" ";
  }
cout<<"\n";
}
cout<<"\n\nDifference of Matrix 1 and Matrix 2:\n";

for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   matrix3[i][j]=matrix1[i][j]-matrix2[i][j];
   cout<<matrix3[i][j]<<" ";
  }
cout<<"\n";
}

getch();
}

C++ program to print sum of two matrices


//C++ program to sum two matrices
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][4],matrix2[3][4],matrix3[3][4];
int i,j;
cout<<"\nEnter numbers to fill matrix 1\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix1[i][j];
  }
}
cout<<"\nEnter numbers to fill matrix 2\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix2[i][j];
  }
}

cout<<"Original Matrix 1:\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix1[i][j]<<" ";
  }
 cout<<"\n";
}
cout<<"\n\nOriginal Matrix 2:\n";

for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix2[i][j]<<" ";
  }
cout<<"\n";
}
cout<<"\n\nSum of Matrix 1 and Matrix 2:\n";

for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
   cout<<matrix3[i][j]<<" ";
  }
cout<<"\n";
}

getch();
}

C program to print transpose matrix


//C++ program to print transpose of a matrix
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int matrix[3][4],transpose_matrix[4][3];
int i,j;
cout<<"\nEnter numbers to fill matrix\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix[i][j];
   transpose_matrix[j][i]=matrix[i][j];
  }
}
cout<<"Original Matrix:\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix[i][j]<<" ";
  }
 cout<<"\n";
}
cout<<"\n\nTranspose of Matrix:\n";

for(i=0;i<4;i++)
{
  for(j=0;j<3;j++)
  {
   cout<<transpose_matrix[i][j]<<" ";
  }
cout<<"\n";
}

getch();
}

Tuesday, October 18, 2011

Tutorial on Prefix and Postfix in C programming


The prefix and postfix notations in C++ are really simple for an expert but extremely difficult for novice to understand. In this tutorial, you can easily understand the working of increment and decrement operators in prefix and postfix notation.

Consider the following codes
Code 1
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int b,i=10;
b=i++ + ++i;
cout<<b;
getch();
}

OUTPUT will be 22.

Code 2
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=10;
i=i++ + ++i;
cout<<i;
getch();
}

OUTPUT will be 23.

Wonder how? Do not wonder. I will explain how it works.

Explanation: Prefix notation is always given first priority over postfix notation to execute. Just see the priority setting for each operation and understand how it works:

Code 1
b=3(i++)4 +2 (++i)1;

See the priority orders as 1, 2, 3 and 4. First prefix i increases and becomes 11. Secondly, + operator adds 11+11 and thirdly = operator assigns 22 to b and lastly postfix i increases to 12. So finally b=22 and i=12.

Code 2
i=3(i++)4 +2 (++i)1;

See the priority orders as 1, 2, 3 and 4. First prefix i increases and becomes 11. Secondly, + operator adds 11+11 and thirdly = operator assigns 22 to i and lastly postfix i increases to 23. So finally i=23.

I hope you understood the working of prefix and postfix in the above examples. Share it if you liked this tutorial.

Monday, October 17, 2011

C++ program to check a palindrome string

//C++ program to check a palindrome string

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j,flag=0;
char string[20];
cout<<"Enter A String\n";
cin>>string;
for(i=0;string[i]!='\0';i++);
for(j=0,--i;i>=0;--i,++j)
{
if(string[i]!=string[j])
{
flag=1;
break;
}
}
if(flag)
cout<<"Not a palindrome string";
else
cout<<"Palindrome String"; 
getch();
}


OUTPUT:



C++ program to calculate length of a string


//C++ program to calculate the length of the string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i;
char string[20];
cout<<"Enter A String\n";
gets(string);
for(i=0;string[i]!='\0';i++);
cout<<"The length of the string is"<<i;
getch();
}

Output:


C++ program to print reverse of a string


//C++ program to print reverse of a string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i;
char string[20];
cout<<"Enter A String\n";
gets(string);
for(i=0;string[i]!='\0';i++);
for(i=i-1;i>=0;i--)
{
cout<<string[i];
}
getch();
}


Output:


Sunday, October 16, 2011

C program to print pyramid of asterisks

//C++ program to print pyramid of asterisks
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,l,sp1=4,sp2=-1;
for(i=1;i<=5;i++,sp1--,sp2=sp2+2)
{
for(j=1;j<=sp1;j++)
{
cout<<" ";
}
if(i==1)
{
cout<<"*";
}
if(i==2||i==3||i==4)
{
cout<<"*";
for(k=1;k<=sp2;k++)
{
cout<<" ";
}
cout<<"*";
}
if(i==5)
{
for(l=1;l<=9;l++)
{
cout<<"*";
}
}
 cout<<"\n";
}
getch();
}


The output:

C++ program to print pattern of numbers with each line reversing the previous line

//C++ program to print pattern of numbers with each line reversing the previous line
#include<iostream.h>
#include<conio.h>
void main()
{
int i,k,t,a=1,s=0;
clrscr();
for(i=6;i>=1; i--)
{
  if(i%2==0)
  {
    for(k=1;k<=i;k++)
{
cout<<a<<"   ";
++a;
}
   cout<<"\n";
   t=a;
   }
 if(i%2!=0)
 {
  for(s=t+i-1;s>=t;s--,a++)
  {
  cout<<s<<"   ";
  }
 cout<<"\n";
 }
     
}
getch();
}

The output will be as following: