fork download
  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4.  
  5.  
  6. int m,n;
  7. cin>>m;
  8.  
  9. cin>>n;
  10. int a[10][10];
  11. for(int i=1;i<=m;i++){
  12. for(int j=1;j<=n;j++){
  13. cin>>a[i][j];
  14. }
  15. }
  16.  
  17. int startRow=1,endRow = m ,startCol=1,endCol = n;
  18. while(startRow<=endRow && startCol<=endCol){
  19.  
  20.  
  21. ///Print the start row
  22. for(int i=startCol;i<=endCol;i++){
  23. cout<<a[startRow][i]<<","<<" ";
  24. }
  25. startRow++;
  26.  
  27. ///Print the end col from sr to er
  28. for(int i=startRow;i<=endRow;i++){
  29. cout<<a[i][endCol]<<","<<" ";
  30. }
  31.  
  32. endCol--;
  33.  
  34. ///Print the BottomRow
  35. if(endRow>=startRow){
  36. for(int i=endCol;i>=startCol;i--){
  37. cout<<a[endRow][i]<<","<<" ";
  38. }
  39. endRow--;
  40. }
  41.  
  42. ///Print the startCol
  43. if(startCol<=endCol){
  44. for(int i=endRow;i>=startRow;i--){
  45. cout<<a[i][startCol]<<","<<" ";
  46. }
  47. startCol++;
  48. }
  49.  
  50. }
  51. cout<<"END";
  52.  
  53.  
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
END