• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Assignment problem 1

a guest Apr 1st, 2020 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <iomanip>
3. #include <algorithm>
4. #include <vector>
5.
6. using namespace std;
7.
8. bool cmp(pair <int, pair <int, int> > a, pair <int, pair <int, int> > b){
9.      return a.second.second < b.second.second;
10. }
11. int main(){
12.      int n, m;
13.
14.      cout << "Please enter the number of rows and columns of the matrix:\n";
15.      cin >> n >> m;
16.
17.      int A[n + 1][m + 1], T[m + 1][n + 1];
18.
19.      cout << "Please enter the matrix:\n";
20.      for(int i = 1;i <= n;i ++){
21.           for(int j = 1;j <= n;j ++){
22.                cin >> A[i][j];
23.                T[j][i] = A[i][j];
24.           }
25.      }
26.
27.      vector<pair <int, pair <int, int> > > ansA, ansT;
28.      for(int i = 1;i <= n;i ++){
29.           for(int j = 1;j <= m;j ++){
30.                if(A[i][j])
31.                  ansA.push_back (make_pair(i, make_pair(j, A[i][j])));
32.                if(T[i][j])
33.                  ansT.push_back (make_pair(i, make_pair(j, T[i][j])));
34.           }
35.      }
36.
37.      cout << "The triple list of matrix is:\n";
38.      for(auto x : ansA){
39.           cout << x.first << ' ' << x.second.first << ' ' << x.second.second << endl;
40.      }
41.      cout << "The triple list of transpose of matrix is:\n";
42.
43.      for(auto x : ansT){
44.           cout << x.first << ' ' << x.second.first << ' ' << x.second.second << endl;
45.      }
46.
47.      sort(ansA.begin(), ansA.end(), &cmp);
48.      cout << "The sorted triple list of matrix is:\n";
49.      for(auto x : ansA){
50.           cout << x.first << ' ' << x.second.first << ' ' << x.second.second << endl;
51.      }
52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top