Search from Cycling Blog

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.

0 comments  

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 { "Name", System::IO::Path::GetFileNameWithoutExtension(imageFile) }));

lstProperties->Items->Add(gcnew ListViewItem(gcnew array {"Width",bitmapImage->Width.ToString()+" Pixels"}));

lstProperties->Items->Add(gcnew ListViewItem(gcnew array {"Height",bitmapImage->Height.ToString()+" Pixels"}));

lstProperties->Items->Add(gcnew ListViewItem(gcnew array {"Vertical Resolution",bitmapImage->VerticalResolution.ToString()+" Pixels/Inch"}));

lstProperties->Items->Add(gcnew ListViewItem(gcnew array {"Horizontal Resolution",bitmapImage->HorizontalResolution.ToString()+" Pixels/Inch"}));

lstProperties->Items->Add(gcnew ListViewItem(gcnew array {"Pixel Format",bitmapImage->PixelFormat.ToString()}));

lstProperties->Items->Add(gcnew ListViewItem(gcnew array {"Pallete Flags",bitmapImage->Palette->Flags.ToString()}));

}

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();

}

0 comments  

Getting Properties of an image[VB.NET]

Getting Properties of an Image

Language: VB.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 Public Class Form1

Dim bitmapImage As Bitmap

Dim imageFile As String

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.

Public Sub New()

InitializeComponent()

lstProperties.View = View.Details

lstProperties.View = View.Details

lstProperties.Columns.Add("Property", Convert.ToInt32(lstProperties.Width * 50 \ 100))

lstProperties.Columns.Add("Value", Convert.ToInt32(lstProperties.Width * 45 \ 100))

lstProperties.GridLines = True

End Sub

Now write a function called GetProperties which gets the properties of an image and shows them in list view.

Private Sub GetProperties()

lstProperties.Items.Clear() ' clear the list view

lstProperties.Items.Add(New ListViewItem(New String() {"Name", System.IO.Path.GetFileNameWithoutExtension(imageFile)})) 'this will show name of image but not with extension

lstProperties.Items.Add(New ListViewItem(New String() {"Type", System.IO.Path.GetExtension(imageFile)})) 'this will show image type like JPEG

lstProperties.Items.Add(New ListViewItem(New String() {"Size", New System.IO.FileInfo(imageFile).Length.ToString() & " Bytes"})) 'this will show image size in bytes

lstProperties.Items.Add(New ListViewItem(New String() {"Width", pic.Width.ToString() & " Pixels"})) 'this will show image width

lstProperties.Items.Add(New ListViewItem(New String() {"Height", pic.Height.ToString() & " Pixels"})) 'this will show image height

lstProperties.Items.Add(New ListViewItem(New String() {"Vertical Resolution", bitmapImage.VerticalResolution.ToString() & " Pixels/Inch"})) 'this will show Vertical Resolution

lstProperties.Items.Add(New ListViewItem(New String() {"Horizontal Resolution", bitmapImage.HorizontalResolution.ToString() & " Pixels/Inch"})) 'this will show Horizontal Resolution

lstProperties.Items.Add(New ListViewItem(New String() {"Pixel Format", bitmapImage.PixelFormat.ToString()})) 'this will show pixel format.

lstProperties.Items.Add(New ListViewItem(New String() {"Palette Flags", bitmapImage.Palette.Flags.ToString()})) 'this will show pallete flags

End Sub

After that add following code to the load button’s click event. This opens a dialog to load picture from the local disk.

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

Dim fileDialog As FileDialog = New 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(Me)

pic.SizeMode = PictureBoxSizeMode.StretchImage

imageFile = fileDialog.FileName

If imageFile = String.Empty Then

Return

End If

pic.Image = Image.FromFile(imageFile)

bitmapImage = New Bitmap(imageFile)

GetProperties() ‘call GetProperties method

End Sub

Note:

If you want to get size of the image in Kilo Bytes then you can do it as follow

System.IO.FileInfo(imageFile).Length.ToString()\1024 & lstProperties.Items.Add(New ListViewItem(New String() {"Size", New" KB"})) 'this will show image size in kilo bytes


1 comments  

Converting decimal to binary[VC++.NET]

Converting decimal number into Binary number

Language: Visual C++.NET

Author: Aamir Mustafa

Introduction:

This article will show you how to convert a decimal number into binary number.

How to Convert:

This is Visual C++.Net so here is some different technique to create a project. I will use here Windows Form Application Project.

Follow these steps to create a project

File->New->New Project

Another window will open,in this window select CLR and then choose

Windows Form Application.

After that enter a name for your project and click OK


A new project will be created and a new form will be front of you.


On the form, add a button, two textboxes

  1. btnBinary
  2. txtNum
  3. txtBinary

We will take input through txtNum and show binary number into txtBinary and we will use btnBinary for converting decimal into Binary.

Double click on btnBinary

Write following code in btnBinary Click event.

[Code]

private: System::Void btnBinary_Click(System::Object^ sender, System::EventArgs^ e)

{

try

{

long num=0;

num=Convert::ToInt64(txtNum->Text);

txtBinary->Text=Convert::ToString(num,2);

}

catch(Exception ^ex)

{

MessageBox::Show("Please enter the number","Binary",MessageBoxButtons::OK,MessageBoxIcon::Information);

txtNum->Focus();

}

}

0 comments  

