Need help developing analytical skills

This is a discussion on Need help developing analytical skills within the C++ Programming forums, part of the General Programming Boards category; Hi, Please help me develop some reasoning skills for these two small programs. They draw geometric figures and I've never ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Smile 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;
       }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    5 is the size of the diamond (radius if you will -- the number of rows in the top half). To figure out where the numbers come from, you should draw the figure (which you appear to have done) and then count (which you appear to have not done).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Unhappy 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
    Last edited by lanef; 10-02-2009 at 08:16 AM. Reason: Wordsmithing

  4. #4
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    I guess you didn't write the program. Someone did and you are trying to understand the code. Am I right?. If that is the case i suggest using a debugger and trace and watch the counter variables of the loop. It would certainly help you a lot. Also main should return int not void.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lanef View Post
    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
    My point was that the radius cannot be derived from anything. It is what it is, and must be given. The others will derive from it. You will derive the other numbers by counting. Specifically, for each line you must count the positions where the spaces on the right start and the spaces on the left end. Once you have counted each row, then the derivation kicks in: you need to write down an equation that gives you the numbers you have counted.


    You can't derive anything for the bottom figure, because there's nothing there that was derived. That's just the pattern of how "and" works. If you know how "and" and "not" work, then you know what that pattern is. If you don't know how "and" and "not" work, then you need to learn how "and" and "not" work, and then you will know the pattern. It's essentially a truth table printed with '*' and ' ' instead of 'T' and 'F'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Developing good logic skills
    By fsx in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 04-21-2009, 01:21 PM
  2. Developing skills
    By Tommo in forum C Programming
    Replies: 4
    Last Post: 08-23-2007, 09:21 PM
  3. MURK - a small preview
    By Mario F. in forum Game Programming
    Replies: 27
    Last Post: 12-18-2006, 07:22 AM
  4. Job skills
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-25-2006, 01:53 AM
  5. Skills of device driver writer for GPU vs Game Developer
    By Silvercord in forum Game Programming
    Replies: 9
    Last Post: 02-15-2003, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21