Codification of the flow chart in C#

By kedinn 2013/01/15

Now we must codify the flow chart using the instructions of the C# language. Since we have seen the surroundings of programming of the C# Line of vision created a basic skeleton to us on which we will continue the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculoSuedo \ {
    class Program
    \ {
        static void Main (string [] args)
        \ {
        }
    }}

As we advance in the course we will see that it means a class and namespace, what is the objective of using etc. at the moment we will concentrate where we will codify our flow charts.
The codification of the flow chart we will do it within the Main function (the Main function is first that is executed at the beginning a program)

The complete program for the calculation of the pay of a worker knowing the amount hour workeds and the cost per hour is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculoSuedo \ {
    class Program
    \ {
        static void Main (string [] args)
        \ {
            int horasTrabajadas;
            float costoHora;
            float pay;
            string line;
            Console.Write (“Enters Hour workeds by the worker: ?);
            line = Console.ReadLine ();
            horasTrabajadas = int. Parse (line);
            Console.Write (“Enters the payment per hour: ?);
            line = Console.ReadLine ();
            costoHora = float. Parse (line);
            pay = horasTrabajadas * costoHora;
            Console.Write (“the total pay of the worker is: ?);
            Console.Write (pay);
            Console.ReadKey ();                
        }
    }}

In order to prove the operation of the program we must press the icon with a green triangle (or the special key F5 or from the menu to choose the option “To purify? - > “To initiate purification?):

line of vision c# 2010 express ones

The execution of the program allows to enter the amount of hour workeds by a worker and its payment per hour, showing next the pay that is due to pay, an example of the execution of this program is:

line of vision c# 2010 express ones

Concepts that must be clear:

  1. At the moment we will make all the algorithm within the Main function. That is to say, the rest always creates it the surroundings of the C# Line of vision.
  2. If we observed the flow chart we see that we must define three variables: (horasTrabajadas, costoHora, pay), it is here where we must define that data types will store themselves in the same. The amount of zone times will be a whole value (ex. 100 - 150 - 230 etc.), but the cost of the hour are very common that it is a real value (ex. 5.35 - 7,50 etc.) and as the pay turns out to multiply the hour workeds by the cost per hour the same will have to be real. The definition of the variables we do it in the Main:
                int horasTrabajadas; float costoHora; float pay;

    We use the keyword int to define whole variables (in C# the keywords must go obligatorily in small letters, but a syntactic error takes place) After the keyword we must indicate the name of the variable, for example: horasTrabajadas (one sets out that the name of the variable begins with small letter and in case of being constituted by two words or more words they must go in capital letters the first character (a varname cannot have spaces in target, to begin with a number, nor to use special characters either)
    We must always look for varnames that they indicate to us that they store (is not advisable to call to varnames with individual letters)

  3. In order to show messages in the screen we used the object “Console?:
                Console.Write (“Enters Hour workeds by the worker: ?);

    With this syntax everything what is in inverted commas content will appear exactly in the window of the “Console?.
    If we prepare a variable:

                Console.Write (pay);

    It will appear the content of the variable. That is to say, value the stored in the variable pay and not message “pay?.

  4. In order to make the entrance of data by keyboard in C# it is complicated. We must define a variable of type string who we will call it line:
                string line;

    Soon whenever we need to enter by keyboard a set of characters we use the ReadLine function of the Console object with the following syntax:

                line = Console.ReadLine ();

    A second step is to copy the content of the variable line in a variable of type int:

                horasTrabajadas = int. Parse (line);

    Or a variable of type float:

                costoHora = float. Parse (line);

    The variable line temporarily stores the data that the operator of the program enters, soon to copy itself to the respective variable (as we see if we want to turn string to integer we used the Parse function of the object int (int. Parse))

    The operations that we indicated in the flow chart by means of the figure rectangle we codified it so what:

                pay = horasTrabajadas * costoHora;

     

We can see a relation between the instructions that we must use for each symbol of the flow chart:

line of vision c# 2010 express ones

In the flow chart we did not indicate the definition of variables:

            int horasTrabajadas; float costoHora; float pay; string line;

 

 

We did not represent with symbols the messages to show previous the load of data by keyboard:

            Console.Write (“Enters Hour workeds by the worker: ?);

 

Since we have seen until now are many parts of our code that we do not understand but they are indispensable for the implementation of our programs, as we advance with the course many of these concepts they will be clarified.