# C++ Programming Code Examples

## C++ > Mathematics Code Examples

### C++ Program to Use rand and srand Functions

/* C++ Program to Use rand and srand Functions This is a C++ Program to make use of Rand and Srand functions to generate unique random numbers. */ #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main(int argc, char **argv) { cout << "First number: " << rand() % 100; srand(time(NULL)); cout << "\nRandom number: " << rand() % 100; srand(1); cout << "\nAgain the first number: " << rand() % 100; return 0; }

Initialize random number generator. The pseudo-random number generator is initialized using the argument passed as seed. The C++ <cstdlib> srand() function seeds the pseudo-random number generator used by rand() function. If rand() is used before any calls to srand(), rand() behaves as if it was seeded with srand(1). For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand. Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.

Get current time. Get the current calendar time as a value of type time_t. The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer. The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime, gmtime or difftime).

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

Generate random number. Returns a pseudo-random integral number in the range between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand. RAND_MAX is a constant defined in <cstdlib>. The rand() function in C++ is used to generate random numbers; it will generate the same number every time we run the program. In order to seed the rand() function, srand(unsigned int seed) is used. The srand() function sets the initial point for generating the pseudo-random numbers. The rand() function generates numbers randomly.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

This represents the queue dequeue function. Every time another object or customer enters the line to wait, they join the "end of the line" and represent the "enqueue function". Queue

Frequency of character in a string means how many times a "particular character" is present in a string. In sample; suppose we have string hitesh in this word h repeated 2 times, it's the

Program stores the information (name, roll & marks) of 10 students using structures. In this program, a structure, student is created. This structure has three members: name ('string'),

Program to implement hill cipher. In classical cryptography, the Hill cipher is a polygraphic substitution cipher based on 'Linear Algebra'. Following discussion assumes an elementary