-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixSum(double).cpp
71 lines (68 loc) · 1.6 KB
/
matrixSum(double).cpp
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
#include <iostream>
using namespace std;
void getMatrix(double**matrix,int rows,int coloums)
{
for(int i=0;i<rows;i++)
{
matrix[i]=new double[coloums];
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<coloums;j++)
{
double element;
cout<<"Enter rows["<<i<<"] /coloums["<<j<<"]";
cin>>element;
matrix[i][j]=element;
}
}
}
void printMatrix(double**matrix,int rows,int coloums)
{
cout<<"[";
for(int i=0;i<rows;i++)
{
cout<<"[";
for(int j=0;j<coloums;j++)
{
if(j==coloums-1)
cout<<matrix[i][j];
else
cout<<matrix[i][j]<<", ";
}
if(i==rows-1)
cout<<"]";
else
cout<<"],";
}
cout<<"]"<<endl;
}
double sumMatrix(double**matrix,int rows,int coloums)
{
double total=0;
for(int i=0;i<rows;i++)
for(int j=0;j<coloums;j++)
total+=matrix[i][j];
return total;
}
void deleteMatrix(double** matrix,int rows)
{
for(int i=0;i<rows;i++)
delete[] matrix[i];
delete[] matrix;
}
int main()
{
unsigned short rows,coloums;
cout<<"Rows: ";
cin>>rows;
cout<<"Coloums:";
cin>>coloums;
double **matrix=new double*[rows];
getMatrix(matrix,rows,coloums);
printMatrix(matrix,rows,coloums);
double matrixSum=sumMatrix(matrix,rows,coloums);
cout<<"Sum of all the elements of the matrix is: "<<matrixSum;
deleteMatrix(matrix,rows);
return 0;
}