Write C++ Program to Find sum of each row and columns 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. #define size 3 // Defining Matrix size
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. int Arr[size][size];
  9. int row, col, sum = 0;
  10.  
  11. //Inputing elements in matrix from user
  12. cout<<"Enter elements in matrix of size "<<size<<" x "<<size<<"\n";
  13. for(row=0; row<size; row++)
  14. {
  15. for(col=0; col<size; col++)
  16. {
  17. cin>>Arr[row][col];
  18. }
  19. }
  20.  
  21. // Calculating sum of elements of each row of matrix
  22. for(row=0; row<size; row++)
  23. {
  24. sum = 0;
  25. for(col=0; col<size; col++)
  26. {
  27. sum += Arr[row][col];
  28. }
  29. //Printing Sum of row elements
  30. cout<<"Sum of elements of row "<<row+1<<" = "<<sum<<"\n";
  31. }
  32.  
  33. // Finding sum of elements of each columns of matrix
  34. for(row=0; row<size; row++)
  35. {
  36. sum = 0;
  37. for(col=0; col<size; col++)
  38. {
  39. sum += Arr[col][row];
  40. }
  41. //Printing Sum of columns elements
  42. cout<<"Sum of elements of column "<<row+1<<" = "<<sum<<"\n";
  43. }
  44.  
  45. return 0;
  46. }
  47.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts