 # C++ Programming Code Examples

## C++ > Algorithms Code Examples

### A C++ Program to Multiply two Matrices. Divide And Conquer Algorithm

/* A C++ Program to Multiply two Matrices. Divide And Conquer Algorithm */ # include <iostream.h> # include <stdlib.h> # include <conio.h> class Matrix { private: float matrix_a; float matrix_b; float matrix_c; public: Matrix( ); void get_matrix_a( ); void get_matrix_b( ); void multiply_matrices( ); void show_result_Matrix( ); }; Matrix::Matrix( ) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { matrix_a[i][j]=0; matrix_b[i][j]=0; matrix_c[i][j]=0; } } gotoxy(1,1); cout<<"*********************************************"<<endl; cout<<" * * * * * * * * * * * * * * Matrix Multiplication * * * * ** * * * * * * *"<<endl; cout<<"********************************************"<<endl; gotoxy(1,25); cout<<"*****************************************************"; } void Matrix::get_matrix_a( ) { gotoxy(1,6); cout<<" Enter the values of the Matrix A row by row : "<<endl; cout<<" Ú ¿"<<endl; cout<<" ³ ³"<<endl; cout<<" ³ ³"<<endl; cout<<" ³ ³"<<endl; cout<<" À Ù"<<endl; gotoxy(18,10); cout<<" A = "<<endl; int x=28; int y=9; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { gotoxy(x,y); cin>>matrix_a[i][j]; x+=5; } x=28; y++; } } void Matrix::get_matrix_b( ) { gotoxy(1,15); cout<<" Enter the values of the Matrix B row by row : "<<endl; cout<<" Ú ¿"<<endl; cout<<" ³ ³"<<endl; cout<<" ³ ³"<<endl; cout<<" ³ ³"<<endl; cout<<" À Ù"<<endl; gotoxy(18,19); cout<<" B = "<<endl; int x=28; int y=18; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { gotoxy(x,y); cin>>matrix_b[i][j]; x+=5; } x=28; y++; } } void Matrix::multiply_matrices( ) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { float value=0; float sum=0; for(int k=0;k<3;k++) { value=matrix_a[j][k]*matrix_b[k][j]; sum+=value; } matrix_c[i][j]=sum; } } } void Matrix::show_result_Matrix( ) { clrscr( ); gotoxy(1,1); cout<<"*********************************************************"<<endl; cout<<" * * * * * * * * * * * * * * Matrix Multiplication * * * * ** * * * * * * *"<<endl; cout<<"*************************************************************"<<endl; gotoxy(1,6); cout<<" The values of Matrix A and B are :"<<endl; cout<<" Ú ¿ Ú ¿"<<endl; cout<<" ³ ³ ³ ³"<<endl; cout<<" ³ ³ ³ ³"<<endl; cout<<" ³ ³ ³ ³"<<endl; cout<<" À Ù À Ù"<<endl; gotoxy(45,9); cout<<" B = "<<endl; gotoxy(10,9); cout<<" A = "<<endl; gotoxy(1,15); cout<<" The Product of Matrix A and B is : "<<endl; cout<<" Ú ¿"<<endl; cout<<" ³ ³"<<endl; cout<<" ³ ³"<<endl; cout<<" ³ ³"<<endl; cout<<" À Ù"<<endl; gotoxy(13,19); cout<<" A * B = "<<endl; int x_1=20; int y_1=8; int x_2=55; int y_2=8; int x_3=28; int y_3=18; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { gotoxy(x_1,y_1); cout<<matrix_a[i][j]; gotoxy(x_2,y_2); cout<<matrix_b[i][j]; gotoxy(x_3,y_3); cout<<matrix_c[i][j]; x_1+=5; x_2+=5; x_3+=5; } x_1=20; y_1++; x_2=55; y_2++; x_3=28; y_3++; } gotoxy(1,25); cout<<"*********************************************"; } int main( ) { textmode(BW80); clrscr( ); Matrix Obj; Obj.get_matrix_a( ); Obj.get_matrix_b( ); Obj.multiply_matrices( ); Obj.show_result_Matrix( ); getch( ); return 0; }

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C++ language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

#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.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

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.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

Positions cursor in text window. The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. If the coordinates are in any way invalid the call to gotoxy is ignored. Neither argument to gotoxy can be zero.

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.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

"Heapsort" can be thought of as an improved "selection sort": like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region

Implement the binary search to find a peak in the array. If the 'middle element' is more than its both Neighbors, 'it is the Peak'. Otherwise, split the array and check the same. A function

C++ program encodes any message using the technique of 'One time' pad cipher technique. Input is not Case Sensitive and works only for all characters. 'White Spaces' are not ignored