Matriz de estructura Aprende programación con ejercicios C# Sharp

Lección:

Matrices, Estructuras y Cadenas


Ejercicio:

Matriz de estructura 61


Objetivo:

Expanda el ejercicio anterior (punto de estructura), de modo que se puedan almacenar hasta 1.000 puntos, utilizando una "matriz de estructura". Pida al usuario datos para los dos primeros puntos y luego muéstrelos.


Código:

using System;
public class exercise80
{
    struct point
    {
        public short x;
        public short y;
        public byte r;
        public byte g;
        public byte b;
    }

    public static void Main()
    {
        point[] p = new point[1000];


        Console.Write("Enter X for first point: ");
        p[0].x = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Y for first point: ");
        p[0].y = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Red for first point: ");
        p[0].r = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Green for first point: ");
        p[0].g = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Blue for first point: ");
        p[0].b = Convert.ToByte(Console.ReadLine());


        Console.Write("Enter X for second point: ");
        p[1].x = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Y for second point: ");
        p[1].y = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Red for second point: ");
        p[1].r = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Green for second point: ");
        p[1].g = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Blue for second point: ");
        p[1].b = Convert.ToByte(Console.ReadLine());


        Console.WriteLine(
            "P1 is located in ({0},{1}), colour ({2},{3},{4})",
            p[0].x, p[0].y, p[0].r, p[0].g, p[0].b);

        Console.WriteLine(
            "P2 is located in ({0},{1}), colour ({2},{3},{4})",
            p[1].x, p[1].y, p[1].r, p[1].g, p[1].b);
    }
}