Wednesday, January 13, 2016

(ENG/TR) Algorithm to draw diamond shape/ Elmas çizen algoritma

1            *
2           **
3          ***
4         ****
5        *****
6       ******
7      *******
8     ********
9    *********
0   **********
1  ***********
2   **********
3    *********
4     ********
5      *******
6       ******
7        *****
8         ****
9          ***
0           **
1            *


ENGLISH

Whenever we try to find an algorithm, first we should find "regularity". Our diamond's middle which has 11 character length, is the largest entry. We see that we have "direct proportion" from up to middle. Then from middle to last line, we have "inverse proportion". I mean;
1. line, we write 1 * into 10. space
2. line, we write 2 * into 9. space
3. line, we write 3 * into 8. space
...
11. line, we write 11 * into 0. space
12. line, we write 10 * into 1. space
13. line, we write 9 * into 2. space
14. line, we write 8 * into 3. space
...
21. line, we write 1 * into 10. space

Now, it is easy to see the way to write this code: up to line 11 decrease space by 1, increase * by 1; from line 11 up to 21 increase space by 1, decrease * by 1.

TÜRKÇE

Her ne zaman bir algoritma bulmak istesek, ilk olarak "düzen"i bulmalıyız. Elmasımızın 11 karakter uzunluğundaki ortası, en uzun girdimiz. Orta kısma kadar "doğru orantı" olduğunu görüyoruz. Sonra ortadan, son satıra doğru "ters orantı"mız var. Yani:
1. satırda, 1 *'i 10. boşluğa yazıyoruz
2. satırda, 2 *'i 9. boşluğa yazıyoruz
3. satırda, 3 *'i 8. boşluğa yazıyoruz
...
11. satırda, 11 *'i 0. boşluğa yazıyoruz
12. satırda, 10 *'i 1. boşluğa yazıyoruz
13. satırda, 9 *'i 2. boşluğa yazıyoruz
14. satırda, 8 *'i 3. boşluğa yazıyoruz
...
21. satırda, 1 *'i 10. boşluğa yazıyoruz

Şimdi kodumuzu yazmanın yolu kolay: 11. satıra kadar boşluğu 1 azalt, *'i 1 arttır; 11. satırdan 21. satıra kadar boşluğu 1 arttır, *'i 1 azalt.

No comments:

Post a Comment