This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

Monday, 21 October 2013

C++ program to Add two Matrices

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], b[3][3], c[3][3], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of a["<<i+1<<"]["<<j+1<<"]...";
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of b["<<i+1<<"]["<<j+1<<"]...";
cin>>b[i][j];
}
}
cout<<"\n";
cout<<"Resultant Matrix\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}

C++ program to Subtract two Matrices

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], b[3][3], c[3][3], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of a["<<i+1<<"]["<<j+1<<"]...";
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\nEnter the value of b["<<i+1<<"]["<<j+1<<"]...";
cin>>b[i][j];
}
}
cout<<"\n";
cout<<"Resultant Matrix\n\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}

Monday, 12 August 2013

C program using Dynamic Memory Allocation to Subtract two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p,q,r,s,*m1, *m2, *a;
clrscr();
printf("Enter the order of matrix A");
printf("\n");
printf("m=");
scanf("%d", &p);
printf("n=");
scanf("%d", &q);
printf("\nEnter the order of matrix B");
printf("\n");
printf("m=");
scanf("%d", &r);
printf("n=");
scanf("%d", &s);
if(q==r)
{
m1=(int*)calloc(p*q, sizeof(int));
m2=(int*)calloc(r*s, sizeof(int));
a=(int*)calloc(p*s, sizeof(int));
printf("\nEnter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",(m1+i*q+j));
}
}
printf("\nEnter the elements of matrix B:");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",(m2+i*r+j));
}
}
printf("\nSubtraction:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
*(a+i*q+j)=*(m1+i*q+j)-(*(m2+i*r+j));
}
}
printf("\nDifference of the Matrices A and B:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",*(a+i*q+j));
}
}
}
getch();
}


C program using Dynamic Memory Allocation to Add two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p,q,r,s,*m1, *m2, *a;
clrscr();
printf("Enter the order of matrix A");
printf("\n");
printf("m=");
scanf("%d", &p);
printf("n=");
scanf("%d", &q);
printf("\nEnter the order of matrix B");
printf("\n");
printf("m=");
scanf("%d", &r);
printf("n=");
scanf("%d", &s);
if(q==r)
{
m1=(int*)calloc(p*q, sizeof(int));
m2=(int*)calloc(r*s, sizeof(int));
a=(int*)calloc(p*s, sizeof(int));
printf("\nEnter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",(m1+i*q+j));
}
}
printf("\nEnter the elements of matrix B:");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",(m2+i*r+j));
}
}
printf("\nAddition:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
*(a+i*q+j)=*(m1+i*q+j)+(*(m2+i*r+j));
}
}
printf("\nSum of the Matrices A and B:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",*(a+i*q+j));
}
}
}
getch();
}