#include <iostream.h>
#include <conio.h>
void main()
{
int d[5][5]; //angka 5 bisa di ganti
int start = 1;
int count = start;
for (int i=0; i<5; i++) {
// pengaturan bagian atas
for (int j=i; j<5-i; j++)
d[i][j] = count++;
count--;
// pengaturan bagian kanan
for (int j=i; j<5-i; j++)
d[j][5-i-1] = count++;
count--;
// pengaturan bagian bawah
for (int j=5-i-1; j>=i; j--)
d[5-i-1][j] = count++;
count--;
// pengaturan bagian kiri
for (int j=5-i-1; j>i; j--)
d[j][i] = count++;
if (count > 5*5+start)
break;
}
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
cout << d[i][j] << " ";
}
cout << endl;
}
getch();
}
#include <conio.h>
void main()
{
int d[5][5]; //angka 5 bisa di ganti
int start = 1;
int count = start;
for (int i=0; i<5; i++) {
// pengaturan bagian atas
for (int j=i; j<5-i; j++)
d[i][j] = count++;
count--;
// pengaturan bagian kanan
for (int j=i; j<5-i; j++)
d[j][5-i-1] = count++;
count--;
// pengaturan bagian bawah
for (int j=5-i-1; j>=i; j--)
d[5-i-1][j] = count++;
count--;
// pengaturan bagian kiri
for (int j=5-i-1; j>i; j--)
d[j][i] = count++;
if (count > 5*5+start)
break;
}
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
cout << d[i][j] << " ";
}
cout << endl;
}
getch();
}
Refrensi:
http://www.cplusplus.com/forum/general/4868/
Labels: algoritma, Program C++ : Membuat Spiral Matrix
3 comments:
-
wah mantep gan thx ya bwt codingan nya.. bisa jadi inspirasi nih bwt tgs ane.. (yaah tugasnya mirip si) arigatou gozaimasu
-
gan ane nemu caranya input dari user:
----------------------------------------
#include
#include
void main()
{
int n;
int d[100][100]; //angka 100 bisa di ganti
int start = 1;
int count = start;
cout<<"masukan data : ";cin>>n;
for (int i=0; i=i; j--)
d[n-i-1][j] = count++;
count--;
// pengaturan bagian kiri
for (int j=n-i-1; j>i; j--)
d[j][i] = count++;
if (count > n*n+start)
break;
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout << d[i][j] << " ";
}
cout << endl;
}
getch();
}
0 comments