Computing Matrices
Language: C++
Author: Aamir Mustafa
Introduction:
In this article, two simple matrices will be computed. We will take values for two matrices and then we will add and subtract them.
First of all we will write a Matrix class. This class contains one member variable called matrix, it is two dimensional and we declare it in private part of the class.
After that we declare two members functions in the public part of the class so we can access these member functions out of the class. These are get(), display() and Compute() functions.
get() function will get values through loops.
display() will show values of matrices through loops.
Compute() will compute the matrices for subtraction and addition.
After that we write two operators and here we overload them which is called operator overloading.
First of all we will write the name of Class which is Matrix, after that the operator key word and then + or – for adding and subtracting matrices. After that we write const keyword and then the name of class and variable.
Matrix operator + (const Matrix& m)
After that our complete class looks like.
//including necessary libraries
#include<iostream>
#include<conio.h>
//declaring Matrix class
class Matrix
{
private:
int matrix[2][2]; //members variables
public:
void get() //member function get() which gets values for matrices
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
cin>>matrix[i][j];
}
void Display() //this member function displays computed values of two matrices.
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
cout<<matrix[i][j]<<"\t";
}
Matrix operator-(const Matrix& m)//operator overloading
{
Matrix mn; //creating an object of class Matrix
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
mn.matrix[i][j]=matrix[i][j]-m.matrix[i][j];
return mn;
}
Matrix operator + (const Matrix& m)//operator overloading
{
Matrix mn; //creating an object of class Matrix
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
mn.matrix[i][j]=matrix[i][j]+m.matrix[i][j];
return mn;
}
void Compute() //member function for computing matrices.
{
Matrix Mat1,Mat2,Mat3; //creating three objects
cout<<"\nEnter elements into the first matrix:\n";
Mat1.get(); //getting values of first matrix
cout<<"\nEnter elements into the second matrix:\n";
Mat2.get(); //getting values of second matrix
Mat3=Mat1+Mat2; //adding two matrices and saving them in mat3
cout<<"\n\nThe Matrix addition is:",Mat3.Display(),"\n";
Mat3=Mat1-Mat2;
cout<<"\n\nThe Matrix subtraction is:",Mat3.Display(),"\n";
}
};
After this we write our main function. Here we will create a matrix object m and then access Compute function through this object m.
main()
{
Matrix m; //creating an object
m.Compute();
getche();
}
Comments are needed.
Getting Properties of an image[VC++.NET]
Getting Properties of an Image
Language: VC++.NET
Author: Aamir Mustafa
Introduction:
This simple article will show how to get image properties. In this article I will load a photo and then its property will be shown. it will display all the properties of an image like name, image type, image size, its resolution.
How to show property:
Create an empty project and add following controls on the form.
Control | Properties |
Form | Name=Form1 Text= Properties Width=554 Height=224 |
Button | Name= btnLoad Text= Load Photo |
ListView | Name= lstProperties Width=329 Height=149 Scrollable=False Font Style=Bold Font Size=8 ForeColor=Coral |
PictureBox | Name= pic Width=117 Height=149 BorderStyle=FixedSingle SizeMode=StretchImage |
After doing that go to view code and add following code below in public area of class Form1.
public:
Bitmap ^bitmapImage;
String ^imageFile;
After that add following just below the above code
Here we are initializing the List View so when first our program runs so we can see two columns, one for Property and second for Property value. Both columns will be added when we first launch our program. Both columns have equal width. Type of View will be detail so we can show properties in detail like a table and we will set Grid Lines to true so that we can show grid lines. We will write following code in Form1 constructor.
Form1(void)
{
InitializeComponent();
lstProperties->View=View::Details;
lstProperties->Columns->Add("Property",Convert::ToInt32(lstProperties->Width*50/100));
lstProperties->Columns->Add("Value",Convert::ToInt32(lstProperties->Width*50/100));
lstProperties->GridLines=true;
}
Now write a function called GetProperties which gets the properties of an image and shows them in list view.
private:
void GetProperties()
{
lstProperties->Items->Clear();
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
lstProperties->Items->Add(gcnew ListViewItem(gcnew array
}
After that add following code to the load button’s click event. This opens a dialog to load picture from the local disk.
private: System::Void btnLoad_Click(System::Object^ sender, System::EventArgs^ e)
{
FileDialog ^fileDialog = gcnew OpenFileDialog();
fileDialog->Filter = "JPG (*.jpg)|*.jpg| Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpeg)|*.jpeg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif|All Files (*.*)|*.*";
fileDialog->ShowDialog(this);
pic->SizeMode = PictureBoxSizeMode::StretchImage;
imageFile = fileDialog->FileName;
if (imageFile == System::String::Empty)
return;
pic->Image = Image::FromFile(imageFile);
bitmapImage = gcnew Bitmap(imageFile);
GetProperties();
}