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 and menu - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

Array of struct and menu - Practice Exercises C# Sharp


Lesson 4:

Structures and Strings


Exercise 4.10:

Array of struct and menu


Objetive:

Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to:

- Add data for one point
- Display all the entered points
- Calculate (and display) the average values for x and y
- Exit the program


Source Code:


using System;
class Program
{
    struct points
    {
        public byte x;
        public byte y;
    }
    static void Main(string[] args)
    {
        points[] p = new points[1000];
        bool Finish = false;
        int countArray = 0;
        int TotalX = 0;
        int TotalY = 0;
        do
        {
            Console.WriteLine("1.Add data for one point");
            Console.WriteLine("2.Display all the entered points");
            Console.WriteLine("3.Calculate (and display) the average values for x and y");
            Console.WriteLine("0.Exit the program");
            Console.Write("Enter a number: ");
            byte respuesta = Convert.ToByte(Console.ReadLine());
            switch (respuesta)
            {
                case 1:
                    Console.WriteLine();
                    Console.Write("Enter a number for point x: ");
                    p[countArray].x = Convert.ToByte(Console.ReadLine());
                    TotalX += p[countArray].x;
                    Console.WriteLine();
                    Console.Write("Enter a number for point y: ");
                    p[countArray].y = Convert.ToByte(Console.ReadLine());
                    TotalY += p[countArray].y;
                    Console.WriteLine();
                    countArray++;
                    break;
                case 2:
                    if (countArray > 0)
                    {
                        for (int i = 0; i < countArray; i++)
                        {
                            Console.WriteLine("Valor x{0}: {1}", i + 1, p[i].x);
                            Console.WriteLine("Valor y{0}: {1}", i + 1, p[i].y);
                        }
                    }
                    else
                        Console.WriteLine("No hay datos");
                    break;
                case 3:
                    if (countArray > 0)
                    {
                        Console.WriteLine("Average x: {0}", TotalX / countArray);
                        Console.WriteLine("Average y: {0}", TotalY / countArray);
                    }
                    else
                        Console.WriteLine("No hay datos");
                    break;
                case 0:
                    Finish = true;
                    break;
                default:
                    break;
            }
        }
        while (!Finish);
    }
}
Exercisey 4.10

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.