SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
1 Programming C++ ©by ARAM-CSD
.‫کۆلۆم‬ ‫ڵێن‬‫ده‬ ‫پێی‬ ‫ستوونی‬ ‫دووری‬ ‫ڕۆو‬ ‫ڵێن‬‫ده‬ ‫پێی‬ ‫ئاسۆیی‬ ‫دووری‬ ‫که‬ ‫پێکهاتووه‬ ‫دووری‬ ‫دوو‬ ‫له‬ :‫ماترێکس‬
‫بنه‬‫له‬ ‫دورری‬ ‫دوو‬ ‫بوونی‬ ‫دروست‬ ‫گشتی‬ ‫مای‬‫ڕیزکراوه‬‫به‬ ‫کاندا‬‫شێوه‬ ‫م‬‫یه‬‫یه‬:
‫جۆر‬ ‫ناو‬ ‫قه‬‫باره‬2‫قه‬‫باره‬1
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
‫ئه‬‫گه‬‫ژماره‬ ‫ر‬‫یه‬ ‫ستوونی‬ ‫و‬ ‫ئاسۆی‬ ‫ی‬‫وه‬ ‫بوو‬ ‫کسان‬‫ک‬3x3،2x2‫ئه‬‫ته‬ ‫وا‬‫نها‬1‫قه‬‫باره‬‫پێویسته‬ ‫مان‬‫به‬‫ئه‬ ‫اڵم‬‫گه‬‫ر‬
‫ئه‬ ‫بوو‬ ‫جیاواز‬‫ده‬ ‫وا‬‫دوو‬ ‫بێت‬2x3،3x4‫ئه‬‫ده‬ ‫وا‬‫قه‬ ‫دوو‬ ‫بێت‬‫باره‬.‫بنووسین‬ ‫جیاواز‬ ‫ی‬
‫لێره‬‫ده‬ ‫دا‬‫چینه‬‫سه‬‫به‬ ‫ر‬‫ئه‬ ‫تر‬ ‫شێکی‬‫ماترێکسه‬ ‫ناساندنی‬ ‫ویش‬‫که‬‫یه‬‫به‬‫شێوه‬ ‫م‬‫یه‬
Int array[size1][size2];
‫هه‬ ‫پاشان‬‫به‬ ‫شتێکمان‬ ‫ر‬‫وه‬ ‫بیناسێنین‬ ‫پێوستا‬ ‫کارهێنا‬( ‫ک‬i,j,n,k,l,m,)int i,j;
‫پاشان‬main()
{‫ده‬‫ده‬ ‫نووسین‬‫ده‬ ‫ست‬‫که‬‫به‬ ‫ین‬‫به‬‫رنامه‬‫داڕشتنه‬‫که‬.
‫ته‬‫ئه‬ ‫نها‬‫نه‬ ‫وشتا‬‫که‬ ‫بێت‬‫له‬‫سه‬‫ره‬‫وه‬‫ناساندوومانه‬‫وه‬‫قه‬ ‫ک‬‫باره‬‫وه‬ ‫ناو‬ ‫و‬‫دووباره‬ ‫نابێت‬.‫ناسێنین‬ ‫بیان‬
‫له‬‫هه‬ ‫بۆ‬ ‫دا‬ ‫ماترێکس‬‫به‬ ‫پێویستمان‬ ‫کردارێک‬ ‫موو‬‫ده‬ ‫لووپ‬ ‫دوو‬‫که‬ ‫بێت‬‫به‬‫د‬ )‫(فۆڕ‬‫ه‬.‫کرێت‬
‫وه‬( ‫ک‬cin, cout,if,‫وه‬ )‫هه‬‫که‬ ‫تر‬ ‫کردارێکی‬ ‫ر‬.‫بوو‬ ‫پێویست‬
for(i=0;i<size1;i++)
{
for(j=0;j<size2;j++)
{
_____
_____
}}
‫لێره‬‫یه‬ ‫لووپی‬‫که‬‫که‬ ‫م‬‫به‬(i( ‫و‬ )size1‫به‬ )‫ستراوه‬‫به‬‫ڕیزه‬ ‫بۆ‬ ‫کاردێت‬‫ئاسۆیه‬‫که‬ ‫کان‬‫له‬‫ده‬ ‫سفر‬‫پێده‬ ‫ست‬‫تا‬ ‫کات‬
‫دانه‬‫یه‬‫که‬ ‫ک‬‫متر‬‫له‬(size1.)
‫وه‬‫دووه‬ ‫لووپی‬‫که‬ ‫م‬‫به‬(j( ‫و‬ )size2‫به‬ )‫ستراوه‬‫به‬‫ڕیزه‬ ‫بۆ‬ ‫کاردێت‬‫ستوونیه‬‫که‬ ‫کان‬‫له‬‫ده‬ ‫سفر‬‫پێده‬ ‫ست‬‫تا‬ ‫کات‬
‫دانه‬‫یه‬‫که‬ ‫ک‬‫له‬ ‫متر‬(size2.)
( ‫کردن‬ ‫داغڵ‬ /‫أ‬cin>>:)‫ماترێکسی‬ :‫نموونه‬ ‫بۆ‬A2x2‫که‬‫قه‬‫باره‬‫کامان‬n=2‫قه‬‫باره‬‫یه‬ ‫یان‬‫کسانه‬
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
‫لێره‬‫له‬ ‫دا‬‫به‬‫ئه‬ ‫ر‬‫وه‬‫قه‬ ‫ی‬‫باره‬‫که‬= ‫مان‬2‫واته‬2x2‫ده‬‫بێت‬4‫ژماره‬‫بکه‬ ‫داغڵ‬‫به‬ ‫ین‬‫شێوه‬ ‫م‬‫یه‬‫خواره‬ ‫ی‬‫وه‬:
Cin>>a[i][j];
1 2 3 4
May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX)
3 Programming C++ ©by ARAM-CSD
‫لێره‬‫که‬ ‫کاتێک‬ ‫دا‬‫ژماره‬‫ده‬ ‫داخڵ‬‫که‬‫ده‬ ‫ین‬‫که‬ ‫بزانین‬ ‫بێ‬‫یه‬‫که‬‫ژماره‬ ‫م‬‫ده‬‫چێته‬a[i=0]‫وه‬‫دووه‬‫ژماره‬ ‫م‬‫ده‬‫چێته‬a[j=0]
‫واته‬a[i=0][j=0]‫یه‬‫که‬‫ژماره‬ ‫م‬‫دووه‬ ‫و‬‫ژماره‬ ‫م‬‫ده‬‫که‬ ‫گرن‬‫به‬‫شێوه‬‫ناوه‬ ‫دامان‬ ‫لوپ‬ ‫دوو‬ ‫ی‬‫واته‬‫هه‬‫موو‬
‫کرداره‬‫په‬ ‫کانمان‬‫یوه‬‫به‬ ‫ندی‬i‫و‬j‫هه‬‫یه‬‫که‬‫ماتریک‬ ‫بۆ‬ ‫گرنگن‬ ‫زۆر‬‫س‬‫له‬‫به‬.‫کارهێنانیاندا‬
‫به‬ // ‫تێبینی‬‫شێوه‬ ‫هیچ‬‫یه‬‫ک‬cin>>‫واته‬‫ئینپوت‬<<endl‫و‬<<” “‫وه‬‫راسته‬ ‫رناگرێت‬‫به‬ ‫وخۆ‬‫شێوه‬ ‫م‬‫یه‬
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<<"nn";
}
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<<"nn";
}
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<<"nn";
}
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 Matrixn"<<"_____________________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

