It is a very common program in schools and colleges. Most of the students tend to mug up the coding and reproduce it at the time of the examination. Of course most of them succeed in doing so (the brain is the best memory device in the world!) but some tend to forget one line of coding and end up with weird outputs. Never mug up programs. Understand the logic of the program and then try it yourself. After all, if your logic is correct then there is no chance that the computer will produce wrong results (it will be as good as you program it to be). Let’s write a program to add two matrices (matrices are two dimensional arrays used in mathematics). The various steps involved are:
Ask the user for the order of the matrix (i.e. the number of rows and columns) To add 2 matrices, they should be of the same size. Get the input for each element of the matrix. First get elements of first matrix and then the second. Add the two matrices (i.e. add corresponding elements) and store the values in a third matrix. Display all 3 matrices. //To obtain two matrices and two find their sum
#include<iostream.h>
#include<iomanip.h> //needed for using the manipulator setw ( )
int main( ) {
int i , j , r1 , r2 , c1 , c2 , a[20][20] , b[20][20] , c[20][20]; cout<<"Enter the number of rows and columns of first matrix :"; cin>>r1>>c1;
cout<<"Enter the number of rows and columns of second matrix :"; cin>>r2>>c2;
// If the matrix orders are not equal then addition is not possible
if ( (r1! = r2) || (c1!=c2) ) {
cout<<endl<<"Matrix addition is not possible"; return 0;
}
// If orders are equal then get input for two matrices ‘a’ and ‘b’
for (i =0; i< r1; i ++) {
for (j = 0; j<c1; j ++) {
cout<<"Enter the "<< i <<" x "<< j <<" element of first matrix: "; cin>>a[ i ][ j ];
} } for (i = 0; i<r2 ; i ++) { for (j =0; j<c2; j ++) {
cout<<"Enter the "<<i<<"x"<<j<<" element of second matrix: "; cin>>b[ i ] [ j ];
} }
cout<<endl;
// Displaying matrix ‘a’
for (i =0; i<r1; i++) { cout<<endl; for (j =0; j<c1; j++) { cout<<setw(9)<<a[ i ][ j ]; } } cout<<"\t"<<"+"; cout<<endl; // Displaying matrix ‘b’ for (i = 0; i<r1; i ++) { cout<<endl; for (j =0; j<c1; j++) { cout<<setw(9)<<b[ i ] [ j ]; } } cout<<endl; cout<<"\t \t \t = ";
cout<<endl; // Calculating and displaying the result which is stored in matrix ‘c’
for (i =0; i<r1; i++) { cout<<endl; for (j =0; j<c1; j++) { c[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ]; cout<<setw(9)<<c[ i ][ j ]; } } return 0; }
setw ( ) will be explained later on. It is a manipulator that is used to format the way the output is displayed.
Structures
The following topics are covered in this section: • Structures
• Nesting of Structures
Structures are user defined data types. Structures were used in C and they consist of different data types that are related logically in some way. In other words, a structure groups together different data types under one name. The data items that come under a structure are called structure elements or members (or fields). A structure basically puts different data types into one package.
Even for structures you have a declaration and a definition. Declaration means telling the compiler what type a particular variable is. Definition means that the compiler allots memory space to the variable that was declared.
In the case of structures, a structure declaration forms a template. When a structure variable is defined the compiler automatically allocates sufficient memory to accommodate all of its elements. You can define as many structure variables as you want.
The structure syntax is:
struct tag/structure-name { data-type variable-name; data-type variable-name; //more declarations }
structure-variables; // Structure definition
‘struct’ is a keyword and it has to be used to declare a structure. Following the keyword, we type the name of the structure (this is also known as the tag-name). Then we have an open brace, followed by the different data types that you want to put in the structure. Finally we add a closing brace and can give the names of the structure variables. This is known as the structure definition.
First of all you should know the use of structures before proceeding further. Suppose you own a small shop. For every set of purchases by a customer, you give the customer a bill. This bill has the bill number and the total amount of purchase mentioned in it. The point to be noted is that, every bill will have a bill number and a particular amount mentioned in it.
Now suppose you want to keep a record of all the bills you have issued through your computer, you will have to store the following in your computer: the bill number and the amount for each bill issued. Bill numbers will be integers whereas the amount to be paid will be a float quantity. Hence, you have two different data types but they are logically related (since every bill will consist of the two data types). It is here that the concept of structures will be found useful. struct bill { int billnumber; float amount; } one,two;
In the above example, we declare a structure by the name ‘bill’. This contains data items ‘billnumber’ (which is an integer) and ‘amount’ (which is a float quantity). The braces are closed and then we define the variables of the structure, i.e. ‘one’ and ‘two’. This means that ‘one’ and ‘two’ belong to the structure ‘bill’. Both ‘one’ and ‘two’ have their own individual ‘billnumber’ and ‘amount’. Therefore, you can give the bill number and amount for bill ‘one’. Similarly, you can also give the details for ‘two’.
Remember that at the end of the structure declaration and definition, we have to terminate it
using the statement terminator.
‘one’ and ‘two’ are known as structure variables.
‘billnumber’ and ‘amount’ are known as the structure elements. For normal built in data types we would declare a variable as:
int x;
Where x is the variable and int specifies the type of a variable. Similarly in structures, the variable is declared as belonging to the data type that you have created (which in turn is a combination of different built-in data types).
struct bill { int billnumber; float amount; } one,two;
Instead of this we can write it as follows:
struct bill {
int billnumber; float amount; };
// End of struct declaration–memory not allocated since struct variable not defined
int main ( ) {
bill one, two; //Structure variable definition return 0;
}
Both the methods are equivalent and are acceptable.
Accessing and Initializing Structure Elements:
Every structure variable that you define will have its own individual elements. So how do we access these individual elements of a structure? To access a particular structure element we make use of the dot operator.
The syntax is:
structure-variable . structure-element For example:
one.billnumber
is the syntax for accessing the bill number of structure variable one.
Now that you know how to access a structure element you can initialize it as well. Initializing means giving a value to the element.
Example:
one.billnumber = 214; one.amount = 1032.25;
Bill ‘one’ has a bill number 214 and the ‘amount’ of the bill is 1032.25
One structure variable can be assigned to another only when they are of the same structure type.
two = one;
is a valid statement because both ‘one’ and ‘two’ belong to the same structure ‘bill’. This statement assigns the values of one.billnumber and one.amount to two.billnumber and two.amount respectively.
An example using Structures concept:
//Declaring the structure phonebook
struct phonebook { char name[40]; char city[40]; int tel; }; int main( ) { phonebook a1;
cout<<"Enter the name : "; cin>>a1.name;
cout<<"Enter the city : "; cin>>a1.city;
cout<<"Enter the telephone number : "; cin>>a1.tel;
cout<<"\nThe size of the structure variable is : "<<sizeof(a1); cout<<"\nThe entry you made is (a1): ";
cout<<a1.name<<"-"<<a1.city<<"-"<<a1.tel; return 0; }
The output would be: Enter the name : Ajay Enter the city : Chennai
Enter the telephone number : 21345 The size of the structure variable is : 84
The entry you made is (a1): Ajay-Chennai-21345
Remember: Arithmetic operator works only with built-in data types. Structures are user
defined data types.
Hence it is not possible to say: a3 = a1 + a2;
when a1 and a2 are structure variables even if they belong to the same structure type.