Hi,
Please help me develop some reasoning skills for these two small programs.
They draw geometric figures and I've never been exposed to their likes before.
One is a diamond and what I don't get are how they establish the constants (i.e. 5), the iteration numbers (0-2*N),
or the testing values (especially 3*N-i). These numbers all seem arbitrary but I'd like to understand how they
arrived at the numbers analytically.
The other is a triangle. I don't understand what about the problem tells us that we're supposed to notice that the
bitwise inversion of the outer loop counter bitwise anded with the inner loop counter will give us a nice triangle
pattern. Any help will be greatly appreciated.
Thanks,
lanef
Diamond
PHP Code:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Diamond Code
Code:
#include <iostream>
void main() {
const int N=5;
for (int i = 0; i <= 2 * N; i++) {
for (int j = 0; j <= 2 * N; j++) {
if (i <= N) {
if (j < N - i || j > N + i) {
std::cout << ' ';
}
else {
std::cout << '*';
}
}
else {
if (j < i - N || j > 3 * N - i) {
std::cout << ' ';
}
else {
std::cout << '*';
}
}
}
std::cout << std::endl;
}
}
Triangle
PHP Code:
*
**
* *
****
* *
** **
* * * *
********
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
****************
Triangle Code
Code:
#include <iostream>
void main() {
const int N = 16;
for (int n = 0; n < N; n++) {
for (int k = 0; k <= n; k++) {
std::cout << ((~n & k) != 0 ? ' ' : '*');
}
std::cout << std::endl;
}
}