marți, 11 decembrie 2018

c++ programming part ten



929. Simple algorithms
930. Let a1, a2, a3 .... an array of n numbers and x a given real number. Check to
see if x is among the given numbers
931. #include  
932. void main ()
933. {
934. int, n, found = 0;
935. float a, x;
936. /read the n number of numbers
937. cout << "n =" cin >> n;
938. /read the searched number
939. cout << "x =" cin >> x;
940. /in the cycle for from 1 to n read the numbers
941. for (i = 1; i <= n; i ++)
942. {
943. cout << "a ="; cin >> a;
944. /if the entered number is equal to the number searched, the variable
945. found (which was initialized at its declaration with 0), becomes equal to 1

946. if (a == x) found = 1;
947. }
948. /if found = 1, it is displayed as being in the string, otherwise it is not in the string
949. if (found == 1) cout << "the number" << x << "is found in the string";
950. else cout << "the number" << x << "is not found in the string";
951. }


952. Calculation of n!
953. N! = 1 x 2 x ... n
954. that is, 3! = 1 x 2 x 3 = 6
955. #include  
956. void main ()
957. {
958. int n, p = 1, i;
959. cout << "n =" cin >> n;
960. for (i = 1; i <= n; i ++)
961. p = p * i;
962. cout << "n! =" << p;
963. }

964. We give a V vector consisting of integer type variables.
965. Is required:
966. Display the sum of the negative elements in the vector
967. Display the item's product in odd positions
968. #include  
969. void main ()
970. {
971. int, n, v [20], s = 0, p = 1;
972. /read the number of elements and the vector
973. cin >> n;
974. for (i = 1; i <= n; i ++) cin >> v [i];
975. /calculate the required amount
976.
977.
978. for (i = 1; i <= n; i ++)
979. if (v [i] <0 i="" nbsp="" p="" s="s" v="">980. /I show the calculated amount
981. cout << "s =" << endl << s;
982. /calculate the required product
983. for (i = 1; i <= n; i ++)
984. if (i% 2 == 1) p = p * v [i];
985. /Display the calculated product
986. cout << "p =" << endl << p;
987. }

988. Recursion
989. Recursiveness is the property of an object, a function of self-call.
990. We assume that we want to calculate the sum of the first integers, ie:
991. s (n) = 1 + 2 + .................... + n-1 + n
992. We make an abstraction that there is mathematical formula for
direct computation.
993. The sum of 1 to n-1 is denoted by s (n-1).
994. In this case, the first formula becomes:
995. s (n) = s (n-1) + n
996. If the number of elements is 0, obviously the sum is equal to 0.
997. In this case, we define:
998. 0, if n = 0
999. s (n) =s (n-1) + n, otherwise

1000. Statement s = 0 if n = 0 represents the exit condition of recursivity.
1001. Calculate on this principle the sum of the first 3 numbers:
1002. s (3) = s (2) +3
1003. s (2) = s (1) 2
1004. s (1) = s (0) 1
1005. The value s (0) is known, being equal to 0, comes out of recursivity.
1006. S (1) = 1
1007. s (2) = 1 + 2 = 3
1008. s (3) = 3 + 3 = 6

1009. The maximum in a vector
1010. #include  
1011. #include  
1012. /defines a new type of date, with vector name, whole type elements (int)
1013. typedef int vector [20];
1014. /n is the number of elements in the vector, v is the variable v of the type
1015. /above vector
1016. int n; vector v;
1017. /Defines the maximum function, which returns an int result, for example
1018. /the elements of the vector are of int type, and the maximum int type
Imagine that you are an alien on the moon, what do you do first?
1019. int maximum (int n)
1020. /-defines a max variance that will keep the maximum in the vector
1021. {int max;
1022. /if the number of elements in the vector is 1 (n = = 1), then the maximum
1023. i/s it even [1]
1024. if (n == 1) return in [1];
1025. /otherwise
1026. else
1027. {
1028. /the function recursively
1029. max = max (N-1);
1030. /if the maximum current max is less than the current position v [n] then
1031. /the new peak will be v [n]
1032. if (max 1033. else return max;
1034. }

1035. }


Niciun comentariu:

Trimiteți un comentariu

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