 # C++ Programming Code Examples

## C++ > Arrays and Matrices Code Examples

### program to add two matrices

``` program to add two matrices Write a c++ program to add two matrix using 2-D arrays use any c++ compiler, turbo c++, visual studio or codeblocks. 2D arrays are just like tables which consist of rows and colums and each cell contains a value. Matrix addition as 1 2 1 2 1+1 2+2 2 4 + = > Result= 3 4 3 6 3+3 4+6 6 10 Program explanation: For 2D array size there must be constant value in square brackets like array[constant value][constant value] Two const variables row and col are used to define size if we do not make both const then error found because without const reserve word they are behaving as variable. Before placing both variable in square brackets they must initialized else error will be found three nested for loops are used two for taking input in matrix 2D arrays and one for resultant matrix #include<iostream> using namespace std; int main(){ //Using const int for array size const int row=2,col=2; // if not use const error found cout<<"Size of Matrices : "<<row<<" X "<<col<<endl; cout<<"Enter Value For First Matrix:"<<endl; int first[row][col], second[row][col]; int x,j; for( x=0;x<row;x++){ cout<<"Enter value for row number: "<<x+1<<endl; for( j=0;j<col;j++){ cin>>first[x][j]; } } cout<<"\n\n\nEnter Value For Second Matrix:"<<endl; for( x=0;x<row;x++){ cout<<"Enter value for row number: "<<x+1<<endl; for( j=0;j<col;j++){ cin>>second[x][j]; } } // Resultant Matrix for( x=0;x<row;x++){ for( j=0;j<col;j++){ first[x][j]=first[x][j]+second[x][j]; } } cout<<"\n\n\t\tResultant Matrix:"<<endl; for( x=0;x<row;x++){ cout<< endl; for( j=0;j<col;j++){ cout<<"\t\t"<<first[x][j]<<" "; } } return 0; } ```

C Programming Codes Displaying a Table - C Language code how to display a table in C programming language is more or less similar to that of counting. We use only one iteration and increment it with value of which table is

If Any Graph is Possible to be Constructed - This algorithm takes the input of the number of 'Vertexes' and their corresponding degree. Checks various 'constraints', tries to build the graph. If it fails, no valid graph can be created

C Codes Calculates Prints the Value of nPr - In the following c program we are calculating the value of nPr based on the given values of n and r. nPr can also be represented as P(n,r). The formula of P(n, r) is: n! / (n - r)!. P(5, 2)=5!

C Program to displaying Day of the Month - C programming code to implement calender. Program example to display day of month. Program will accept year, month and date from the user and will display the day of the

C++ Pointers to Pointer or Double Pointer - Dereference operator or indirection operator, noted by asterisk, is also a "unary operator" in C++ languages that uses for pointer variables. 'Double Pointer' or Pointer to Pointer need to

C Code Remove Spaces Blanks from String - C program remove extra spaces, blanks from the string. Removes 'extra blank spaces' from the given string and returns a new string with single "blank spaces" and skip all consecutive

Encoding a Message Matrix Multiplication - This C++ Program perform 'encoding matrix' using a hidden-key. "Encoding" is performed using "matrix multiplication" between given matrix and 'key matrix'. Enter the number of

Convert Decimal to Octal Number System - C Program Code input decimal number and convert to octal number system. Decimal number system is a base 10 number system. Decimal number system uses 10 symbols to