Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

Array of struct - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,55302940

Array of struct - Practice Exercises C# Sharp


Lesson 4:

Structures and Strings


Exercise 4.9:

Array of struct


Objetive:

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.


Source Code:


using System;
public class ArrayOfStruct
{
    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);
    }
}
Exercisey 4.9

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.