C# language: 5. Arrays

in programming •  7 years ago 

CC.png

In the previous lesson we learn using loops, now we will learn how to save bigger numbers variables, if is a lot of variables, we can save it like this:

int a=124;
int b=123;
int c=12;
int d=1265;

Or that

int [] array = {124,123,12,1265};

One-dimensional arrays

Arrays in C # differ in a few respects from tables in C or C ++, for example:

int [] array;

The above code creates an empty array of int, it can not be done because we have not yet allocated this table in memory and we did not put any data there, we must first determine how big this array, we determined that there will be 6 values.

array = new int[6];

and we save these values to it

array[0]=12;
 
array[1]=15;
 
array[2]=20;
 
array[3]=43;
 
array[4]=35;
 
array[5]=98;

Cells are always numbered from zero, you can shorten the above record as I showed at the beginning, that is:

int [] array = {12,15,,20,43,35,98};

And below is a program that should explain everything:

static void Main(string[] args)
{
     Console.WriteLine("Enter the size of the array");
 
     int a = int.Parse(Console.ReadLine());
 
     int[] array = new int[a];
     int b = 0;
 
     for (int i=0;i<a;i++)
     {
         b += 5;
         array[i] =b;
         Console.WriteLine("The value of the array number "+i+" that "+array[i]);
     }
 
     Console.WriteLine("End of the loop");
 
     Console.ReadKey();
}
  1. First, we writing the inscription on the screen
  2. Then we have to specify the size of the array in the console
  3. Next, we declare an array called “array” with the size we gave
  4. Next, we create a variable named “b” of type int, which we will use in a loop
  5. Then we create the initial condition where i = 0, then the looping condition which says that the loop is to be executed until the size of the array is greater than the variable i and operation on the initial condition increases after each execution of the loop variable i by 1
  6. Then we increase the variable “b” by 5 every time we execute the loop then we put it into the table, so each value in the table will be about 5 more than the previous one
  7. And we display all the values of the table on the screen

Multidimensional arrays

The multidimensionals arrays differs only from ordinary arrays only in that they can have more rows, one-dimensional arrays always have one, I will explain it briefly with an example program:

static void Main(string[] args)
{
    Console.WriteLine("Enter the number of rows in the array");
    int rows = int.Parse(Console.ReadLine());
 
    Console.WriteLine("Enter the number of columns in the array");
    int columns = int.Parse(Console.ReadLine());
 
    int[,] array= new int[rows,columns];
 
    for (int i=0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            Console.WriteLine("Enter the value of the row number " + i + " and column number " + j);
            array[i, j] = int.Parse(Console.ReadLine());
            Console.WriteLine("Row number " + i + " an array with name 'array' and column number " + j+" are have a common value "+array[i,j]);
        }
    }
    Console.WriteLine("End of the loop");
 
    Console.ReadKey();
}

Rather, you should not have problems understanding this code, first enter the number of rows and columns, then make the first loop, the second loop will be done until you enter all the values in these columns, when you enter all values in columns, we move to the next row.

This content also you can find on my blog devman.pl: http://devman.pl/csharplan/language-c-5-arrays/

If you recognise it as useful, share it with others so that others can also use it and leave upvote and follow if you wait for next articles :) .

If anything is not understood, write in the comments.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!