Mais conteúdo relacionado

Destaque

Access lesson 04 Creating and Modifying Forms
Access lesson 04 Creating and Modifying FormsAccess lesson 04 Creating and Modifying Forms
Access lesson 04 Creating and Modifying FormsAram SE
 
Business in russia
Business in russiaBusiness in russia
Business in russiaAmalieFrom
 
1 borland c++ 5.02 by aramse
1   borland c++ 5.02 by aramse1   borland c++ 5.02 by aramse
1 borland c++ 5.02 by aramseAram SE
 
Access lesson 03 Creating Queries
Access lesson 03 Creating QueriesAccess lesson 03 Creating Queries
Access lesson 03 Creating QueriesAram SE
 
TFG DIPUTACIÓ DE BARCELONA
TFG DIPUTACIÓ DE BARCELONA TFG DIPUTACIÓ DE BARCELONA
TFG DIPUTACIÓ DE BARCELONA cocarroideceba
 
Access lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAccess lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAram SE
 
C++ programming flowchart. home work t.hemn
C++ programming flowchart. home work t.hemnC++ programming flowchart. home work t.hemn
C++ programming flowchart. home work t.hemnAram SE
 
Access lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAccess lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAram SE
 
Controle automatico de_processos
Controle automatico de_processosControle automatico de_processos
Controle automatico de_processosWilliam Andrade
 
Access lesson 01 Microsoft Access Basics
Access lesson 01 Microsoft Access BasicsAccess lesson 01 Microsoft Access Basics
Access lesson 01 Microsoft Access BasicsAram SE
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scriptingAkshay Siwal
 
Breakfast club oktober 2013
Breakfast club oktober 2013Breakfast club oktober 2013
Breakfast club oktober 2013AmalieFrom
 

Destaque (14)

Access lesson 04 Creating and Modifying Forms
Access lesson 04 Creating and Modifying FormsAccess lesson 04 Creating and Modifying Forms
Access lesson 04 Creating and Modifying Forms
 
