Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FillPrintMatrix
- {
- class Program
- {
- static void Main()
- {
- Console.Write("Please enter the matix size: ");
- string input = Console.ReadLine();
- int N;
- int digit = 1;
- while (int.TryParse(input, out N) == false)
- {
- Console.Write("Please enter a valid integer!: ");
- input = Console.ReadLine();
- }
- int[,] Matrix = new int[N, N];
- Console.WriteLine();
- //// Variant A:
- for (int col = 0; col < N; col++)
- {
- for (int row = 0; row < N; row++)
- {
- Matrix[row, col] = digit; //Fill in the matrix
- digit++;
- }
- }
- Console.WriteLine("This is variant A:");
- for (int row = 0; row < N; row++)
- {
- for (int col = 0; col < N; col++)
- {
- Console.Write("{0,4}", Matrix[row, col]); //Print the matrix
- }
- Console.WriteLine();
- }
- digit = 1;
- Console.WriteLine();
- // Variant B:
- for (int col = 0; col < N; col++)
- {
- if(col % 2 == 0)
- {
- for (int row = 0; row < N; row++)
- {
- Matrix[row, col] = digit; //Fill in the matrix
- digit++;
- }
- }
- else
- {
- for (int row = N-1; row >= 0; row--)
- {
- Matrix[row, col] = digit; //Fill in the matrix
- digit++;
- }
- }
- }
- Console.WriteLine("This is variant B:");
- for (int row = 0; row < N; row++)
- {
- for (int col = 0; col < N; col++)
- {
- Console.Write("{0,4}", Matrix[row, col]); //Print the matrix
- }
- Console.WriteLine();
- }
- digit = 1;
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement