Objective
Write a C# program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first two points and then display them.
Write Your C# Exercise
C# Exercise Example
using System; // Import the System namespace for basic functionality
// Define the Point struct to store 2D point data and RGB color information
struct Point
{
public short x; // X-coordinate of the point (short type)
public short y; // Y-coordinate of the point (short type)
public byte r; // Red color component (byte type)
public byte g; // Green color component (byte type)
public byte b; // Blue color component (byte type)
}
class Program // Define the main class
{
static void Main() // The entry point of the program
{
// Define an array of Point structs with a size of 1000
Point[] points = new Point[1000];
// Ask the user for the data of the first point
Console.WriteLine("Enter data for Point 1:");
Console.Write("Enter X coordinate: ");
points[0].x = short.Parse(Console.ReadLine()); // Get the X coordinate for the first point
Console.Write("Enter Y coordinate: ");
points[0].y = short.Parse(Console.ReadLine()); // Get the Y coordinate for the first point
Console.Write("Enter Red color value (0-255): ");
points[0].r = byte.Parse(Console.ReadLine()); // Get the red color component for the first point
Console.Write("Enter Green color value (0-255): ");
points[0].g = byte.Parse(Console.ReadLine()); // Get the green color component for the first point
Console.Write("Enter Blue color value (0-255): ");
points[0].b = byte.Parse(Console.ReadLine()); // Get the blue color component for the first point
// Ask the user for the data of the second point
Console.WriteLine("\nEnter data for Point 2:");
Console.Write("Enter X coordinate: ");
points[1].x = short.Parse(Console.ReadLine()); // Get the X coordinate for the second point
Console.Write("Enter Y coordinate: ");
points[1].y = short.Parse(Console.ReadLine()); // Get the Y coordinate for the second point
Console.Write("Enter Red color value (0-255): ");
points[1].r = byte.Parse(Console.ReadLine()); // Get the red color component for the second point
Console.Write("Enter Green color value (0-255): ");
points[1].g = byte.Parse(Console.ReadLine()); // Get the green color component for the second point
Console.Write("Enter Blue color value (0-255): ");
points[1].b = byte.Parse(Console.ReadLine()); // Get the blue color component for the second point
// Display the contents of both points
Console.WriteLine("\nPoint 1 Data:");
Console.WriteLine($"X: {points[0].x}, Y: {points[0].y}, Color: RGB({points[0].r}, {points[0].g}, {points[0].b})");
Console.WriteLine("\nPoint 2 Data:");
Console.WriteLine($"X: {points[1].x}, Y: {points[1].y}, Color: RGB({points[1].r}, {points[1].g}, {points[1].b})");
}
}