Business in russia
Business in russiaBusiness in russia
Business in russia
 
1 borland c++ 5.02 by aramse
1   borland c++ 5.02 by aramse1   borland c++ 5.02 by aramse
1 borland c++ 5.02 by aramse
 
Access lesson 03 Creating Queries
Access lesson 03 Creating QueriesAccess lesson 03 Creating Queries
Access lesson 03 Creating Queries
 
TFG BUFF
TFG BUFFTFG BUFF
TFG BUFF
 
85
8585
85
 
TFG DIPUTACIÓ DE BARCELONA
TFG DIPUTACIÓ DE BARCELONA TFG DIPUTACIÓ DE BARCELONA
TFG DIPUTACIÓ DE BARCELONA
 
Access lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAccess lesson 02 Creating a Database
Access lesson 02 Creating a Database
 
C++ programming flowchart. home work t.hemn
C++ programming flowchart. home work t.hemnC++ programming flowchart. home work t.hemn
C++ programming flowchart. home work t.hemn
 
Access lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAccess lesson 02 Creating a Database
Access lesson 02 Creating a Database
 
Controle automatico de_processos
Controle automatico de_processosControle automatico de_processos
Controle automatico de_processos
 
Access lesson 01 Microsoft Access Basics
Access lesson 01 Microsoft Access BasicsAccess lesson 01 Microsoft Access Basics
Access lesson 01 Microsoft Access Basics
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
 
Breakfast club oktober 2013
Breakfast club oktober 2013Breakfast club oktober 2013
Breakfast club oktober 2013
 

