Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

Struct - Tutorial C# Sharp
Tutorial C# Sharp4,85581245

Struct - Tutorial C# Sharp

Lesson 4:

Structures and Strings

Exercise 4.8:



Create a "struct" to store data of 2D points. The fields for each point will be:

x coordinate (short)
y coordinate (short)
r (red colour, byte)
g (green colour, byte)
b (blue colour, byte)

Write a program which creates two "points", asks the user for their data, and then displays their content.

Source Code:

using System;
public class PointAsStruct
    struct point
        public short x;
        public short y;
        public byte r;
        public byte g;
        public byte b;

    public static void Main()
        point p1, p2;

        Console.Write("Enter X for first point: ");
        p1.x = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Y for first point: ");
        p1.y = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Red for first point: ");
        p1.r = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Green for first point: ");
        p1.g = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Blue for first point: ");
        p1.b = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter X for second point: ");
        p2.x = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Y for second point: ");
        p2.y = Convert.ToInt16(Console.ReadLine());

        Console.Write("Enter Red for second point: ");
        p2.r = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Green for second point: ");
        p2.g = Convert.ToByte(Console.ReadLine());

        Console.Write("Enter Blue for second point: ");
        p2.b = Convert.ToByte(Console.ReadLine());

            "P1 is located in ({0},{1}), colour ({2},{3},{4})",
            p1.x, p1.y, p1.r, p1.g, p1.b);

            "P2 is located in ({0},{1}), colour ({2},{3},{4})",
            p2.x, p2.y, p2.r, p2.g, p2.b);
Exercisey 4.8

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.


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.