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()
{
char[,] grid = new char[70, 20];
int xCenter = 35;
int yCenter = 10;
int radius = 8;
for (int i = 0; i < 70; i++)
{
for (int j = 0; j < 20; j++)
{
grid[i, j] = ' ';
}
}
for (int angle = 0; angle < 360; angle += 5)
{
double radians = angle * Math.PI / 180.0;
int x = (int)(xCenter + radius * Math.Cos(radians));
int y = (int)(yCenter + radius * Math.Sin(radians));
if (x >= 0 && x < 70 && y >= 0 && y < 20)
{
grid[x, y] = 'X';
}
}
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]);
}
Console.WriteLine();
}
}
}
Output
The grid with a circumference of radius 8:
X
X X
X X
X X
X X
X X
X