Thursday, 27 August 2015
Saturday, 22 August 2015
Matrix multiplication in C++
You have often learnt the method of matrix multiplication in Mathematics.
Matrix multiplication
Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.
Source code
#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();
void multiplication();
int mat1 [3][3], mat2[3][3],mat3[3][3], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 3*3 matrix multiplication."<<endl<<endl;
cout<<"Press 2 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
multiplication();
break;
case 2:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}
goto x;
}
}
void multiplication()
{
cout<<"\nEnter values for first 3 x 3 matrix:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for (j = 0 ; j <= 2 ; j++ )
cin>>mat1 [i][j];
}
cout<<"\n Enter values for second 3 x 3 matrix:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cin>>mat2[i][j];
}
cout<<"\n The first 3 x 3 matrix entered by you is:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<< mat1[i][j]<<"\t";
cout<<endl<<endl;
}
cout<<"\n the second 3 x 3 matrix entered :\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<< mat2[i][j]<<"\t" ;
cout<<endl<<endl;
}
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
{
sum = 0;
for ( k = 0 ; k <=2 ; k++ )
sum = sum + mat1 [i][k] * mat2[k][j];
mat3[i][j] = sum ;
}
}
cout<<"\nThe product of the above two matrices is:\n";
for ( i = 0 ;i<= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
Output
Labels:
Arrays,
C,
C++,
C++ coding,
C++ programming,
C++ programs,
Coding,
CPP,
khgamujtaba,
programming,
Programs
Monday, 17 August 2015
Matrix addition and subtraction in C++
You are well aware of the matrix addition and subtraction in Mathematics.
Matrix addition
Matrix subtraction
Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.
Source code
#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();
int mat1[2][2], mat2[2][2],mat3[2][2], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 2*2 matrix addition."<<endl<<endl;
cout<<"Press 2 for 2*2 matrix subtraction."<<endl<<endl;
cout<<"Press 3 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}
goto x;
}
}
void addition()
{
cout<<"\nEnter values for first 2 x 2 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for (j = 0 ; j <= 1 ; j++ )
cin>>mat1 [i][j];
}
cout<<"\n Enter values for second 2 x 2 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j=0 ; j<=1 ; j++ )
cin>>mat2[i][j];
}
cout<<"\n The first 2 x 2 matrix entered by you is:\n";
for ( i=0 ; i<=1 ; i++ )
{
for ( j=0 ; j<=1 ; j++ )
cout<< mat1[i][j]<<"\t";
cout<<endl<<endl;
}
cout<<"\n the second 2 x 2 matrix entered :\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cout<< mat2[i][j]<<"\t";
cout<<endl<<endl;
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
cout<<"The addition of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
void subtraction()
{
cout<<"\nEnter values for first 2 x 2 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for (j = 0 ; j <= 1 ; j++ )
cin>>mat1 [i][j];
}
cout<<"\n Enter values for second 3 x 3 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cin>>mat2[i][j];
}
cout<<"\n The first 2 x 2 matrix entered by you is:\n"<<endl;
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cout<< mat1[i][j]<<"\t";
cout<<endl<<endl;
}
cout<<"\n the second 2 x 2 matrix entered :\n"<<endl;
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cout<< mat2[i][j]<<"\t";
cout<<endl<<endl;
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
cout<<"The subtraction of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
Output
Saturday, 15 August 2015
Initialization of 2-d arrays in C++
Initializing 2-dimensional array
The 2-D arrays which are declared by the keyword int and able to store integer values are called two dimensional integer arrays. The process of assigning values during declaration is called initialization. TheseArrays can be initialized by putting the curly braces around each row separating by a comma also each element of a matrix should be separated by a comma.
Example:
int mat[3][3]={ {2,3},{4,5},{6,7} }
Initializing 2-d character array
The 2-D arrays which are declared by the keyword char and able to store characters are called two dimensional character arrays. 2-D character array can be initialized by putting the curly braces around each row as well as apostrophe around every alphabet.
Example:
char character[3][3]={'b', 'a' ,'t' ,'f', 'a' ,'t' ,'m' ,'a' ,'t'};
reference
The reference of this article is taken from
In my next blog inshAllah,I will post an important example of 2-d arrays that will be very helpful in understanding this concept.So,stay tuned.
Labels:
Arrays,
C,
C++,
C++ coding,
C++ programming,
C++ programs,
Coding,
CPP,
Programs
Thursday, 13 August 2015
C++ The Complete Reference
Introduction
C++ The Complete Reference
Master programmer and best-selling author Herb Schildt has updated and expanded his classic reference to C++. Using expertly crafted explanations, insider tips, and hundreds of examples, Schildt explains and demonstrates every aspect of C++. Inside you'll find details on the entire C++ language, including its keywords, operators, preprocessor directives, and libraries. There is even a synopsis of the extended keywords used for .NET programming. Of course, everything is presented in the clear, crisp, uncompromising style that has made Herb Schildt the choice of millions. Whether you're a beginning programmer or a seasoned pro, the answers to all your C++ questions can be found in this lasting resource.
Detailed coverage includes:
- Data types and operators
- Control statements
- Functions
- Classes and objects
- Constructors and destructors
- Function and operator overloading
- Inheritance
- Virtual functions
- Namespaces
- Templates
- Exception handling
- The I/O library
- The Standard Template Library (STL)
- Containers, algorithms, and iterators
- Principles of object-oriented programming (OOP)
- Runtime type ID (RTTI)
- The preprocessor
- Much, much more
Author:
Herbert Schildt
Date of publishing:
November 19, 2002
Publisher:
McGraw-Hill Education
Language:
English
Genre:
Programming
ISBN:
- ISBN-10: 0072226803
- ISBN-13: 978-0072226805
Rating:
4.3/5.0 stars
Votes:
37
Labels:
books,
C,
C++,
C++ books,
computer,
CPP,
khgamujtaba,
programming
Wednesday, 12 August 2015
Two-dimensional arrays in C++
Definition
In C++ Two Dimensional array in C++ is an array that consists of more than one rows and more than one column. In 2-D array each element is refer by two indexes. Elements stored in these Arrays in the form of matrices. The first index shows a row of the matrix and the second index shows the column of the matrix.
Syntax
data type name of array[no. of rows][no. of columns]
For example,
int item[4][5];
Accessing each location
In order to store values in a C++ two dimensional arrays the programmer have to specified the number of row and the number of column of a matrix. To access each individual location of a matrix to store the values the user have to provide exact number of row and number of column.
Example:
int matrix[2][2];
matrix[0][0]=20;
matrix[0][1]=25;
matrix[1][0]=30;
matrix[1][1]=35;Entering data
Nested loop is used to enter data in 2-D arrays. It depends upon the programmer which loop he wants to use it could be While loop or it could be a For loop. The outer loop acts as the number of rows of a matrix and the inner loop acts as the number of columns of a matrix.
The reference of this article has been taken from
Example:
#include<iostream.h>#include<conio.h>main(){int matrix [5] [5];for (int m1=0 ; m1<5 ; m1++){for (int m2=0 ; m2<5 ; m2++){Matrix [m1] [m2] = 5 ;}}getch();}
|
Labels:
Arrays,
C,
C++,
C++ coding,
C++ programs,
Coding,
computer,
khgamujtaba,
programming
Subscribe to:
Posts (Atom)






