Write C++ Program to interchange diagonals of a matrix


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. static int array[10][10];
  7. int i, j, m, n, a;
  8.  
  9. cout<<"Enter the order of the matix \n";
  10. cin>>m;
  11. cin>>n;
  12. if (m == n)
  13. {
  14. cout<<"Enter the co-efficients of the matrix\n";
  15. for (i = 0; i < m; ++i)
  16. {
  17. for (j = 0; j < n; ++j)
  18. {
  19. cin>>array[i][j];
  20. }
  21. }
  22. cout<<"The given matrix is \n";
  23. for (i = 0; i < m; ++i)
  24. {
  25. for (j = 0; j < n; ++j)
  26. {
  27. cout<<" "<<array[i][j];
  28. }
  29. cout<<"\n";
  30. }
  31. for (i = 0; i < m; ++i)
  32. {
  33. a = array[i][i];
  34. array[i][i] = array[i][m - i - 1];
  35. array[i][m - i - 1] = a;
  36. }
  37. cout<<"The matrix after changing the \n";
  38. cout<<"main diagonal & secondary diagonal\n";
  39. for (i = 0; i < m; ++i)
  40. {
  41. for (j = 0; j < n; ++j)
  42. {
  43. cout<<" "<<array[i][j];
  44. }
  45. cout<<"\n";
  46. }
  47. }
  48. else
  49. cout<<"The given order is not square matrix\n";
  50. }
  51.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts