Drawing a Circumference in a 2D Array in C#

In this C# program, we declare a 70x20 two-dimensional array of characters and "draw" a circumference with a radius of 8 inside it. The program calculates the points that make up the circumference using the mathematical formula for a circle and plots them on the 2D array. The program then displays the array with the drawn circumference on the screen. This exercise demonstrates the use of trigonometric functions and array manipulation in C#.



Group

C# Arrays, Structures and Strings

Objective

1. Declare a 70x20 two-dimensional array.
2. Use the mathematical formula to calculate the points on the circumference with radius 8.
3. Convert the angle from degrees to radians.
4. Use `Math.Cos` and `Math.Sin` to calculate the x and y positions of the points on the circumference.
5. Display the 2D array, showing the drawn circumference.

Write a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it on screen.

Example C# Exercise

 Copy C# Code
using System;

class Program
{
    static void Main()
    {
        // Define a 70x20 two-dimensional array
        char[,] grid = new char[70, 20];

        // Define the center of the circumference
        int xCenter = 35; // Horizontal center of the array (middle)
        int yCenter = 10; // Vertical center of the array (middle)

        // Define the radius of the circumference
        int radius = 8;

        // Initialize the array with empty spaces (' ')
        for (int i = 0; i < 70; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                grid[i, j] = ' '; // Empty space
            }
        }

        // Draw the circumference: 72 points (360 degrees / 5 degrees step)
        for (int angle = 0; angle < 360; angle += 5)
        {
            // Convert angle to radians
            double radians = angle * Math.PI / 180.0;

            // Calculate the x and y coordinates for the circumference
            int x = (int)(xCenter + radius * Math.Cos(radians));
            int y = (int)(yCenter + radius * Math.Sin(radians));

            // Ensure the coordinates are within bounds of the array
            if (x >= 0 && x < 70 && y >= 0 && y < 20)
            {
                grid[x, y] = 'X'; // Mark the point on the grid
            }
        }

        // Display the content of the array
        Console.WriteLine("The grid with a circumference of radius 8:");
        for (int i = 0; i < 20; i++)
        {
            for (int j = 0; j < 70; j++)
            {
                Console.Write(grid[j, i]); // Print each character
            }
            Console.WriteLine(); // New line after each row
        }
    }
}

 Output

The grid with a circumference of radius 8:
                                                                              
                                                                              
                           X                                                    
                        X     X                                                
                      X           X                                          
                     X             X                                         
                      X           X                                          
                        X     X                                                
                           X                                                    
                                                                              
                                                                              
                                                                              
                                                                              
                                                                              
                                                                              
                                                                              
                                                                              
                                                                              
                                                                              

Share this C# Exercise

More C# Practice Exercises of C# Arrays, Structures and Strings

Explore our set of C# Practice 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#.

  • Managing Computer Program Records in C#

    In this C# program, we will create a system to store and manage up to 1000 records of computer programs. Each program will have the following attributes: Name, Category, Descriptio...

  • Managing To-Do Tasks in C#

    This C# program helps to manage a list of to-do tasks by storing up to 2000 tasks. For each task, the program keeps track of the date (day, month, year), a description of the task,...

  • Domestic Accounting System in C#

    This C# program is designed to manage expenses and revenues for a small domestic accounting system. The program allows the user to store up to 10,000 records for expenses and reven...

  • Displaying Numbers in Reverse Order in C#

    This exercise focuses on working with arrays in C#. The program will prompt the user to enter 5 numbers, which will be stored in an array. Once all the numbers have been entered, t...

  • Checking if a Value Exists in a Previously Entered List in C#

    This exercise involves creating and searching within a list of floating-point numbers in C#. The program will first ask the user how many numbers they want to enter and store them ...

  • Displaying Even Numbers from a List of 10 Integers in C#

    This exercise focuses on working with arrays and conditionals in C#. The program will prompt the user to enter 10 integer numbers, store them in an array, and then display only the...