marți, 11 decembrie 2018

c++ programming part nine



800. Example:
801. #include  
802. int main ()
803. {
804. int n;
805. do {
806. Enter a number
807. (0 for the end):
808. 123

809. You have entered: 123
810. Enter a number
811. (0 for the end):
812. cout << "Give one
813. number (0 for
814. the end): ";
815. cin >> n;
816. cout << "you introduce "<817. } while (n! = 0);
818. return 0;
819. }
820. 88
821. You have entered: 88
822. Enter a number
823. (0 for the end):
824. 0
825. You have entered: 0
listen to good music
826. Switch operator
827. This operator performs several more statements.
828. Evaluate the expression and choose the appropriate label
829. switch (expression)
830. {
831. case label1: statement1;
832. case label2: statement2;
833. ....
834. default labeln: statementn;
835. }

836. One of the tags can be default. In this case, if the expression in the switch
statement has none of the values in the labels, it's go to the execution statement
with the default label.
837. It may also appear and the break statement is used for exit the switch statement.
838. Example. We will read two integers and one operator +, - and we will
calculate the result of the corresponding operation.
839. #include  
840. int main ()
841. {
842. int a, b;
843. char operator;
844. cout << "give two integer values:";
845. cin >> a >> b;
846. cout << "give an operator:";
847. cin >> operator;
848. switch (INT)
849. {
850. case '+':
851. cout << a + b;
852. break;
853. case '-':
854. cout << a - b;
855. break;
856. default:
857. cout << "error";
858. break;
859. }
860. return 0;
861. }
chew slowly, 9 times before you swallow
862. The message "give two full values" is displayed.
863. We enter two full values ??from the keyboard, which are taken in variables a and
b.
864. The message "give an operator" is displayed. We introduce from + keyboard,
or something else.

865. It goes into "switch". If the + operator was inserted, it is gather a and b and
exit the switch through the break statement.
866. If the operator has been entered - it is subtracted a from the value of b and exits
the switch through the break statement.
867. If none of the operators were entered above, the error message is displayed.
868. Functions
869. Functions are used in a program when we went to group many statements.
870. The statement are used several times in the program.
871. How is a function defined?
872. type function name (parameter list)
873. {
874. statement;
875. .............
876. statement final;
877. }
in the morning eat well, at noon a soup, and nothing in the evening
878. The statement is usually made up of several statements between braces. The
parameter list has type 1 param-1, type 2 param-2, ..., tipn param-n where type1,
type2, ..., typn represent the types of parameters param-1, param-2, ..., param-n. The
parameters in the definition function are called formal parameters. Some of the
parameters are variables that will be processed by the function (these parameters
879. are called input parameters). If a function does not have one value associated
with the name, its type is void.

880. Associating a values ??calculated for the function name are done with
the statement
881. return. Or, if the type is "void":
882. return are missing;
883. Example of definition
884. We want to define a function that calculates the second sum of the numbers
a and b, and returns the calculated value, ie a whole number.
885. int sum (int a, int b)
886. {
887. int c;
888. c = a + b;
889. return c;
890. }

891. If we want the function type to be "void"
892. void sum (int a, int b)
893. {
894. int c;
895. c = a + b;
896. return;
897. }
898. How do I call a function?
899. Calling a function can be done in an expression.
900. Example. We'll write a function to calculate the amount components of a
vector with integer numbers.
901. The parameters of the function will be the vector and its size, that is
902. number of items. The result is an entire value.
903. #include  
904. / *
905. Input parameters
906. v - vector
907. no - vector size v
908. * /
909. int sum (int v [], int no)
910. {
911. int i;
912. int s = 0;
913. // Calculates the sum of the components
cares for a flower
914. for (i = 0; i 915. s = s + v [i];
916. return s;
917. }
918. int main ()
919. {
920. int v [4] = {1, -2, -2, 15};
921. int z;
922. // calculate the sum of vector elements
923. z = sum (v, 4);
924. // write the result
925. cout << "is" << z << endl;
926. }
927. Observation:
928. In a program we can have more functions with the same names but with
different parameters. This is called overloading functions.

Niciun comentariu:

Trimiteți un comentariu

Rețineți: Numai membrii acestui blog pot posta comentarii.