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