ideas | discover | explore

Friday, October 28, 2011

C# 12 - Multidimensional Arrays - Multiplication Table

Manage and share your stuff. Sign up now for FREE!
meestuf.com

-----------------------------------------------------------------------------------------------------

1. Create a new console project.

2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
static void Main(string[] args)
        {
            int[,] multiplicationTable = new int[11,11];
 
            for (int i = 1; i < 11; i++)
                for (int x = 1; x < 11; x++)
                    multiplicationTable[i, x] = i * x;
 
            Console.Write("x".PadRight(3) + "|");
            for (int i = 1; i < 11; i++)
            {
                Console.Write(i.ToString().PadRight(3) + "|");
            }
            Console.Write("\n");
            Console.WriteLine("--------------------------------------------");
 
            for (int i = 1; i < 11; i++)
            {
                Console.Write(i.ToString().PadRight(3) + "|");
                for (int x = 1; x < 11; x++)
                {
                    Console.Write(multiplicationTable[i, x].ToString().PadRight(3) + "|");
                }
                Console.Write("\n");
                Console.WriteLine("--------------------------------------------");
            }
 
            Console.ReadLine();
       }       

3.       Press F5 to run and see the results.


4. Thank you.

Rectangular array is another type of array that allows you to define a table like array that has a row and column definition.

Syntax:

type[,] = new type[row,column];

type – can be any data type
row – is the number of rows for your table
column – is the number of columns for your table

e.g.
                int[,] mytable = new int[10, 5];

-----------------------------------------------------------------------------------------------------
Manage and share your stuff. Sign up now for FREE!
meestuf.com

Thursday, October 13, 2011

C# 11 - Arrays

An array of sorts.

1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
static void Main(string[] args)
{
     //declaring and initializing an array
     string[] theAvengers = new string[5];
     theAvengers[0] = "Iron Man";
     theAvengers[1] = "Thor";
     theAvengers[2] = "Captain America";
     theAvengers[3] = "Hawkeye";
     theAvengers[4] = "Hulk";
 
     Console.WriteLine("The Avengers!");
     Console.WriteLine("-------------------------");
     //loop through an array to access or modify its contents
     for (int i = 0; i < 5; i++)
     {
         Console.WriteLine(theAvengers[i]);
     }
 
     //another way of declaring and initializing an array
     //also another way is
    //string[] uncannyXMen = new string[] { "Wolverine", "Prof. X", "Cyclops", "Storm", "Beast" };
     string[] uncannyXMen = { "Wolverine""Prof. X""Cyclops""Storm""Beast" };
             
     Console.WriteLine("\nThe X-Men!");
     Console.WriteLine("-------------------------");
     //loop through an array to access in contents
     for (int i = 0; i < uncannyXMen.Length; i++)
     {
          Console.WriteLine(uncannyXMen[i]);
     }
 
     Console.WriteLine("Press any key to continue...");
     Console.ReadLine();
}

3. Press F5 to run and see the results.

4. Thank you.

Declaring an array allows you to store multiple items of a specific type. It syntax is:

type[] arrayName;

To learn more about arrays check these resources.

Friday, October 7, 2011

C# 10 - do-while-loop statement

1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
   static void Main(string[] args)
        {
            int counter = 0;            
            while (counter < 10)
            {
                Console.WriteLine("Hello World from a while loop.");
                counter++;
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
            }
3. Press F5 to run and see the results.

5. Thank you.

The do- while-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. It executes code at least once. Its syntax is:

do
{
                //code to execute
}
while(<condition>);

Thursday, October 6, 2011

C# 9 - while-loop statement

1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.

    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            
            while (counter < 10)
            {
                Console.WriteLine("Hello World from a while loop.");
                counter++;
            }
 
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }

3. Press F5 to run and see the results.

4. Thank you.


The while-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. Its syntax is:

while(<condition>)
{
    //code to execute
}