Skip to content

Repetitive Lyrics Using Arrays & Nested Loop

Lyrics streamlined with arrays and a nested loop.

A variation on the solution at rosettacode.org that uses arrays, a nested loop, and composite formatting to print out the repetitive lyrics to the song "There Was an Old Lady Who Swallowed a Fly".

Screenshot

Repetitive Lyrics

Repetitive Lyrics Code

using System;

namespace Song
{
    class Program
    {
        private const string reason = "She swallowed the {0} to catch the {1}";
        private static readonly string[] creatures = { "fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse" };

        private static readonly string[] comments =
        {
            "I don't know why she swallowed that fly.\nDon't ask me why!\n",
            "That wiggled and jiggled and tickled inside her",
            "How absurd, to swallow a bird",
            "Imagine that. She swallowed a cat",
            "What a hog to swallow a dog",
            "She just opened her throat and swallowed that goat",
            "I don't know how she swallowed that cow",
            "She's full of course!"
        };

        private static void Main()
        {
            Console.Title = "There was an old lady...";
            Console.WindowHeight = Console.LargestWindowHeight-20;
            Header();
            int max = creatures.Length;
            for (int i = 0; i < max; i++)
            {
                Console.WriteLine("There was an old lady who swallowed a {0}", creatures[i]);
                Console.WriteLine(comments[i]);
                for (int j = i; j > 0 && i < max - 1; j--)
                {
                    Console.WriteLine(reason, creatures[j], creatures[j - 1]);
                    if (j == 1)
                    {
                        Console.WriteLine(comments[j - 1]);
                    }
                }
            }
            Console.Read();
        }

        private static void Header()
        {
            string header = @"
The                                  )*(
   Old Lady          .-.     .-.     .           
           Who... _.'   `._.'   `._.'       
";
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(header);
            Console.ResetColor();
        }
    }
}

Changes

Small changes to the original at rosettacode.org:

  • Changed the window height: Console.WindowHeight = Console.LargestWindowHeight-20;
  • Added a header function to write out the title in a different color with ASCII decoration: Header();
  • Changed last line to be: "She's full of course!"
  • Added a title to the application's title bar: Console.Title = "There was an old lady...";

Without the window height change the application opens in a shorter window cutting off part of the song. Setting the window height to a fixed number of rows (for example, 70) doesn't always work because the height is in rows, not pixels, and the amount of rows that can be shown will vary with resolution. Console.LargestWindowHeight is the height of the largest possible console window measured in rows, so we can use that number to make our window taller and show more of the lyrics. Subtracting 20 makes the height smaller than the full monitor screen; for most resolutions this is a good compromise between showing the full lyrics and keeping the application window a manageable size.