How to reverse diagonals in a 2D array in c++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Misstrius
    New Member
    • Jan 2013
    • 2

    How to reverse diagonals in a 2D array in c++?

    I have been trying this for so long.
    I need to make a separate function named reverseDiagonal where I have to reverse the diagonals in a 2D array.. I have tried swapping it but i don't know where to place the "cout" and print the diagonal.
    Please help?
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    I don't see any code to comment about. However, it's a good practice to separate the data input from processing from data output. This will make your code more readable, easily understood and maintainable.

    Comment

    • Misstrius
      New Member
      • Jan 2013
      • 2

      #3
      Code:
      int createArithmeticSeq ( int first, int difference)
      {
          
          
          cout<<"Please enter the first value: ";
          cin>>first;
          
          cout<<"Please enter the difference: ";
          cin>>difference;
          cout<<endl;
          
          int seq[4][4];
          for (int i=1; i<=16; i++)
          {   
              
               for ( int row=0; row<4; row++)
             {
                   
                   
                      for( int col=0; col<4; col++)
                      {
      
                       seq[row][col] = first+(((i++)-1)*difference);
          
                       cout<<seq[row][col]<<" ";
                        
                       }
                       cout<<endl;
              }
      
      }
      }
      
      int reverseDiagonal ( int row, int col)
      {
          for ( row=0, col=0; row<4/2 && col<4/2; row++, col++)
          {
               
                int temp;
               temp = seq[row][col];
               seq[row][col] = seq[4-1-row][4-1-col];
               seq[4-1-row][4-1-col] = temp; 
                       
              
      
      
      
      
          
          for ( int row=0, col=4-1; row<4/2 && col>4/2; row++, col--)
          {
                int seq[4][4];
              swap(seq[row][col], seq[4-row-1][4-col]) ;
          }
      }

      ^ This is what I have done.
      I'am a beginner, so i'am finding it a little difficult. I'am not sure where to place the "cout". Please help
      Last edited by Meetee; Jan 9 '13, 11:08 AM. Reason: Use code tags <code/> around code

      Comment

      • simondclinch
        New Member
        • Jan 2013
        • 1

        #4
        The first thing to understand is that a function is intended to function correctly for all possible actual parameter values. So there shouldn't be any hard coded numbers in the function's code like the '4's you have put there. Next there should be a single parameter pointing to a 2D array, not 2 parameters being the dimensions of the array. I think you have misread some code published elsewhere on the internet that declares row and column as local dynamic variables rather than function parameters. Further, the term reverse diagonals is misleading as it can be interpreted as a trend calculation requiring processing all of the data, not just the values in the diagonals. So it needs to be specified what the application is before the term can be narrowed down in meaning. The cout syntax is the least of your worries and hardly worth dealing with at this stage in your efforts - at some stage though you will need to consider output, but you are not there yet!

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Suggest the following:
          1)Declare and initialize a 4x4 array with integers 1 to 16.
          int arr[4][4]={{1,2,3,4},{5, 6,7,8},etc.}};
          2)Print the array with 2 loops similar to those in your code.
          //loop_1 using int i.
          //loop_2 using int j.
          cout<<arr[i][j];//don't forget the '\n' and braces!
          3)Use if ...else statements and array indexes in combination with a swap() function (defined in <algorithm>)t o swap the relevant elements in the 2D array.(or write your own)
          4)Print the array to prove that the element positions have been change successfully.
          5)Refine to the level desired.(test small packets of code frequently as the program is built)

          Comment

          Working...