simonradev

Matrix

Nov 7th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Second2DArray
  4. {
  5.     class Program
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             Console.Write("Enter the number of rows: ");
  10.             int rowsInMatrix = int.Parse(Console.ReadLine());
  11.             Console.Write("Enter the number of cols: ");
  12.             int colsInMatrix = int.Parse(Console.ReadLine());
  13.             int[,] matrix = new int[rowsInMatrix, colsInMatrix];
  14.            
  15.             //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]
  16.             //                                                                                                 [0,1], [1,1], [2,1], [3,1]
  17.             int outputValueForEven = 1;
  18.             int outputValueForOdd = 1;
  19.             for (int cols = 0; cols < matrix.GetLength(1); cols++)
  20.             {
  21.                 if (cols % 2 == 0)
  22.                 {
  23.                     outputValueForEven = outputValueForOdd;
  24.                     for (int rowsEven = 0; rowsEven < matrix.GetLength(0); rowsEven++)
  25.                     {
  26.                         matrix[rowsEven, cols] = outputValueForEven;
  27.                         outputValueForEven += 1;
  28.                     }
  29.                 }
  30.                 else
  31.                 {
  32.                     outputValueForOdd = outputValueForEven;
  33.                     for (int rowsOdd = matrix.GetLength(0) - 1; rowsOdd >= 0; rowsOdd--)
  34.                     {
  35.                         matrix[rowsOdd, cols] = outputValueForOdd;
  36.                         outputValueForOdd += 1;
  37.                     }
  38.                 }
  39.             }
  40.            
  41.             for (int rows = 0; rows < matrix.GetLength(0); rows++)
  42.             {
  43.                 for (int cols = 0; cols < matrix.GetLength(1); cols++)
  44.                 {
  45.                     Console.Write("{0} ", matrix[rows, cols]);
  46.                 }  
  47.                
  48.                 Console.WriteLine();
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment