1. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
QUESTION 1
Write a C++ program to find the mean (average) of N numbers stored in
an array
#include <iostream>
using namespace std;
int main()
{
int n, i;
float number[100], sum=0, average;
cout << "Enter the total numbers of data: ";
cin >> n;
while (n >100 || n<=0)
{
cout << "Error! the number should in range of (1 to 100).n”;
cout << “Plase enter the number again: “;
cin >> n;
}
for(i=0; i<n; i++)
{
cout << “Enter a number: “;
cin >> number[i];
sum= sum + number[i];
}
average=sum / n;
2. cout << “nThe average is: “ << average << endl;
return 0;
}
//Output:
Enter the total numbers of data: 101
Error! the number should in range of (1 to 100).
Plase enter the number again: -4
Error! the number should in range of (1 to 100).
Plase enter the number again: 5
Enter a number: 4
Enter a number: 7
Enter a number: 2
Enter a number: 9
Enter a number: 15
The average is: 7.4
QUESTION 2
Write a C++ program to find the smallest number in one-dimensional
array of order n
#include<iostream>
using namespace std;
int main()
{
int n,i, farhan[20], min;
3. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
cout << "Enter the total number of elements for 1-D array : ";
cin >> n;
for(i=0;i<n;i++)
{
cout <<"Enter the element of farhan: ";
cin >> farhan[i];
}
min = farhan[0];
for(i=0;i<n;i++)
{
if(min > farhan[i])
min= farhan[i];
}
cout <<"nThe smallest element is: " << min << endl;
return 0;
}
//Output:
Enter the total number of elements for 1-D array : 7
Enter the element of farhan: 3
Enter the element of farhan: 6
Enter the element of farhan: 10
Enter the element of farhan: 13
4. Enter the element of farhan: 1
Enter the element of farhan: 53
Enter the element of farhan: 6
The smallest element is: 1
QUESTION 3
Write a C++ program to add the given two matrices.
#include <iostream>
using namespace std;
int main()
{
int m,n,c,d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix" << endl;
cin >> m>> n;
cout << "Enter the elements of first matrix n";
for( c=0; c<m ; c++)
for(d=0; d<n ; d++)
cin >> first [c][d];
cout << "Enter the elements of second matrix n";
for(c=0; c<m; c++)
for(d=0; d<n; d++)
cin >> second[c][d];
5. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
for (c=0;c<m;c++)
for(d=0;d<n;d++)
sum[c][d]= first[c][d] + second[c][d];
cout << "The summation of entered matrixes :- n";
for(c=0; c<m; c++)
{
for(d=0; d<n; d++)
cout << sum[c][d] << "t";
cout << endl;
}
return 0;
}
//Output :
Enter the number of rows and columns of matrix
4 4
Enter the elements of first matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Enter the elements of second matrix
1 2 3 4
6. 5 6 7 8
9 10 11 12
13 14 15 16
The summation of entered matrixes :-
2 4 6 8
10 12 14 16
18 20 22 24
26 28 30 32
QUESTION 4
Write a C++ program to find the transpose of a given matrix.
// Using for loop:
#include <iostream>
using namespace std;
int main()
{
int m,n,c,d, matrix[10][10];
do
{
cout << "Enter the no. of rows & columns of matrix(MAX 10x10):" << endl;
cin >> m >> n ;
}
while (m<1 || n<1 || m>10 || n>10);
cout << "Enter the elements of matrix n";
for( c=0; c<m ; c++)
7. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
for(d=0; d<n ; d++)
cin >> matrix [c][d];
cout << "The transpose of the entered matrixes :- n";
for(c=0; c<n ; c++)
{
for(d=0; d < m; d++)
cout << matrix[d][c] << "t";
cout << endl;
}
return 0;
}
// Output:
Output 1:
Enter the number of rows and columns of matrix
4 4
Enter the elements of matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The transpose of the entered matrixes :-
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
8. Output 2:
Enter the number of rows and columns of matrix
4 7
Enter the elements of matrix
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
The transpose of the entered matrixes :-
1 8 15 22
2 9 16 23
3 10 17 24
4 11 18 25
5 12 19 26
6 13 20 27
7 14 21 28
QUESTION 5
Write a C++ program for multiplication of given two matrices
2X2 DIMENSION ONLY
#include <iostream>
using namespace std;
int main()
{
int m,n,c,d, first[2][2], second[2][2], mult[2][2];
cout << "Enter the number of rows and columns of matrix" << endl;
9. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
cin >> m>> n;
cout << "Enter the elements of first matrix n";
for( c=0; c<m ; c++)
for(d=0; d<n ; d++)
cin >> first [c][d];
cout << "Enter the elements of second matrix n";
for(c=0; c<m; c++)
for(d=0; d<n; d++)
cin >> second[c][d];
for (c=0;c<m;c++)
for(d=0;d<n;d++)
mult[c][d]= first[c][0] * second[0][d] + first[c][1] * second[1][d];
cout << "Multiplication of entered matrixes :- n";
for(c=0; c<m; c++)
{
for(d=0; d<n; d++)
cout << mult[c][d] << "t";
cout << endl;
}
return 0;
}
10. //Output :
Enter the number of rows and columns of matrix
2 2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
1 2
3 4
Multiplication of entered matrixes :-
7 10
15 22
FOR OTHER DIMENSIONS UP TO 10X10 (FIRST ALTERNATIVE)
#include <iostream>
using namespace std;
int main()
{
int m,n,k,l,c,d, first[10][10], second[10][10], mult[10][10];
cout << "Enter the number of rows and columns of first matrix" << endl;
cin >> m>> n;
// m denotes number of rows in first matrix
// n denotes number of columns in first matrix
cout << "Enter the elements of first matrix n";
for( c=0; c<m ; c++)
11. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
for(d=0; d<n ; d++)
cin >> first [c][d];
cout << "Enter the number of rows and columns of second matrix" << endl;
cin >> k >> l;
// k denotes number of rows in second matrix
// l denotes number of columns in second matrix
cout << "Enter the elements of second matrix n";
for(c=0; c<k; c++)
for(d=0; d<l; d++)
cin >> second[c][d];
if(n==k)
{
for(c=0;c<m;c++)
{
for(d=0;d<l;d++)
{
mult[c][d]=0;
for(int x=0; x < k ; x++)
{
mult[c][d]= mult[c][d]+ first[c][x]* second[x][d];
}
}
}
12. cout<<"nThe multiplication of first matrix and second matrix is : " << endl;
for(c=0; c<m; c++)
{
for(d=0 ; d<l;d++)
{
cout<< mult[c][d] << "t";
}
cout<< endl;
}
}
else
{
cout<<"nnMultiplication is not possible" ;
}
return 0;
}
//Output :
Output 1:
Enter the number of rows and columns of first matrix
4 4
Enter the elements of first matrix
1 2 3 4
5 6 7 8
13. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
9 10 11 12
13 14 15 16
Enter the number of rows and columns of second matrix
4 4
Enter the elements of second matrix
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
The multiplication of first matrix and second matrix is :
80 70 60 50
240 214 188 162
400 358 316 274
560 502 444 386
Output 2:
Enter the number of rows and columns of first matrix
2 5
Enter the elements of first matrix
1 2 3 4 5
6 7 8 9 10
Enter the number of rows and columns of second matrix
5 3
Enter the elements of second matrix
1 2 3
14. 4 5 6
7 8 9
10 11 12
13 14 15
The multiplication of first matrix and second matrix is :
135 150 165
310 350 390
FOR OTHER DIMENSIONS UP TO 10X10 (SECOND ALTERNATIVE)
#include <iostream>
using namespace std;
int main ()
{
int m,n,a,b,i,j,k,sum = 0,A[10][10],B[10][10];
cout << "THE MULTIPLICATION OF TWO MATRICES WITH
DIFFERENT DIMENSIONS" << endl;
cout << "COLUMN OF 1ST = ROW OF 2ND" << endl;
do
{
cout << "nPlease enter the dimension of the first Matrix(MAX 10x10):" <<
endl;
cin >> m >> n ;
cout << "Please enter the dimension of the second Matrix(MAX 10x10):"
<< endl;
15. TUTORIAL 5 SJEM2231: STRUCTURED PROGRAMMING (C++)
cin >> a >> b ;
} while (m<1 || n<1 || m>10 || n>10 || a<1 || b<1 || a>10 || b>10 || n!=a);
cout << "Please enter the first matrix:" << endl;
for (i=0;i<m;i++)
for (j=0;j<n;j++)
cin >> A[i][j] ;
cout << "Please enter the second matrix:" << endl;
for (i=0;i<a;i++)
for (j=0; j<b;j++)
cin >> B[i][j] ;
cout << "nThe multiplication of two matrices is:" << endl;
for (i=0;i<m;i++)
{
for (j=0;j<b;j++)
{
for (k=0; k<n; k++)
sum = sum + A[i][k] * B[k][j];
cout << sum << "t" ;
}
cout << endl;
}
return 0;
}
16. //Output:
THE MULTIPLICATION OF TWO MATRICES WITH DIFFERENT DIMENSIONS
COLUMN OF 1ST = ROW OF 2ND
Please enter the dimension of the first Matrix(MAX 10x10):
2 9
Please enter the dimension of the second Matrix(MAX 10x10):
2 1
Please enter the dimension of the first Matrix(MAX 10x10):
3 2
Please enter the dimension of the second Matrix(MAX 10x10):
2 4
Please enter the first matrix:
1 2
3 4
5 6
Please enter the second matrix:
1 2 3 4
5 6 7 8
The multiplication of two matrices is:
11 25 42 62
85 115 152 196
231 277 334 402