square matrix, assigning number to diagonals and top row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nazzii
    New Member
    • Jun 2015
    • 2

    #1

    square matrix, assigning number to diagonals and top row

    Im using codeblocks
    Lets say primary matrix is:
    88888
    88888
    88888
    88888
    88888
    I need to make it to:
    00000
    80808
    88088
    80808
    08880

    Thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You just did it.

    Just put your desired result into code.

    Like, array[1][1] = 0; etc...

    Comment

    • nazzii
      New Member
      • Jun 2015
      • 2

      #3
      Well on this exercise it should be three c code functions that replaces 8 with 0. 1 for main diagonal, other for secondary diagonal and finaly top line functions. And finally a functions that prints everything. At the momentu i have functions that makes matrix, also functions that replaces primary diagonal and prints everything. Secondary diagonal and top line functions are the Ones i can not write properly...

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Look at your desired output. The forward diagonal starts at the last row of the array in the last position. Your example is a 5 element row so the forward diagonal starts at [0][0]. The next location is the previous row plus one more position since it is a diagonal. That would be [1][1]. Then [2][2], etc.

        The reverse diagonal starts at [0][4] then advances to the next row less 1 more byte since the reverse diagonal retreats. That would be [1][3], [2][2], [3][1], and [4][0].

        If you look at these indexes you can see a progression where you can use a loop counter to advance the rows plus another counter where you add 1 for the forward diagonal and subtract 1 for the reverse diagonal.

        Your functions will need the address of the array, the number of rows and columns and maybe a toggle to tell the function to make the diagonal forwards or reverse. Maybe even another argument for the value to place in the diagonal positions.

        The top line is simply places a zero in [0][0],[0][1], until you reach the end of the row.

        Comment

        Working...