Introduction to C# Programming: ASCII Xmas Tree - C# and .Net Sample Project


C#, pronounced C Sharp, is a relatively new programming language from Microsoft that was designed to take full advantage of the new .Net Framework.

Below is a sample project I did for a programming class I'm attending at MJC in Modesto. To test and run the code listed on this site you should download a free copy of Microsoft Visual C# Express 2005. There are four Visual Studio Express versions available for free from Microsoft, Visual Basic, C#, J# and C++.

The xmas tree project was a good idea. It really makes you think. I think this project could have been a little more fun for me if I had a little more time but my schedule just won't allow it. I don't see myself coming back to often to update this code but if others want to sent me some other samples I may post them later. So here it goes....

Last updated: Monday, December 4, 2006 8:00 PM

Screen Shot


This is what the final result should look like.

The Code

The purpose of this project as I understood it was to write a console program using C#. The goal is to draw a Christmas tree on the screen by making use of arrays and nested for loops. Of course, the one time I missed class all year and that was the day the instructor was discussing this new project. Luckily a fellow student took the time to go over it with me and this is what I came up with. Not sure if it is exactly correct but I'm sure it is close.

Download a copy of tree.exe, the executable file. You can run the code right from here by clicking on it or right click and choose save link as...

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{
class Program

{

static void Main(string[] args)

{

//create the main array

int[] myArray = new int[] { 1, 3, 5, 7, 9 };
//The outside foreach loop to loop throught the array

foreach (int intLoop in myArray)

{

//creates the spaces, takes the array number minus 1 then divide by 2

//this gives you the amount of spaces needed for each level of the tree

for (int iSpace = 0; iSpace < ((myArray[4]-intLoop)/2); iSpace++)

{

System.Console.Write(" ");

}

//middle loop writes the asterisks "*" the full amount of current array[]

for (int i = 0;i < intLoop; i++)

{

System.Console.Write("*");

}

//creates the spaces, takes the array number minus 1 then divide by 2

//this gives you the amount of spaces needed for each level of the tree

for (int iSpace = 0; iSpace < ((myArray[4] - intLoop) / 2); iSpace++)

{

System.Console.Write(" ");

}

//creates new lines after all 3 loops run

System.Console.WriteLine("");

}

//nest this loop and do it 3 times

for (int iBase = 0; iBase < myArray[1]; iBase++)

{

// now make the base of the tree

for (int iSpaces = 0; iSpaces < myArray[1]; iSpaces++)

{

System.Console.Write(" ");

}

for (int iPipes = 0; iPipes < myArray[1]; iPipes++)

{

System.Console.Write("|");

}

// now make the base of the tree

for (int iSpaces = 0; iSpaces < myArray[1]; iSpaces++)

{

System.Console.Write(" ");

}

//creates new lines after all 3 loops run

System.Console.WriteLine("");

}

}

}
}

Comments

There have been no comments made on this article. Why not be the first and add your own comment using the form below.

Leave a comment

Commenting is restricted to registered users only. Please register or login now to submit a comment.