چاپ مختصات اعداد یک ماتریس به زبان ++c
کد:
#include <iostream.h>
#include<iomanip.h>
#include<conio.h>
class mokh
{
// written by: navid
public:
mokh(int);
void cal(int);
void print();
private:
void make();
int x[20][20];
int n;
};
//***************************
mokh::mokh(int r)
{
n=r;
make();
}
//*******************************
void mokh::make()
{
int l,d,j,i,e=0;
l=n*n-1;d=n-1;
while(l>=0)
{
for(i=d;i>=e;i--)
x[e][i]=l--;
for(j=e+1;j<=d;j++)
x[j][e]=l--;
for(i=e+1;i<=d;i++)
x[d][i]=l--;
for(j=d-1;j>=e+1;j--)
x[j][d]=l--;
e++; d--;
}
}
//*****************************
void mokh::print()
{
clrscr();
cout<<"the matrix is:\n\n";
for(int i=0;i<n;i++)
{
cout<<'\t';
for(int j=0;j<n;j++)
cout<<x[i][j]<<setw(4);
cout<<"\n";
}
}
//*****************************
void mokh::cal(int s)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(x[i][j]==s)
cout<<"the result is: ( "<<(j-(n-1)/2)<<" , "<<((n-1)/2-i)<<" )";
}
//****************************
int main()
{
mokh bn(15);
int d;
char ch;
cout<<"enter a number to serch:";
cin>>d;
bn.cal(d);
cout<<"\n\nif you have see matrix press(y) else press any key:";
cin>>ch;
if(ch=='y')
bn.print();
return 0;
}
منبع:دانشجو كلوپ