CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    Beginner C# hw help!!

    can someone please help me with my hw?

    im currently taking a beginners C# course and need some pointers on the hw. I have a very hard nosed instructor that refuses to give much help..

    the objective is to get a "mountain of alphabet" given the inputs and he wants us to get the following output:

    a
    b c
    d e f
    g h i j
    k l m n o
    p q r s
    t u v
    w x
    y

    Inputs:

    Enter a letter between 'a' and 'z':

    Enter a positive integer:


    Here's what i have so far:
    {
    char sc = Convert.ToChar(TbxLetter.Text.Trim()[0]);

    int n = Convert.ToInt32(TbxInteger.Text);


    if (sc < 'a' || sc > 'z')
    {
    TbxOutput.Text = "Please enter a letter from 'a' to 'z'!!";
    return;
    }

    TbxOutput.Text = "";



    int row = 1, col = 1;

    for (int i = 1; i <= n; i++)
    {
    TbxOutput.Text += sc.ToString() + " ";
    sc++;
    if (sc > 'z') sc = 'a';

    if (row == 10) row = 1;

    if (col == row)
    {
    TbxOutput.Text += "\n";
    row++;
    col = 0;
    }

    col++;

    Here's the Ouput that i get:

    a
    b c
    d e f
    g h i j
    k l m n o
    p q r s
    t u v w x
    y z a b


    what do i need to do to make it continually decrease until it gets to Y and then make it start over again at Z?

    any pointers would be helpful

    Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Re: Beginner C# hw help!!

    Firstly You need to learn to put your code between tags

    Secondly Please make it clear what you want to achive here? If you just have to print the left to right pyramid why do you have to ask a character between a to z and integer?? Please make it clear so that we can understand your problem better......

    Now for printing part I suggest you to 2 sets of loops....1 for printing upright pyramid


    a
    b c
    d e f
    g h i j
    k l m n o
    and other for printing upside down pyramid
    p q r s
    t u v
    w x
    y
    by this way it is easier for to code......if your teacher is not satisfied or you have any problem implementing it please reply here....

  3. #3
    Join Date
    Feb 2010
    Posts
    2

    Re: Beginner C# hw help!!

    thanks for the reply im trying to get the following result

    a
    b c
    d e f
    g h i j
    k l m n o
    p q r s
    t u v
    w x
    y
    z
    a b
    c d e etc....

    instructions:
    Given a letter (‘a’ thru ‘z’), create mountain shapes using letters starting from the letter entered and ensuing letters. The total number of alphabets displayed is specified by the number entered on the user interface.
    Validate the user input for no entries, a lower case alphabet, and an integer. In case of invalid input, stop the execution and display the error message.

    thanks in advance for any help...

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Beginner C# hw help!!

    Here is a way of doing it recursively....
    Code:
            private void Grow(Char currentLetter, Int32 currentHeight, Int32 requiredHeight, Int32 lettersRemaining, ref String pyramid)
            {
                // add the letter to the string and increase the height
                pyramid += currentLetter;
                currentHeight++;
    
                // we've used up a letter
                lettersRemaining--;
    
                // exit if we have done
                if (lettersRemaining.Equals(0))
                    return;
    
                // move to the next letter
                if (++currentLetter > 'z')
                    currentLetter = 'a';
    
                // check if we've reached the required height
                if (currentHeight.Equals(requiredHeight))
                {
                    // move to the next line and reset the current height
                    pyramid += Environment.NewLine;
                    currentHeight = 0;
    
                    // check if we have reached the peak of the pyramid
                    if (requiredHeight.Equals(5))
                    {
                        // start shrinking
                        Shrink(currentLetter, currentHeight, 4, lettersRemaining, ref pyramid);
                        return;
                    }
                    
                    // otherwise we need to go one higher next time
                    requiredHeight++;
                }
    
                // process the next letter
                Grow(currentLetter, currentHeight, requiredHeight, lettersRemaining, ref pyramid);
            }
    
            private void Shrink(Char currentLetter, Int32 currentHeight, Int32 requiredHeight, Int32 lettersRemaining, ref String pyramid)
            {
                // add the letter to the string and increase the height
                pyramid += currentLetter;
                currentHeight++;
    
                // we've used up a letter
                lettersRemaining--;
    
                // exit if we have done
                if (lettersRemaining.Equals(0))
                    return;
    
                // move to the next letter
                if (++currentLetter > 'z')
                    currentLetter = 'a';
    
    
                // check if we've reached the required height
                if (currentHeight.Equals(requiredHeight))
                {
                    // move to the next line and reset the current height
                    pyramid += Environment.NewLine;
                    currentHeight = 0;
    
                    // check if we have reached the trough of the pyramid
                    if (requiredHeight.Equals(1))
                    {
                        // start growing
                        Grow(currentLetter, currentHeight, 2, lettersRemaining, ref pyramid);
                        return;
                    }
    
                    // otherwise we need to go one lower next time
                    requiredHeight--;
                }
    
                // process the next letter
                Shrink(currentLetter, currentHeight, requiredHeight, lettersRemaining, ref pyramid);
            }
    Called like this
    Code:
                String pyramid = String.Empty;
    
                // parameters
                // first letter, current height, required height, number of letters, the outputted pyramid
                Grow('a', 0, 1, 80, ref pyramid);
    It does not do 2 consecutive rows of height '1' though, as in your example. It goes 1 2 3 4 5 4 3 2 1 2 3...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured