3 matrix in c++ programming aramse
Post on 27-Dec-2014
89 Views
Preview:
DESCRIPTION
TRANSCRIPT
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
1 Programming C++ ©by ARAM-CSD
ماترێکس: له دوو دووری پێکهاتووه که دووری ئاسۆیی پێی ده ڵێن ڕۆو دووری ستوونی پێی ده ڵێن کۆلۆم.
: یه یه م شێوه کاندا به ڕیزکراوه مای گشتی دروست بوونی دوو دورری له بنه
1باره قه 2باره قه ناو جۆر
Type array [size1][size2];
A[ i ][ j ]
. یه که رنامه کی به ره شێکی سه به ر(ی بۆ بنووسین که کانی پێشوو ) هێده رنامه موو به کو هه وه تا پێویسته ره سه
حساباتی بیرکاری که)ک ین وه ردا زیادی بکه هێده بێ له ر جۆر کردارێکی تر بوو ده هه ر پێویستمان به گه ئه وه
دا. که رنامه به رف( له نووسین) تێکست و حهوێت، یان که تێده (توانی
1. #include <iostream.h>
2. #include <conio.h>
3. #include <math.h>
4. #include <cstring.h>
اڵم: کدا بنووسرێت به یه رنامه موو به هه بێت له ده که که ره کین بۆ هێده ره م سه م و دووه که خاڵی یه
ژێ ...(. ته ،گ ڕه ،توان کاردێت بۆ )حساباتی بیرکاری که به 3خاڵێ
دا. که رنامه به رف( له کاردێت بۆ ) تێکست و حه به 4خاڵی
یان ده پێویستمانه ی که وشتانه تیایدا ئه مانای لێدوان دێت که به شه م به ئه که یشن( ره )دیکلهشی به چینه پاشان ده
: میش چوار جۆره ئه که رنامه به ناسێنین به
1. int کان واوه ی ته کاردێت بۆ ژماره ر به ئینتیجه
2. float کان یه کار دێت بۆ ژماره ڕێژه فلۆت به
3. string یان ناو کاردێت بۆ نوسینی وشه سترینگ به
4. char رف ک حه نها یه کاردێت بۆ نوسینی ته ر به کته کاره
م به وه ر(ه دوای )هێده ین له تادا داغڵی بکه ره سه بێت له ده که گۆڕێکه نه ماترێکس دا پێویستمان به له
یه: شێوه
Int const size1=?,size2=?;
ئاسۆی Row
ستوونی Column
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
2 Programming C++ ©by ARAM-CSD
ر گه اڵم ئه به مان پێویسته باره قه 1نها وا ته ئه 3x3 ،2x2ک کسان بوو وه ی ئاسۆی و ستوونی یه ر ژماره گه ئه
ی جیاواز بنووسین. باره بێت دوو قه وا ده ئه 2x3 ،3x4بێت دوو وا ده جیاواز بوو ئه
یه م شێوه به یه که ویش ناساندنی ماترێکسه شێکی تر ئه ر به سه چینه دا ده لێره
Int array[size1][size2];
;int i,j( ,i,j,n,k,l,mک ) کارهێنا پێوستا بیناسێنین وه ر شتێکمان به پاشان هه
()mainپاشان
. که داڕشتنه رنامه به ین به که ست ده نووسین ده ده }
بیان ناسێنین. نابێت دووباره و ناو وه باره ک قه وه ناساندوومانه وه ره سه له بێت که وشتا نه نها ئه ته
کرێت. ه)فۆڕ( د به بێت که دوو لووپ ده موو کردارێک پێویستمان به ماترێکس دا بۆ هه له
وه ( کcin, cout,if,وه ) پێویست بوو. ر کردارێکی تر که هه
for(i=0;i<size1;i++)
{
for(j=0;j<size2;j++)
{
_____
_____
}}
کات تا ست پێده سفر ده له کان که ئاسۆیه کاردێت بۆ ڕیزه به ستراوه ( بهsize1( و )i) به م که که لووپی یه لێره
(.size1) له متر ک که یه دانه
کات تا ست پێده سفر ده له کان که ستوونیه کاردێت بۆ ڕیزه به ستراوه ( بهsize2( و )j) به م که لووپی دووه وه
(.size2) متر له ک که یه دانه
کسانه یان یه باره قه n=2کامان باره قه کهA2x2 بۆ نموونه: ماترێکسی (:<<cinأ/ داغڵ کردن )
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
:وه ی خواره یه م شێوه ین به داغڵ بکه ژماره 4بێت ده 2x2 واته 2مان = که باره ی قه وه ر ئه به دا له لێره
Cin>>a[i][j];
1 2 3 4
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
3 Programming C++ ©by ARAM-CSD
a[j=0] چێته ده م ژماره دووه وه a[i=0] چێته ده م ژماره که یه بێ بزانین که ین ده که داخڵ ده ژماره دا کاتێک که لێره
موو هه واته ی دوو لوپ دامان ناوه شێوه به گرن که ده م ژماره و دووه م ژماره که یه a[i=0][j=0] واته
کارهێنانیاندا. به له سزۆر گرنگن بۆ ماتریک که یه هه jو i ندی به یوه کانمان په کرداره
یه م شێوه وخۆ به رناگرێت راسته وه “ ”>>و endl>>ئینپوت واته <<cinک یه هیچ شێوه تێبینی // به
cin>>a[i][j]<<endl<<” “;
cin>>a[i][j];
}}
cout<<endl<<” “;
. که ر شاشه سه له داغڵمان کردووه داتێ که مان ده کرداره نجامی ئه ئه شه م به ( :ئه>>coutوت ) ستکه ده ب/
: یه م شێوه به
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<” “;
}
cout<<endl;
}
چێت: رده ده یه م شێوه به کردووهداغڵمان وه هر سه له ی که وه ئه بۆ نموونه
1 2
3 4 م واته ڕیزی دووه بێت به ده که شینه نگه ڕه نێت وه یه گه ده iمانای م واته که ڕیزی یه بێت به ده سووره نگه ڕه که
j نێت. یه گه ده
نووسین: ده ین خه چێت تا خۆمان دایده وێت و النه ربکه مان بۆ ده که شه ڕه ی شاشه وه بۆ ئه مانه مووی ئه دوا هه له وه
getch();
}
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
4 Programming C++ ©by ARAM-CSD
چێت: رده ده یه م شێوه به 3 باره ماترێکسێکی قه ین به بکه وه ره ی سه و کردارانه موو ئه ر هه گه ئه ئێسته
//write C++ program to read and print the matrix A3x3 .
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ INPUT
cin>>a[i][j];
}}
cout<<endl;
int const n=3;
int a[n][n]; Declaration
int I,j;
#include <iostream.h>
#include <conio.h>
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout <<a[i][j]<<” “; OUTPUT
}
cout<<endl;
}
getch();
}
Header
END
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
5 Programming C++ ©by ARAM-CSD
ن ی زیاتر لێێ تێ بگه وه بۆ ئه ی شیکارکراوم داناوه ڵێک نموونه دا کۆمه لێره
1- Write C++ program to read and print out the Matrix A3x3 .then print sum of all rows.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int sum[n];
int i,j;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(i=0;i<n;i++)
{
sum[i]=0;
for(j=0;j<n;j++)
{
sum[i]=sum[i]+a[i][j];
}
cout<<"Sum of row ("<<(i+1)<<")="<<sum[i]<<endl;
}
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
Sum of row (1) = 6
Sum of row (2) = 15
Sum of row (3) = 24
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
6 Programming C++ ©by ARAM-CSD
2-Write C++ program to read and print out the Matrix A3x3 .then print sum of all columns.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int sum[n];
int i,j;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(j=0;j<n;j++)
{
sum[j]=0;
for(i=0;i<n;i++)
{
sum[j]=sum[j]+a[i][j];
}
cout<<"Sum of column ("<<(j+1)<<")="<<sum[j]<<endl;
}
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
Sum of column (1) = 12
Sum of column (2) = 15
Sum of column (3) = 18
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
7 Programming C++ ©by ARAM-CSD
#include <iostream.h>
#include <conio.h>
int const n=3,m=2;
int a[n][m];
int i,j,sum[n],mult[m];
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}}
cout<<endl;
cout<<"A1"<<"\t"<<"A2"<<endl;
cout<<"___________"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
cout<<"____________"<<endl;
for(i=0;i<n;i++)
{ sum[i]=0;
for(j=0;j<m;j++)
{
sum[i]=sum[i]+a[i][j];
}
cout<<"Sum of row ["<<(i+1)<<"]="<<sum[i]<<endl;
}
cout<<"_________________\n"<<endl;
for(j=0;j<m;j++)
{ mult[j]=1;
for(i=0;i<n;i++)
{
mult[j]=mult[j]*a[i][j];
}
cout<<"Mult of col ["<<(j+1)<<"]="<<mult[j]<<endl;
}
getch();
}
3-write a program to read and printout Matrix Anxm, then find the sum of each row
in A and Multiplication of each column in A.
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
8 Programming C++ ©by ARAM-CSD
4- Write program to create an array A2x2. first row are EVEN and second row are ODD.
#include <iostream.h>
#include <conio.h>
int const n=2;
int a[n][n];
int i,j;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
cout<<"__"<<"\t"<<"__"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\n__________________\n";
cout<<"__"<<"\t"<<"__"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]%2==0)
cout<<a[i][j]<<"\t";
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]%2==1)
cout<<a[i][j]<<"\t";
}}
getch();
}
1 2 3 4
__ __
1 2
3 4
___________
__ __
2 4
1 3
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
9 Programming C++ ©by ARAM-CSD
5-Write C++ program to read and print out the Matrix A3x3 when Diagonal equal to Zero.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
a[i][j]=0;
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n\n";
}
getch();
}
1 2 3 4 5 6 7 8 9
0 2 3
4 0 6
7 8 0
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
10 Programming C++ ©by ARAM-CSD
6-Write C++ program to read and print out the Matrix A3x3 .then print sum of lower triangular.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j,l;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n\n";
}
l=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
l=l+a[i][j];
}
}
cout<<"Sum of Lower Triangular="<<l;
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
Sum of Lower Triangular=11
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
11 Programming C++ ©by ARAM-CSD
7- Write C++ program to read and print out the Matrix A3x3 .then print sum of Upper triangular.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j,u;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n\n";
}
u=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
u=u+a[i][j];
}
}
cout<<"Sum of Upper Triangular="<<l;
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
Sum of Upper Triangular=19
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
12 Programming C++ ©by ARAM-CSD
8- Write C++ program read and print the matrix A3x3, and find the Identity of the matrix.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j,k;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;}
cout<<"\nThe Identity of the Matrix\n"<<"_____________________\n";
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{if(i==j)
a[i][j]=1;
else
a[i][j]=0;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{cout<<a[i][j]<<" ";
}cout<<"\n";
}
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
The identity of the Matrix
______________________
1 0 0
0 1 0
0 0 1
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
13 Programming C++ ©by ARAM-CSD
9- Write C++ program read and print the matrix A3x3, and find the Trace of the matrix.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j,k;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
k=k+a[i][j];
}
}}
cout<<"\nThe Trace of the Matrix="<<k;
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
The Trace of the Matrix=15
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
14 Programming C++ ©by ARAM-CSD
10- Write C++ program read and print the matrix A3x3, and make the lower triangular=0.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{if(i<j)
a[i][j]=0;
cout<<a[i][j]<<” “;
}
cout<<endl;
}
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
1 0 0
4 5 0
7 8 9
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
15 Programming C++ ©by ARAM-CSD
11- Write C++ program read and print the matrix A3x3, then check whether is symmetric or
not.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j,k;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;}
cout<<endl;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{if(a[i][j]==a[i][j])
k=k+1;
}}
if(k==n*n)
cout<<”The Matrix is symmetric”;
else
cout<<”The Matrix is not symmetric”;
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
The Matrix is not symmetric
1 2 3 2 5 6 3 6 9
1 2 3
2 5 6
3 6 9
The Matrix is symmetric
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
16 Programming C++ ©by ARAM-CSD
12- Write C++ program read and print the matrix A3x3, and make the Upper triangular=0.
#include <iostream.h>
#include <conio.h>
int const n=3;
int a[n][n];
int i,j;
main()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{if(i>j)
a[i][j]=0;
cout<<a[i][j]<<” “;
}
cout<<endl;
}
getch();
}
1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
1 2 3
0 5 6
0 0 9
top related