Need help developing analytical skills
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;
}
}
Need help developing analytical skills
Hi,
I guess I didn't make myself clear? You seem to understand these problems. I don't. Saying that the radius of the diamond is 5 doesn't help alot.
Please criticize this statement if it's not what you were hinting at. "Knowing the radius is 5 does help me to see that the diameter then drives the iteration limits, and that you have to go up to the diameter and then include it because there are really eleven rows and columns, not ten."
I still don't understand how to arrive at the 3*N-i term analytically. I can kinda see it as a trial and error thing but would like to understand it along the lines of "Okay, the radius of the diamond is 5 therefore you have to discriminate the lower right area by 5.mathoperation(radius) = 3".
And counting and drawing don't help me understand the triangle problem at all.
Thank you for replying,
Frank
Need help developing analytical skills
Hi Steve,
Yup, you're right. It's not that simple to me to understand it either. I've gotta figure out how to run my command line debugger but that's a great idea I didn't think of.
Thank you for replying,
Frank