Thursday, July 26, 2012
Wednesday, April 18, 2012
Matrix ( Add, Sub, Multiply ,Transpose ) through a FUNCTIONS.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
int m,p,n,q,i,j;
//ADDITION
void add(int a[10][10],int b[][10])
{
clrscr();
int c[10][10];
cout<<"\nORIGINAL MATRIX 1:\t\t\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
cout<<"\nORIGINAL MATRIX 2:\t\t\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<" "<<b[i][j];
}
cout<<"\n";
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}
//SUBTRACTION
void sub(int a[][10],int b[][10])
{
clrscr();
int c[10][10];
cout<<"\nORIGINAL MATRIX 1:\t\t\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
cout<<"\nORIGINAL MATRIX 2:\t\t\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<" "<<b[i][j];
}
cout<<"\n";
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}
//MULTIPLY
void multiply(int a[][10],int b[][10])
{
clrscr();
int c[10][10];
cout<<"\nORIGINAL MATRIX 1:\t\t\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
cout<<"\n\nORIGINAL MATRIX 2:\t\t\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<" "<<b[i][j];
}
cout<<"\n";
}
cout<<"\nMatrix Multiplication:\n";
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
if(n==3||p==3)
{
c[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j];
}
else
{
c[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j];
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
cout<<" "<<c[i][j];
}
cout<<"\n";
}
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}
//TRANSPOSE
void trans(int a[][10],int b[][10])
{
clrscr();
cout<<"\nORIGINAL MATRIX 1:\t\t\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
cout<<"\nTranspose of 1st Matrix:\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<" "<<a[j][i];
}
cout<<"\n";
}
cout<<"\n\nORIGINAL MATRIX 2:\t\t\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<" "<<b[i][j];
}
cout<<"\n";
}
cout<<"\nTranspose of 2nd Matrix:\n";
for(i=0;i<q;i++)
{
for(j=0;j<p;j++)
{
cout<<" "<<b[j][i];
}
cout<<"\n";
}
cout<<"\n\nPress any key to go to main menu";
getch();
clrscr();
}
void main()
{
clrscr();
int a[10][10],b[10][10];
char ch;
cout<<"\nEnter the Size of MAtrix 1:\n";
cin>>m>>n;
cout<<"\nSize Entered: "<<m<<"X"<<n;
cout<<"\nEnter the size of Matrix 2:\n";
cin>>p>>q;
cout<<"\nSize Entered: "<<p<<"X"<<q;
cout<<"\n\nPress any key to Continue:";
getch();
clrscr();
do{
cout<<"\n---------------------------------------------";
cout<<"\n\t\t\tMENU\t\t\n";
cout<<"\n\t1.ADDITION";
cout<<"\n\t2.SUBTRACTION";
cout<<"\n\t3.MULTIPLY";
cout<<"\n\t4.TRANSPOSE";
cout<<"\n\t5.EXIT";
cout<<"\n----------------------------------------------------------";
cout<<"\nEnter your choice:";
cin>>ch;
clrscr();
//Conditions
if(ch=='1')
{
if(m==p &&n==q)
{
cout<<"\nMatrix are Comparable";
cout<<"\nEnter MATRIX 1:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
add(a,b);
}
else
{
cout<<"\nSORRY! MATRICES ARE NOT COMPARABLE";
cout<<"\n\nPlease try Again";
}
}
else if(ch=='2')
{
if(m==p &&n==q)
{
cout<<"\nMatrix are Comparable";
cout<<"\nEnter MATRIX 1\n:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
sub(a,b);
}
else
{
cout<<"\nSORRY! MATRICES ARE NOT COMPARABLE";
cout<<"\n\nPlease try Again";
}
}
else if(ch=='3')
{
if(n==p)
{
cout<<"\nMatrix are Multiplicable";
cout<<"\nEnter the Matrix:";
cout<<"\nEnter MATRIX 1\n:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
multiply(a,b);
}
else {
cout<<"\nSorry! Matrices are not Multiplicable! TRY AGAIN";
}
}
if(ch=='4')
{
cout<<"\nEnter the Matrix:";
cout<<"\nEnter MATRIX 1:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter MATRIX 2:\n";
for(i=0;i<p;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
trans(a,b);
}
else if(ch=='5')
{
cout<<"\n\nThanks for Watching :-)";
cout<<"\n\n\t\tABORTING!\n\t\t";
for(i=0;i<9;i++)
{
delay(500);
cout<<".";
}
exit(0);
}
}while(1);
getch();
}
Friday, March 9, 2012
Matrix ( Diagonal Sum )
The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,sum=0,a[3][3];
cout<<"\nEnter the Matrix of 3-3 size:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
cout<<"\n Sum is:"<<sum;
getch();
}
Fibonacci Series
The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,second=1,first=0,third;
cout<<"\nFIBINACOO SERIES:";
cout<<"\n"<<first;
cout<<"\n"<<second;
for(i=3;i<10;i++)
{
third=first+second;
cout<<third;
first=second;
second=third;
}
getch();
}
Prime or Composite number
The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int i,num,ans;
cout<<"\nEnter the number you want to check:";
cin>>num;
for(i=2;i<=(num-1);i++) //(i=2;i<=num/2;i++)
{
if(num%2==0)
{
ans=1;
break;
}
else
{
ans=0;
}
}
if(ans==1)
{
cout<<"\It is a composite number";
}
else {
cout<<"\nits a prime number";
}
getch();
}
Replacing space by a character " * "
The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char ch,name[20];
int i,l;
cout<<"\nEnter a String:";
gets(name);
l=strlen(name);
for(i=0;i<=l;i++)
{
if(name[i]==' ')
{
name[i]='*';
}
}
cout<<"\n "<<name;
getch();
}
Bubble Sort ( Ascending & Descending )
Code starts from next line :
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main()
{
int i, j, size, name[20], temp;
cout<<"\nHow many numbers:";
cin>>size;
cout<<"\nEnter the 1 d arrays:";
for(i = 0; i < size; i++)
{
cin>>name[i];
}
for(i = 0; i < size; i++)
{
for(j = 0; j < size-i-1; j++)
{
if(name[j] > name[j+1]) //just replace > by < for descending order.
{
temp = name[j];
name[j] = name[j+1];
name[j+1] = temp;
}
}
}
cout<<"\nAccending order is:";
for(i = 0; i < size; i++)
{
cout<<" "<<name[i];
}
getch();
}
Call by Reference
The code starts from next line :
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void swap(int &a,int &b);
void main()
{
clrscr();
int a,b;
cout<<"\nEnter the value of a:";
cin>>a;
cout<<"\nEnter the value of b:";
cin>>b;
swap(a,b);
cout<<"\nReplace value of a:"<<a;
cout<<"\nReplace dvalue of b:"<<b;
getch();
}
void swap(int &a,int &b) // Function starts here
{
int c;
c=a; //Swapping code
a=b;
b=c;
}
Subscribe to:
Posts (Atom)