Two Dimensional Array 2: Circunference On Screen - C# Programming Exercise

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 perform this Exercise, mathematical formulas are used to calculate the points on the circumference in the Cartesian plane: x = xCenter + r * cos(angle) and y = yCenter + r * sin(angle), where "r" is the radius of the circumference, "xCenter" and "yCenter" are the coordinates of the center, and the angle is measured in radians. The main goal of this Exercise is to apply trigonometry knowledge to draw geometric shapes inside a two-dimensional array in C#.

In this Exercise, the program should create a 70x20 character array and then calculate the positions of the points of a circumference with a radius of 8 using Math.Cos and Math.Sin functions in C#. To represent the circumference, 72 points must be calculated (since there are 360 degrees in a circle, the points will be spaced 5 degrees apart). Finally, the program should display the array on the screen, showing how the circumference is drawn.

This type of Exercise is helpful for practicing applied mathematics concepts, such as using trigonometry to draw geometric shapes, and manipulating two-dimensional arrays in C#. It also reinforces understanding of how to convert between degrees and radians, as well as using mathematical functions like Math.Cos and Math.Sin.

 Category

Arrays, Structures and Strings

 Exercise

Two Dimensional Array 2: Circunference On Screen

 Objective

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.

Hint: the points in the circumference can be obtained using:
x = xCenter + r * cos angle
y = yCenter + r * sin angle

"sin" and "cos" expect the angle to be measured in radians, instead of degrees. To convert from one unit to the other, you must remember that 360 degrees = 2 PI radians (or 180 degrees = PI radians): float radians = (angle * Math.PI / 180.0);

You might draw 72 points (as there are 360 degrees in a circumference, they would be spaced 5 degreees from each other)

Hint: in C#, cosine is Math.Cos, sine is Math.Sin and PI is Math.PI

 Write Your C# Exercise

using System;  // Importing the System namespace for accessing Console, Math, and other functionalities

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

        // Define the center of the circle and the radius
        int xCenter = 35;  // Center column (x-coordinate) of the screen buffer
        int yCenter = 10;  // Center row (y-coordinate) of the screen buffer
        int radius = 8;    // Radius of the circle

        // Draw the points of the circle using the parametric equations for a circle
        for (int angle = 0; angle < 360; angle += 5)  // Loop through 360 degrees with 5 degree increments
        {
            // Convert angle to radians
            double radians = (angle * Math.PI / 180.0);

            // Calculate the x and y coordinates of the point on the circumference
            int x = (int)(xCenter + radius * Math.Cos(radians));  // Calculate x-coordinate
            int y = (int)(yCenter + radius * Math.Sin(radians));  // Calculate y-coordinate

            // Place the point 'O' in the calculated position on the screen buffer
            if (x >= 0 && x < screenBuffer.GetLength(1) && y >= 0 && y < screenBuffer.GetLength(0))
            {
                screenBuffer[y, x] = 'O';  // Mark the point on the screen buffer
            }
        }

        // 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#.

  •  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:

  •  Array of even numbers

    In this C# exercise, you are asked to write a program that asks the user for 10 integer numbers and displays the even ones. This exercise w...