Hello,

I am at the end of my first C# programming class. I am working on some extra credit. Having problems with my loops. I am supposed to get output looking exactly like this:


Bark
Bark Meow
Bark Meow Bark
Bark Meow Bark Meow
Bark Meow Bark Meow Bark
Bark Meow Bark Meow Bark Meow
Bark Meow Bark Meow Bark Meow Bark
Bark Meow Bark Meow Bark Meow Bark Meow
Bark Meow Bark Meow Bark Meow Bark Meow Bark
Bark Meow Bark Meow Bark Meow Bark Meow Bark Meow

using loops and stringbuilder. Here is one version of many I have tried:

static void Main(string[] args)
        {
            //declare strings
            string cat = "Meow ";
            string dog = "Bark ";
                     
            int row;
            int column;

            StringBuilder catsNdogs = new StringBuilder();

            for (row = 1; row <= 10; row++)
            {
                catsNdogs.Append(dog);
                          
                for (column = 1; column < row; column++)
                
                    catsNdogs.Append(cat);
                
                Console.WriteLine(catsNdogs.ToString());
            }

        
        }

I have searched this site and google for help to no avail. Forgive any indiscretions, still very new at this. Thank you for the help!!!

Recommended Answers

All 5 Replies

This is the code (in Java, if you don't know it don't worry, it is very similar so you won't have trouble understanding) for creating the wanted output with loops, hopefully this will help your project:

public static void main(String[] args)
{
   String str = null;
   boolean needToBark = true;
   int row = 10;
   for (int i = 0; i < row; ++i)
   {
      str = "";
      for(int j = 0; j < i + 1; ++j)
      {
         if(needToBark)
	 {
	    str += "Bark ";
	 }
	 else
	 {
	    str += "Meow ";
	 }
	 needToBark = !needToBark;
      }
      System.out.println(str);
      needToBark = true;
      str = "";
   }
}

Hello,

Sorry I took so long to get back to this. Thanks for the info. Here is how I adjusted it to what I am trying to do. But it just gave me an endless steam of Bark . .
So I must have messed something up.

static void Main(string[] args)
        {
            //declare strings
            string cat = "Meow ";
            string dog = "Bark ";
            Boolean needToBark = true;
                                 
            int row = 10;
            int column;

            StringBuilder catsNdogs = new StringBuilder();

            for (column = 0; column < row; ++row)
            {
                catsNdogs.Append(dog);

                for (int j = 0; j < column + 1; ++j)
                {
                    if (needToBark)
                    {
                        catsNdogs.Append(dog);
                    }
                    else
                    {
                        catsNdogs.Append(cat);
                    }
                    needToBark = !needToBark;
                }
                Console.WriteLine(catsNdogs.ToString());
                needToBark = true;

            }

After you finish the inner loop, you need to erase the information inside the String Builder, or to adjust the loops to append just the needed string. Right now you are appending "Bark" at the first line, then in the second line you append to the same string "Bark Meow" and so on - it will create a long string.

Yayyyy, I finally got it to work. I set an array for row with 10 elements and did a foreach loop. Made a couple other changes and voila! Thank you for the inspiration using bool! I would post it, but not sure I should since it is a current challenge for a class. What is the best practice for that?

Thank you again!!!

Well, if your code works, there is no reason to post it again I guess.
Glad I could inspire :) Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.