Computer Programs C# Sharp Exercise


Computer Programs


Create a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data:

* Name
* Category
* Description
* Version (is a set of 3 data: version number -text-, launch month -byte- and launch year -unsigned short-)

The program should allow the user the following operations:
1 - Add data of a new program (the name must not be empty, category must not be more than 30 letters (or should be re-entered), and for the description, it will accept only the first 100 letters entered and skip the rest; the version needs no validation).
2 - Show the names of all the stored programs. Each name must appear on one line. If there are more than 20 programs, you must pause after displaying each block of 20 programs, and wait for the user to press Enter before proceeding. The user should be notified if there is no data.
3 - View all data for a certain program (from part of its name, category or description, case sensitive). If there are several programs that contain that text, the program will display all of them, separated by a blank line. The user should be notified if there are no matches found.
4 - Update a record (asking the user for the number, the program will display the previous value of each field, and the user can press Enter not to modify any of the data). He should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.
5 - Delete a record, whose number will be indicated by the user. He should be warned (but not asked again) if he enters an incorrect number.
6 - Sort the data alphabetically by name.
7 - Fix redundant spaces (turn all the sequences of two or more spaces into a single space, only in the name, for all existing records).
X - Exit the application (as we do not store the information, data will be lost).

Example Code

using System;
public class exercise94
    struct versionType
        public string number;
        public byte month;
        public ushort year;

    struct program
        public string name;
        public string category;
        public string description;
        public versionType version;

    public static void Main()
        int capacity = 1000;
        program[] data = new program[capacity];
        bool repeat = true;
        string option;
        int counter = 0;

            Console.WriteLine("Computer programas database");
            Console.WriteLine("1.- Add data.");
            Console.WriteLine("2.- View names of the programs.");
            Console.WriteLine("3.- Search programs.");
            Console.WriteLine("4.- Modify program.");
            Console.WriteLine("5.- Delete Program.");
            Console.WriteLine("6.- Sort alphabetically");
            Console.WriteLine("7.- Fix redundant spaces");
            Console.Write("Option: ");
            option = Console.ReadLine();

            switch (option)
                case "1": //add
                    if (counter > capacity - 1)
                        Console.WriteLine("Database full!");

                        Console.Write("Enter Name: ");
                        data[counter].name = Console.ReadLine();
                        if (data[counter].name.Length == 0)
                            Console.Write("Cannot be empty");
                    } while (data[counter].name.Length == 0);

                        Console.Write("Enter category: ");
                        data[counter].category = Console.ReadLine();
                        if (data[counter].category.Length > 30)
                            Console.Write("Too long. 30 letters max, please");
                    } while (data[counter].category.Length > 30);

                    Console.Write("Enter Description: ");
                    data[counter].description = Console.ReadLine();
                    if (data[counter].description.Length > 100)
                        data[counter].description = data[counter].description.Substring(0, 100);

                    Console.Write("Enter the version number: ");
                    data[counter].version.number = Console.ReadLine();
                    Console.Write("Enter the version month: ");
                    data[counter].version.month = Convert.ToByte(Console.ReadLine());
                    Console.Write("Enter the version year: ");
                    data[counter].version.year = Convert.ToUInt16(Console.ReadLine());


                case "2": //view
                    if (counter == 0)
                        Console.WriteLine("No data!");
                        for (int i = 0; i < counter; i++)
                            Console.WriteLine("{0}: {1}", i + 1, data[i].name);
                            if (i % 20 == 19)
                                Console.Write("Press Enter...");

                case "3": //search
                    Console.Write("Enter part of the name, description, etc... (case sensitive): ");
                    string search = Console.ReadLine();
                    bool found = false;
                    for (int i = 0; i < counter; i++)
                        if (data[i].name.Contains(search)
                                || data[i].description.Contains(search)
                                || data[i].category.Contains(search))
                            Console.WriteLine("{0}: {1}", i + 1, data[i].name);
                            found = true;
                    if (!found)
                        Console.WriteLine("Not found!");

                case "4":
                    Console.Write("Enter the program number: ");
                    int progNumber = Convert.ToInt32(Console.ReadLine()) - 1;

                    if ((progNumber > counter) || (progNumber < 0))
                        Console.Write("Out of range!");

                    Console.Write("Program name (was {0}; hit ENTER to leave as is): ",
                    string newText = Console.ReadLine();
                    if (newText != "")
                        data[progNumber].name = newText;

                    Console.Write("Program category (was {0}; hit ENTER to leave as is): ",
                    newText = Console.ReadLine();
                    if (newText != "")
                        data[progNumber].category = newText;

                    Console.Write("Program description (was {0}; hit ENTER to leave as is): ",
                    newText = Console.ReadLine();
                    if (newText != "")
                        data[progNumber].description = newText;

                    Console.Write("Program version (number) (was {0}; hit ENTER to leave as is): ",
                    newText = Console.ReadLine();
                    if (newText != "")
                        data[progNumber].version.number = newText;

                    Console.Write("Program version (month) (was {0}; hit ENTER to leave as is): ",
                    newText = Console.ReadLine();
                    if (newText != "")
                        data[progNumber].version.month = Convert.ToByte(newText);

                    Console.Write("Program version (year) (was {0}; hit ENTER to leave as is): ",
                    newText = Console.ReadLine();
                    if (newText != "")
                        data[progNumber].version.year = Convert.ToUInt16(newText);


                case "5": //delete
                    int position = 0;
                    Console.Write("Enter the position number to delete: ");
                    position = Convert.ToInt32(Console.ReadLine()) - 1;
                    if (position > counter)
                        Console.WriteLine("Error: out of range");
                        for (int i = position; i < counter; i++)
                            data[i] = data[i + 1];

                case "6": // Sort
                    program aux;
                    for (int i = 0; i < counter - 1; i++)
                        for (int j = i + 1; j < counter; j++)
                            if (data[i].name.CompareTo(data[j].name) > 0)
                                aux = data[i];
                                data[i] = data[j];
                                data[j] = aux;

                case "7": //replace "  " x " "
                    for (int i = 0; i < counter; i++)
                        while (data[i].name.Contains("  "))
                            data[i].name = data[i].name.Replace("  ", " ");

                case "0":
                    repeat = false;

                    Console.WriteLine("Wrong option!");
        while (repeat != false);

More C# Sharp Exercises of Arrays, Structures And Strings

Reverse Array
Create a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
Search In Array
Create a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he...
Array Of Even Numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
Array Of Positive And Negative Numbers
Create a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
Many Numbers And Sum
Create a program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must d...
Two Dimensional Array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
Statistics V2
reate a statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been entere...
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) ...
Array Of Struct
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...
Array Of Struct And Menu
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 al...
Books Database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
Centered Triangle
Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
Cities Database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
Create a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
Triangle Right Side
Create a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
Strings Manipulation
Create a C# program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Disp...
Nested Structs
Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and ...
Sort Data
Create a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
Two Dimensional Array As Buffer For Screen
Create a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays th...
Two Dimensional Array 2: Circunference On Screen
Create a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it ...
Exercise Tasks
Create a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month an...
Household Accounts
Create a program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), shou...