Exercise
Two Dimensional Array As Buffer For Screen
Objective
Write a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays the content of the array on screen.
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace for accessing Console and Random
class Program
{
static void Main()
{
// Declare a 70x20 two-dimensional array to represent the screen buffer
char[,] screenBuffer = new char[20, 70]; // 20 rows and 70 columns
// Fill the screen buffer with empty spaces initially
for (int row = 0; row < screenBuffer.GetLength(0); row++)
{
for (int col = 0; col < screenBuffer.GetLength(1); col++)
{
screenBuffer[row, col] = ' '; // Filling the array with empty spaces
}
}
// Create an instance of Random to generate random positions for the letters
Random rand = new Random();
// Draw 80 random 'X' characters in random positions on the screen buffer
for (int i = 0; i < 80; i++)
{
int row = rand.Next(0, 20); // Random row between 0 and 19
int col = rand.Next(0, 70); // Random column between 0 and 69
screenBuffer[row, col] = 'X'; // Place 'X' in the randomly chosen position
}
// Display the content of the screen buffer (the array)
for (int row = 0; row < screenBuffer.GetLength(0); row++)
{
for (int col = 0; col < screenBuffer.GetLength(1); col++)
{
Console.Write(screenBuffer[row, col]); // Print each character in the array
}
Console.WriteLine(); // Move to the next line after each row is printed
}
}
}