-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.cpp
More file actions
63 lines (52 loc) · 1.17 KB
/
struct.cpp
File metadata and controls
63 lines (52 loc) · 1.17 KB
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
#include<iostream>
using namespace std;
int ** readmatrix( int m, int n);
void displaymatrix(int **k, int m, int n);
int main()
{
int **matrix;
int M,N;
cout<<"Enter the number of rows m = ";
cin>>M;
cout<<endl<<"Enter the number of columns n = ";
cin>>N;
matrix = readmatrix(M,N);
for(int i =0 ; i < M ; i++){
for(int j = 0 ; j < N ;j++){
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
displaymatrix(matrix,M,N);
for(int i =0 ; i < M; i++){
delete matrix[i];
}
delete matrix;
return 0;}
int ** readmatrix(int m, int n)
{
int **memalloc;
memalloc = new int * [m];
for(int i = 0; i < m; i++){
memalloc[i] = new int[n];
}
if (memalloc != NULL)
{
for ( int i=0; i<m;i++){
for (int j=0;j<n;j++)
cin>>memalloc[i][j];
}
return (memalloc);
}
cout<<"Couldn't allocate memory"<<endl;
return NULL;
}
void displaymatrix(int **k, int m, int n)
{
cout<<m<<" "<<n<<endl;
for(int i=0; i<m; i++)
{ cout<<endl;
for(int j=0;j<n;j++)
cout<<k[i][j]<<" ";
} return;
}