Write a program to define a two dimensional array of numbers to store 5 rows and 6 columns.
Write a code to accept the data, assign it in array, and print the data entered by the user.
using System;
namespace two_dim_arr
{
class Program
{
public static void Main(string[] args)
{
int[,] array = new int[5,6];
string[] tokens;
string str;
Console.WriteLine("Enter array:");
for (int i = 0; i<5; i++)
{
Console.WriteLine("Enter row {0} (sep. space):", i+1);
str = Console.ReadLine();
tokens = str.Split(' ');
for (int j=0; j<6; j++)
array[i,j]= Convert.ToInt32(tokens[j]);
}
Console.WriteLine();
Console.WriteLine("Two dimensional array:");
for (int i = 0; i<5; i++)
{
for (int j = 0; j<6; j++)
Console.Write("{0} ", array[i,j]);
Console.WriteLine();
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment