Like Tree1Likes

asterik tree

This is a discussion on asterik tree within the C# Programming forums, part of the General Programming Boards category; I have some code for a asterik tree and I can't get the trunk to line up correctly in the ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    150

    asterik tree

    I have some code for a asterik tree and I can't get the trunk to line up correctly in the middle of the base of the tree it's all over the place depending on what value I put in ( I'm supposed to use odd numbers)
    I got the code off the internet so I didn't write it myself. If someone can even explain what it's doing would be enough for me. Thanks for looking at my post

    Here's the code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter6Problem14
    {
        class Program
        {
            static void Main()
            {
                int rows;
                int a, b, c, d, e;
    
                Console.Write("Input an odd number of rows for the asterik tree: ");
                rows = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine();
    
                for (a = 1; a <= (rows / 2) + 1; a++)
                {
                    for (b = 1; b <= rows - a; b++)
                        Console.Write("{0}", ' ');
                    for (c = 1; c <= (2 * a) - 1; c++)
                        Console.Write("{0}", "*");
                    Console.WriteLine();
                }
    
                for (e = 1; e <= rows - 2; e++)
                    Console.Write(" ");
                for (d = 1; d <= (rows / 2); d++)
                {
                    Console.Write("{0}", '*');
                }
                Console.WriteLine();
                Console.ReadKey();
    
    
    
            }
        }
    }

  2. #2
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Are you allowed to use recursion or does it have to be iterative like you have now?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Actually I wouldn't mind seeing both. I know that might be a little bit of work but, maybe I would understand it better. If I saw two versions

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,252
    O_o

    Your posts are kind of all over the place, and I suspect you aren't doing anything near due diligence in your programming class.

    Why don't you describe, with simple words, how you might draw the appropriate shape for a given input.

    [Edit]
    Also a little disappointing that this isn't B*Tree related...
    [/Edit]

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Well I have this so far I hope this is what you meant
    make pseudocode



    asterisks = 7
    print asteriks minus asteriks minus one
    print asteriks minus asteriks minus two
    print asteriks minus asteriks minus three
    print asteriks minus asteriks minus four
    print asteriks minus asteriks minus five
    print asterkik minus asteriks minus six
    print asteriks

    not really sure how to make the trunk

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,252
    O_o

    Are you talking about a tree or a wedge?

    Can you draw what you want this to look like by hand and post the example in code tags?

    I only ask because that prints more a wedge than a tree.

    If you want a tree, you need to consider how you would center each line relative to the longest line.

    Consider what you would need to type if you were on an old typewriter.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Code:
                         *
                        **
                       ***
                      ****
                     *****
                    ******
                   *******
    This is what I would like to do but I guess the pseudo code I gave you will print a wedge sorry about that obviously I'm a newbie hee hee

    I can't get it to draw a tree even with the code tags

    I guess that would be half a tree
    Last edited by artistunknown; 03-20-2014 at 05:26 AM.

  8. #8
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Err ok. I didn't realise you meant a "tree" like that. Is that even considered a tree? Wouldn't you normally describe such a structure as a triangle?

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    I would draw it properly but, this message box won't let me it's shape like a triangle with asteriks at the bottom for the trunk tough to explain but I have a feeling you guys thought I was talking about a binary tree or something which I have no idea what that is. That's in data structures class and I haven't taken that yet

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Oh, by the way I'm not really in college right now I getting my self prepared to take programming 2. So I'm doing programming exercises out of the book C for Business Programmers. This problem is actually harder than meets the eye if you ask me because of the trunk the triangle part of the code is not that hard but getting the trunk to work with any odd number you put in is hard if you ask me I'm also suppose to center the tree in the center of the terminal screen but, I'm probably going to skip that since the problem is so hard anyway

    I attached a picture of the asterik tree with a trunk
    sorry its upside down
    Attached Images Attached Images  
    Last edited by artistunknown; 03-20-2014 at 06:46 PM.

  11. #11
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Ahh, I see the problem now: you're reading your book upside down.

    Seriously, the problem isn't hard if you think about it carefully. Have a look at your psuedocode and see if you can come up with a method that calculates the number of leading spaces and the number of '*'s to print.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    part of the problem is

    it says to choose a bottom row an odd number say 7

    7 works
    but 3 will not
    5 will not
    and 9 will not

    you might have to run the code to see what I mean
    Last edited by artistunknown; 03-21-2014 at 01:01 AM.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Boy, I feel like a horses ass The problem I got off the Internet is different from the problem in my book I think I can solve the problem in the book easily I feel stupid because I was basically making a mountain out of a mole hill sorta
    Last edited by artistunknown; 03-21-2014 at 02:20 AM.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,252
    O_o

    If you give up now, you may as well find a different profession.

    I'm not trying to discourage you; that is simply the reality: programming is often difficult and may problems are not easy to manage.

    If you want to keep at it though, ask yourself why some values work yet others do not work. If nothing else, grab a piece of paper and walk through the code writing down the bits and bobs of the code.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  15. #15
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Quote Originally Posted by phantomotap View Post
    O_o
    Are you a sorcerer?

    -- Hodor
    phantomotap likes this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rooted Tree (Family tree)
    By ch4 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 04:24 PM
  2. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  3. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 11:47 PM
  4. Replies: 5
    Last Post: 05-23-2003, 01:11 PM
  5. b-tree (not binary tree) implementation help
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 01-02-2002, 02:30 PM

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