Q:

C# | different types of two dimensional array declarations

belongs to collection: C# Basic Programs | array programs

0

C# | different types of two dimensional array declarations

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arrays2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Multi Dimension 

            Console.WriteLine("Type 1 : Declaration");
            int[,] arr1 = {
                            {1,2,3,4 },
                            {11,22,33,44}
                          };
            for(int i=0;i<2;i++)
            {
                for(int j=0;j<4;j++)
                {
                    Console.Write("{0}\t", arr1[i, j]);
                }
                Console.WriteLine();
            }


            Console.WriteLine("\n\nType 2 : Declaration");
            int[,] arr2 = new int[2, 5];
            Console.WriteLine("Enter {0} values:", 2 * 5);
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    arr2[i, j]=Convert.ToInt32(Console.ReadLine());
                }
            }
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write("{0}\t", arr2[i, j]);
                }
                Console.WriteLine();
            }

            Console.WriteLine("\n\nType 3 : Declaration");
            Console.Write("Enter Rows :");
            int rows = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter Cols :");
            int cols = Convert.ToInt32(Console.ReadLine());
            int[,] arr3 = new int[rows,cols];
            Console.WriteLine("Enter {0} values:", rows * cols);
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    arr3[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write("{0}\t", arr3[i, j]);
                }
                Console.WriteLine();
            }


            Console.ReadKey();
        }
    }
}

Output

Type 1 : Declaration
1       2       3       4
11      22      33      44


Type 2 : Declaration
Enter 10 values:
10
10
10
20
20
20
30
30
30
40
10      10      10      20      20
20      30      30      30      40


Type 3 : Declaration
Enter Rows :2
Enter Cols :3
Enter 6 values:
1
1
1
9
9
9
1       1       1
9       9       9

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# | Two dimensional array with fixed row size and... >>
<< C# | printing an integer array using foreach loop...