Two Dimensional Array As Buffer For Screen - C# Programming Exercise

This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" 80 letters (e.g., 'X') in random positions within the array. The program then must display the content of the array on the screen. The goal of this Exercise is to practice using two-dimensional arrays and manipulating data within such a structure in C#, as well as learning how to work with random positions within an array.

In this Exercise, the program will declare a character array with 70 rows and 20 columns. It will then generate 80 random positions within the array to place the letter 'X'. Finally, the program will display the full content of the array on the screen, allowing the user to see how the letters are randomly distributed.

This type of Exercise is ideal for practicing the manipulation of two-dimensional arrays in C#, as well as understanding the concept of random number generation and its application for assigning values to positions within a data structure. Additionally, it helps strengthen the logic of how to work with arrays and how to present data efficiently on the screen.

 Category

Arrays, Structures and Strings

 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

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
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Two dimensional array 2: circunference on screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" a circumference with a radius of 8 inside it. To...

  •  Computer programs

    Exercise in C# involves creating a program that can store up to 1000 records of computer programs. For each program, the following data should be stored: name,...

  •  Exercise tasks

    This exercise in C# consists of creating a program that can store up to 2000 "to-do tasks". Each task must store the following data: • Date (a set of 3 data: ...

  •  Household accounts

    This exercise in C# consists of creating a program that can store up to 10,000 costs and revenues, to create a small domestic accounting system. For eac...

  •  Reverse array

    In this C# exercise, you are asked to write a program that prompts the user for 5 numbers, stores them in an array, and displays them in reverse order. ...

  •  Search in array

    In this C# exercise, you are asked to write a program that checks if a given data belongs to a previously created list. The steps to follow are: