C#.Net find output programs (Arrays) | set 2

Find the output of C#.Net programs | Arrays | Set 2: Enhance the knowledge of C#.Net Arrays concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 06, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int [,]arr;

            arr = new int[2,2];

            for (int I = 0; I < 2; I++)
            {
                for (int J = 0; J < 2; J++)
                    arr[I, J] = I + J;
            }

            for (int I = 0; I < 2; I++)
            {
                for (int J = 0; J < 2; J++)
                    Console.WriteLine(arr[I, J] + " ");
            }
        }
    }
}

Output:

0 1 1 2 
Press any key to continue . . .

Explanation:

In the above program, we created a two-dimensional array arr of size 2X2,

for (int I = 0; I < 2; I++)
{
    for (int J = 0; J < 2; J++)
        arr[I, J] = I + J;
}

In the above nested loop, we assign value to the TWO-D array,

Arr[0,0] = 0 +0 = 0;
Arr[0,1] = 0 +1 = 1;
Arr[1,0] = 1 +0 = 1;
Arr[1,1] = 1 +1 = 0;

Now,

for (int I = 0; I < 2; I++)
{
    for (int J = 0; J < 2; J++)
        Console.WriteLine(arr[I, J] + " ");
}

The above nested loop will print elements of TWO-D array.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int[][] arr = new int[][] 
                            {
                                new int[] {10, 11, 12, 13},
                                new int[] {14, 15},
                                new int[] {16, 17,18},
                                new int[] {19, 20,21,22,23}
                            };


            for (int I = 0; I < arr.Length; I++)
            {
                for (int J = 0; J < arr[I].Length; J++)
                    Console.Write(arr[I][J] + " ");
                Console.WriteLine();
            }
        }
    }
}

Output:

10 11 12 13
14 15
16 17 18
19 20 21 22 23
Press any key to continue . . .

Explanation:

In the above program, we created a 2D jagged array. A 2D jagged array contain different number of elements in the different rows.

for (int I = 0; I < arr.Length; I++){
    for (int J = 0; J < arr[I].Length; J++)
        Console.Write(arr[I][J] + " ");
    Console.WriteLine();
}

The above nested loop is used to print elements of a jagged array. here we used Length property with row index, which is given below. Because every row may have a different number of elements.

arr[I].Length

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int[,] arr = new int[3,3] ;

            arr[0, 0] = 10;
            arr[0, 1] = 20;
            arr[0, 2] = 30;

            arr[1, 0] = 40;
            arr[1, 1] = 50;
            arr[1, 2] = 60;

            arr[2, 0] = 70;
            arr[2, 1] = 80;
            arr[2, 2] = 90;

            for (int I = 0; I < arr.GetLength(0); I++)
            {
                for (int J = 0; J < arr.GetLength(1); J++)
                    Console.Write(arr[I][J] + " ");
                Console.WriteLine();
            }
        }
    }
}

Output:

main.cs(27,38): error CS0022: Wrong number of indexes `1' inside [], expected `2'

Explanation:

The above program will generate syntax error because we used in-correct syntax to access elements of two-d array in the below statement,

Console.Write(arr[I][J] + " ");

Here we are trying to access the normal two-d array in jagged array style.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int X = 0;
            int[,] arr = new int[3,3] ;

            arr[0, 0] = 10;
            arr[0, 1] = 20;
            arr[0, 2] = 30;

            arr[1, 0] = 40;
            arr[1, 1] = 50;
            arr[1, 2] = 60;

            arr[2, 0] = 70;
            arr[2, 1] = 80;
            arr[2, 2] = 90;

            for (int I = 0; I < arr.GetLength(0); I++)
            {
                for (int J = 0; J < arr.GetLength(1); J++)
                {
                    if (I == J)
                        X += arr[I, J];
                }
            }

            Console.WriteLine(X);
        }
    }
}

Output:

150
Press any key to continue . . .

Explanation:

In the above program, we created a two-d array arr with size 3X3, and created a local variable X and initialized with 0, Then we assigned values for each element of the two-d array,

In the above nested loop, we add value of left diagonal elements to the variable X. Elements arr[0,0], arr[1,1] and arr[1,2] will be added to the X.

Initially X=0.

When I=0, J=0
X += arr[0,0];
X = X+arr[0,0];
X = 0 + 10;
X = 10;

When I=1, J=1 and X=10
X += arr[1,1];
X = X+arr[1,1];
X = 10 + 50;
X = 60;

When I=2, J=2 and X=60
X += arr[2,2];
X = X+arr[2,2];
X = 60 + 90;
X = 150;

Then finally "150" will be printed on the console screen.

Note: The GetLength() method is used to get length based on dimension. Here dimension 0 is used for Rows and dimension 1 is used for columns.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int X = 0;
            int[] arr = {5,8,9,11,13} ;

            for (int I = 0; I < arr.GetLength(0); I++)
            {
                X = 0;
                for (int j = 2; j < (arr[I] / 2); j++)
                {
                    if (arr[I] % j == 0)
                    {
                        X = 1;
                        break;
                    }
                }

                if (X == 0)
                    Console.WriteLine(arr[I]);
            }
        }
    }
}

Output:

5
11
13
Press any key to continue . . .

Explanation:

In the above program, we created an integer array. Here we find prime numbers from the array and print them on the console screen.

Prime numbers are those numbers that can only divide by 1 or itself. But 2 is the lowest prime number.

In the given array 5, 11, and 13 are prime numbers.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.