C++ Programming Code Examples
C++ > Data Structures Code Examples
Matrics Addition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Matrics Addition */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct matrics
{
int a[4][4], m, n;
};
matrics add_mat(matrics m1, matrics m2); //prototype declared
int main()
{
matrics x1, x2, x3;
int i, j;
cout<<"
Enter the size of matrics
";
cout<<"
Enter rows
";
cin>>x1.m;
cout<<"
Enter columns
";
cin>>x1.n;
cout<<"
Enter the elements (Row-wise & Column-wise)
for(i=0; i<x1.m; i++)
{
for(j=0; j<x1.n; j++)
cin>>x1.a[i][j];
}
cout<<"
Enter the size of 2nd matrics
";
cout<<"
Enter rows
";
cin>>x2.m;
cout<<"
Enter columns
";
cin>>x2.n;
cout<<"
Enter the elements (Row-wise & column-wise)
for(i=0; i<x2.m; i++)
{
for(j=0; j<x2.n; j++)
cin>>x2.a[i][j];
}
x3=add_mat(x1,x2);
cout<<"
The added matrics is
";
for(i=0; i<x3.m; i++)
{
for(j=0; j<x3.n; j++)
cout<<x3.a[i][j]<<" ";
}
matrics add_mat(matrics m1, matrics m2)
{
matrics m3; //object of matrics type
int k, l;
if((m1.m==m2.m) && (m1.n==m2.n))
{ m3.m=m1.m+m2.m;
m3.n=m1.n+m2.n;
}
for(k=0; k<m1.m; k++)
{
for(l=0; l<m1.n; l++)
m3.a[k][l]=m1.a[k][l]+m2.a[k][l];
return (m3);
}
}// end of If loop
else
cout<<"
Addition is not possible
";
}
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.
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.
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 computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
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).
In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.
#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.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
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.
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.
A C++ program starts its execution from this method "main". Wherever this main method is, the main method will be executed first. '()' is used at the end as main is a method. Main
A "stack" is a basic data structure and can be defined in an abstract, implementation-free manner, or it can be generally "defined" as a linear list of items in which all additions and
To convert decimal number to octal number in C++ programming, you have to ask to the user to enter the decimal number to convert it into octal number to display the equivalent