Write a program to store elements in m x n 2D array (Matrix)

m(row) and n(coloumns) are taken from user.. Elements are taken from user.. matrix is displayed in tabular form..
This should be your first program for Matrix.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a[10][10],m,n,i,j;
  7. cout<<"Enter number of rows: ";
  8. cin>>m;
  9. cout<<"Enter number of coloumns: ";
  10. cin>>n;
  11. cout<<endl<<"Enter elements of matrix: "<<endl;
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. for(i=0;i<m;i++)
  15. {
  16. for(j=0;j<n;j++)
  17. {
  18. cout<<"Enter element a"<<i+1<<j+1<<": ";
  19. cin>>a[i][j];
  20. }
  21. }
  22. cout<<endl<<"Displaying Matrix: "<<endl<<endl;
  23. for(i=0;i<m;i++)
  24. {
  25. for(j=0;j<n;j++)
  26. {
  27. cout<<a[i][j]<<" ";
  28. }
  29. cout<<endl<<endl;
  30. }
  31. getch();
  32. }

Check out this stream