3 matrix in c++ programming aramse

  • 1. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 1 Programming C++ ©by ARAM-CSD .‫کۆلۆم‬ ‫ڵێن‬‫ده‬ ‫پێی‬ ‫ستوونی‬ ‫دووری‬ ‫ڕۆو‬ ‫ڵێن‬‫ده‬ ‫پێی‬ ‫ئاسۆیی‬ ‫دووری‬ ‫که‬ ‫پێکهاتووه‬ ‫دووری‬ ‫دوو‬ ‫له‬ :‫ماترێکس‬ ‫بنه‬‫له‬ ‫دورری‬ ‫دوو‬ ‫بوونی‬ ‫دروست‬ ‫گشتی‬ ‫مای‬‫ڕیزکراوه‬‫به‬ ‫کاندا‬‫شێوه‬ ‫م‬‫یه‬‫یه‬: ‫جۆر‬ ‫ناو‬ ‫قه‬‫باره‬2‫قه‬‫باره‬1 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
  • 2. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 2 Programming C++ ©by ARAM-CSD ‫ئه‬‫گه‬‫ژماره‬ ‫ر‬‫یه‬ ‫ستوونی‬ ‫و‬ ‫ئاسۆی‬ ‫ی‬‫وه‬ ‫بوو‬ ‫کسان‬‫ک‬3x3،2x2‫ئه‬‫ته‬ ‫وا‬‫نها‬1‫قه‬‫باره‬‫پێویسته‬ ‫مان‬‫به‬‫ئه‬ ‫اڵم‬‫گه‬‫ر‬ ‫ئه‬ ‫بوو‬ ‫جیاواز‬‫ده‬ ‫وا‬‫دوو‬ ‫بێت‬2x3،3x4‫ئه‬‫ده‬ ‫وا‬‫قه‬ ‫دوو‬ ‫بێت‬‫باره‬.‫بنووسین‬ ‫جیاواز‬ ‫ی‬ ‫لێره‬‫ده‬ ‫دا‬‫چینه‬‫سه‬‫به‬ ‫ر‬‫ئه‬ ‫تر‬ ‫شێکی‬‫ماترێکسه‬ ‫ناساندنی‬ ‫ویش‬‫که‬‫یه‬‫به‬‫شێوه‬ ‫م‬‫یه‬ Int array[size1][size2]; ‫هه‬ ‫پاشان‬‫به‬ ‫شتێکمان‬ ‫ر‬‫وه‬ ‫بیناسێنین‬ ‫پێوستا‬ ‫کارهێنا‬( ‫ک‬i,j,n,k,l,m,)int i,j; ‫پاشان‬main() {‫ده‬‫ده‬ ‫نووسین‬‫ده‬ ‫ست‬‫که‬‫به‬ ‫ین‬‫به‬‫رنامه‬‫داڕشتنه‬‫که‬. ‫ته‬‫ئه‬ ‫نها‬‫نه‬ ‫وشتا‬‫که‬ ‫بێت‬‫له‬‫سه‬‫ره‬‫وه‬‫ناساندوومانه‬‫وه‬‫قه‬ ‫ک‬‫باره‬‫وه‬ ‫ناو‬ ‫و‬‫دووباره‬ ‫نابێت‬.‫ناسێنین‬ ‫بیان‬ ‫له‬‫هه‬ ‫بۆ‬ ‫دا‬ ‫ماترێکس‬‫به‬ ‫پێویستمان‬ ‫کردارێک‬ ‫موو‬‫ده‬ ‫لووپ‬ ‫دوو‬‫که‬ ‫بێت‬‫به‬‫د‬ )‫(فۆڕ‬‫ه‬.‫کرێت‬ ‫وه‬( ‫ک‬cin, cout,if,‫وه‬ )‫هه‬‫که‬ ‫تر‬ ‫کردارێکی‬ ‫ر‬.‫بوو‬ ‫پێویست‬ for(i=0;i<size1;i++) { for(j=0;j<size2;j++) { _____ _____ }} ‫لێره‬‫یه‬ ‫لووپی‬‫که‬‫که‬ ‫م‬‫به‬(i( ‫و‬ )size1‫به‬ )‫ستراوه‬‫به‬‫ڕیزه‬ ‫بۆ‬ ‫کاردێت‬‫ئاسۆیه‬‫که‬ ‫کان‬‫له‬‫ده‬ ‫سفر‬‫پێده‬ ‫ست‬‫تا‬ ‫کات‬ ‫دانه‬‫یه‬‫که‬ ‫ک‬‫متر‬‫له‬(size1.) ‫وه‬‫دووه‬ ‫لووپی‬‫که‬ ‫م‬‫به‬(j( ‫و‬ )size2‫به‬ )‫ستراوه‬‫به‬‫ڕیزه‬ ‫بۆ‬ ‫کاردێت‬‫ستوونیه‬‫که‬ ‫کان‬‫له‬‫ده‬ ‫سفر‬‫پێده‬ ‫ست‬‫تا‬ ‫کات‬ ‫دانه‬‫یه‬‫که‬ ‫ک‬‫له‬ ‫متر‬(size2.) ( ‫کردن‬ ‫داغڵ‬ /‫أ‬cin>>:)‫ماترێکسی‬ :‫نموونه‬ ‫بۆ‬A2x2‫که‬‫قه‬‫باره‬‫کامان‬n=2‫قه‬‫باره‬‫یه‬ ‫یان‬‫کسانه‬ for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; }} ‫لێره‬‫له‬ ‫دا‬‫به‬‫ئه‬ ‫ر‬‫وه‬‫قه‬ ‫ی‬‫باره‬‫که‬= ‫مان‬2‫واته‬2x2‫ده‬‫بێت‬4‫ژماره‬‫بکه‬ ‫داغڵ‬‫به‬ ‫ین‬‫شێوه‬ ‫م‬‫یه‬‫خواره‬ ‫ی‬‫وه‬: Cin>>a[i][j]; 1 2 3 4
  • 3. May 13, 2012 TWO-DIMENSIONAL ARRAYS (MATRIX) 3 Programming C++ ©by ARAM-CSD ‫لێره‬‫که‬ ‫کاتێک‬ ‫دا‬‫ژماره‬‫ده‬ ‫داخڵ‬‫که‬‫ده‬ ‫ین‬‫که‬ ‫بزانین‬ ‫بێ‬‫یه‬‫که‬‫ژماره‬ ‫م‬‫ده‬‫چێته‬a[i=0]‫وه‬‫دووه‬‫ژماره‬ ‫م‬‫ده‬‫چێته‬a[j=0] ‫واته‬a[i=0][j=0]‫یه‬‫که‬‫ژماره‬ ‫م‬‫دووه‬ ‫و‬‫ژماره‬ ‫م‬‫ده‬‫که‬ ‫گرن‬‫به‬‫شێوه‬‫ناوه‬ ‫دامان‬ ‫لوپ‬ ‫دوو‬ ‫ی‬‫واته‬‫هه‬‫موو‬ ‫کرداره‬‫په‬ ‫کانمان‬‫یوه‬‫به‬ ‫ندی‬i‫و‬j‫هه‬‫یه‬‫که‬‫ماتریک‬ ‫بۆ‬ ‫گرنگن‬ ‫زۆر‬‫س‬‫له‬‫به‬.‫کارهێنانیاندا‬ ‫به‬ // ‫تێبینی‬‫شێوه‬ ‫هیچ‬‫یه‬‫ک‬cin>>‫واته‬‫ئینپوت‬<<endl‫و‬<<” “‫وه‬‫راسته‬ ‫رناگرێت‬‫به‬ ‫وخۆ‬‫شێوه‬ ‫م‬‫یه‬ 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(); }
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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.
  • 8. 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
  • 9. 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<<"nn"; } getch(); } 1 2 3 4 5 6 7 8 9 0 2 3 4 0 6 7 8 0
  • 10. 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<<"nn"; } 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
  • 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<<"nn"; } 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
  • 12. 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 Matrixn"<<"_____________________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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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