Converting decimal number into Octal number[C#.NET]

Converting decimal number into octal number

Language: C#.NET

Author: Aamir Mustafa

Introduction:

This article will show you how to convert a decimal number into octal number.

How to Use:

Create an empty project

On the form, add a button, two textboxes

  1. btnOctal
  2. txtNum
  3. txtOctal

We will take input through txtNum and show binary number into txtOctal and we will use btnOctal for converting decimal into octal.

Write following code in btnOctal Click event.

Code:

private void btnOctal_Click(object sender, EventArgs e)

{

try

{

long num = 0; //number that we will take through input

num = Convert.ToInt64(txtNum.Text);

txtOctal.Text = Convert.ToString(num, 8); //here 8 means converting to octal

}

catch (Exception ex)

{

MessageBox.Show("Plz enter the number", "Octal", MessageBoxButtons.OK, MessageBoxIcon.Information);

txtNum.Focus();

}

}

0 comments  

Converting decimal to binary[C#.NET]

Converting decimal number into Binary number

Language: C#.NET

Author: Aamir Mustafa

Introduction:

This article will show you how to convert a decimal number into binary number.


How to Use:

Create an empty project

On the form, add a button, two textboxes

  1. btnBinary
  2. txtNum
  3. txtBinary

We will take input through txtNum and show binary number into txtBinary and we will use btnBinary for converting decimal into Binary.

Write following code in btnBinary Click event.

[Code]

private void btnBinary_Click(object sender, EventArgs e)

{

try

{

long num = 0; //number that we will take through input

num = System.Convert.ToInt64(txtNum.Text);

txtBinary.Text = Convert.ToString(num, 2); //here 2 means converting to binary

}

catch (Exception ex)

{

MessageBox.Show("Plz enter the number", "Binary", MessageBoxButtons.OK, MessageBoxIcon.Information);

txtNum.Focus();

}

}

0 comments  

Converting decimal number into Hexadecimal number[VB.NET]

Converting decimal number into Hexadecimal number

Language: VB.NET

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:

This article will show you how to convert a decimal number into Hexadecimal number.


How to Use:

Create an empty project

On the form, add a button, two textboxes

  1. btnHexadecimal
  2. txtNum
  3. txtHexadecimal

We will take input through txtNum and show Hexadecimal number into txtHexadecimal and we will use btnHexadecimal for converting decimal into Hexadecimal.

Write following code in btn Hexadecimal Click event

[code]

Private Sub btnHexadecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHexadecimal.Click

Try

Dim num As Long 'number that we will take through input

num = CLng(txtNum.Text) 'assigning textbox value to num and converting string value to long using CLng function

If IsNumeric(num) Then 'checking that text box contains numeric value

txtHexadecimal.Text = Convert.ToString(num, 16) 'here 16 means converting to hexadecimal

End If

Catch ex As Exception

MessageBox.Show("Plz enter the number", "Octal", MessageBoxButtons.OK, MessageBoxIcon.Information)

txtNum.Focus()

End Try

End Sub

0 comments  

Converting decimal number into Octal number[VB.NET]

Converting decimal number into Octal number

Language: VB.NET

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:


This article will show you how to convert a decimal number into octal number.

How to Use:

Create an empty project

On the form, add a button, two textboxes

  1. btnOctal
  2. txtNum
  3. txtOctal

We will take input through txtNum and show binary number into txtBinary and we will use btnOctal for converting decimal into Octal.

Write following code in btnOctal Click event.

Code:

End Sub Private Sub btnOctal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOctal.Click

Try

Dim num As Long 'number that we will take through input

num = CLng(txtNum.Text) 'assigning textbox value to num and converting string value to long using CLng function

If IsNumeric(num) Then 'checking that text box contains numeric value

txtOctal.Text = Convert.ToString(num, 8) 'here 8 means converting to octal

End If

Catch ex As Exception

MessageBox.Show("Plz enter the number", "Octal", MessageBoxButtons.OK, MessageBoxIcon.Information)

txtNum.Focus()

End Try

End Sub

0 comments  

Converting decimal to binary[VB.NET]

Converting decimal number into Binary number

Language: VB.NET

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:

This article will show you how to convert a decimal number into binary number.

How to Use:

Create an empty project

On the form, add a button, two textboxes

  1. btnBinary
  2. txtNum
  3. txtBinary

We will take input through txtNum and show binary number into txtBinary and we will use btnBinary for converting decimal into binary.

Write following code in btnBinray Click event.

Code:

Private Sub btnBinary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinary.Click

Try

Dim num As Long 'number that we will take through input

num = CLng(txtNum.Text) 'assigning textbox value to num and converting string value to Long using CLng function

If IsNumeric(num) Then 'checking that text box contains numeric value

txtBinary.Text = Convert.ToString(num, 2) 'here 2 means converting to binary

End If

Catch ex As Exception

MessageBox.Show("Plz enter the number", "Binary", MessageBoxButtons.OK, MessageBoxIcon.Information)

txtNum.Focus()

End Try

End Sub

0 comments  

Converting decimal to binary

Converting decimal into Binary

Language: C++

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:

This simple program will convert a decimal number into equivalent binary

Code:

#include

#include

void main(void)

{

int bi,i,k; //declaring variables of integer type//

cout<<"Enter decimal number="; //asking user to prompt//

cin>>bi; //taking input//

cout<<"Its Binary Equivalent is ="; //getting the out put//

for(i=1;i<=12;i++) //executing for loop for 12 times//

{

k=bi%2; //getting the remainder//

bi=bi/2; //dividing by 2

cout<; //getting the value in binary form

}

cout<

getche();

}

Alternative Method

#include

#include

int main()

{

int num;

int result[12];

int i=0;

cout<<"enter the decimal number ";

cin>>num;

while(i<=12)

{

result[i]=num%2;

num=num/2;

i++;

}

for(int j=0;j<=12;j++)

cout<<>

cout<<>

system("pause");

}

0 comments