Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Second2DArray
- {
- class Program
- {
- public static void Main(string[] args)
- {
- Console.Write("Enter the number of rows: ");
- int rowsInMatrix = int.Parse(Console.ReadLine());
- Console.Write("Enter the number of cols: ");
- int colsInMatrix = int.Parse(Console.ReadLine());
- int[,] matrix = new int[rowsInMatrix, colsInMatrix];
- //the rows and cols are upside down because the way i want the matrix to be ordered is as follows: [0,0], [1,0], [2,0], [3,0]
- // [0,1], [1,1], [2,1], [3,1]
- int outputValueForEven = 1;
- int outputValueForOdd = 1;
- for (int cols = 0; cols < matrix.GetLength(1); cols++)
- {
- if (cols % 2 == 0)
- {
- outputValueForEven = outputValueForOdd;
- for (int rowsEven = 0; rowsEven < matrix.GetLength(0); rowsEven++)
- {
- matrix[rowsEven, cols] = outputValueForEven;
- outputValueForEven += 1;
- }
- }
- else
- {
- outputValueForOdd = outputValueForEven;
- for (int rowsOdd = matrix.GetLength(0) - 1; rowsOdd >= 0; rowsOdd--)
- {
- matrix[rowsOdd, cols] = outputValueForOdd;
- outputValueForOdd += 1;
- }
- }
- }
- for (int rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (int cols = 0; cols < matrix.GetLength(1); cols++)
- {
- Console.Write("{0} ", matrix[rows, cols]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment