Thursday 19 July 2012

PROGRAMMING ARRAYS IN C#


Arrays are probably one of the most wanted topics in C#. The focus of this article is arrays in C#. The article starts with basic definitions of different array types and how to use them in our application. Later, the article covers the Arrays class and its methods, which can be used to sort, search, get, and set an array's items.

Introduction

In C#, an array index starts at zero. That means the first item of an array starts at the 0th position. The position of the last item on an array will total number of items - 1. So if an array has 10 items, the last 10th item is at 9th position.
In C#, arrays can be declared as fixed length or dynamic.
A fixed length array can store a predefined number of items.


Let's take a look at simple declarations of arrays in C#. The following code snippet defines the simplest dynamic array of integer types that does not have a fixed size.

int[] intArray;

As you can see from the above code snippet, the declaration of an array starts with a type of array followed by a square bracket ([]) and name of the array.

The following code snippet declares an array that can store 5 items only starting from index 0 to 4.

int[] intArray;

intArray = new int[5];


Example

1.Matrix addition &substraction

Void main()
{
Int i=0,j=0,row,col;
Int[,]a=new int[100,100];
Int[,]b=new int[100,100];
Int[,]result=new int[100,100];
Console.writeline(“enter the no.of rows”);
Row=convert.ToInt32(console.Readline());
Console.writeline(:enter the no.of columns”);
col=convert.ToInt32(console.Readline());
Console.writeline(“enter the 1st matrix”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
A[I,j]= convert.ToInt32(console.Readline());
}
}
Console.writeline(“enter the 2nd matrix”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
b[I,j]= convert.ToInt32(console.Readline());
}
}
Console.writeline(“after addition result is:”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Result[I,j]=a[I,j]+b[I,j];
}
}
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Console.writeline(“/t” result[I,j]);
}
Console.writeline();
}
Console.writeline(“after substraction result is”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Result[I,j]=a[I,j]-b[I,j];
}
}
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Console.writeline(“/t” result[I,j]);
}
Console.writeline();
Console.readline();
}
2.printstar
namespace PRINTSTAR
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, r, c, temp;
 
            Console.WriteLine("enter the no.of rows");
            n = Convert.ToInt32(Console.ReadLine());
            temp = n;
            for (r = 1; r <= n; r++)
            {
                for (c = 1; c < temp; c++)
                {
                    Console.Write(" ");
                }
                temp--;
                for (c = 1; c <=2 * r - 1; c++)
                {

                    Console.Write("*");
                }
                Console.WriteLine();
            }

                Console.ReadLine();
           
        }
    }
}
Output
      *
  *     *
*    *    *

No comments:

Post a Comment