Array Of Struct - C# Programming Exercise

In this C# exercise, you are asked to expand the previous exercise that used a struct to store 2D points. Now, you need to store up to 1,000 points using an array of structs. The program should ask the user for data for the first two points and then display their content.

This expanded exercise will allow you to work with arrays of structs in C#, enabling you to store multiple instances of a structured data type. You will practice how to handle data efficiently using arrays and how to work with structs to represent 2D points. Additionally, it will help you understand how to organize and access large amounts of data more effectively in a program.

 Category

Arrays, Structures and Strings

 Exercise

Array Of Struct

 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

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})");
    }
}

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

  •  Array of struct and menu

    In this C# exercise, you are asked to expand the previous exercise (array of points) so that the program displays an interactive menu. The menu should a...

  •  Books database

    In this C# exercise, you are asked to create a small database that will be used to store information about books. For each book, the following data should be kept:

  •  Triangle V2

    In this C# exercise, you are asked to create a program that asks the user for their name and then displays a triangle made from that name, starting with 1 letter and ...

  •  Rectangle V3

    In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from that name.

  •  Centered triangle

    In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from t...

  •  Cities database

    In this exercise, you are asked to create a database to store information about cities. In the first approach, you will store only the name...