Thread: Diagonals in Matrix

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    14

    Diagonals in Matrix

    Is there a way to store values diagonally from a matrix?
    E.g.
    a b c d
    e f g h
    i j k l
    m n o p

    output string:
    minejoafkp...

    is it possible to be done in a single nested loop?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Is using a simple nested loop an absolute requirement? I can think of one way to do this, but it involves a second array containing the coordinates of interest from the first array.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If your array is of fixed size then Matticus's suggestion is probably best for its simplicity.
    If your array can be any size, but is a square array, then the following seems to work.
    It could presumably be generalized to rectangular arrays.
    Code:
    #include <stdio.h>
    
    #define SIZE 5
    
    int main(void) {
        char m[SIZE][SIZE] = {
            {"AFJMO"},
            {"6BGKN"},
            {"37CHL"},
            {"148DI"},
            {"0259E"}
        };
    
        for (int i = SIZE; i-- > 0; )
            for (int row = i; row < SIZE; row++)
                putchar(m[row][row-i]);
        for (int i = SIZE; i-- > 0; )
            for (int row = 0; row < i; row++)
                putchar(m[row][row+SIZE-i]);
        putchar('\n');
    
        return 0;
    }

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    It can be done with a single loop (not nested) if you're willing to add a few more variables and some if statements to control how the "cursor" walks

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Hodor View Post
    It can be done with a single loop (not nested) if you're willing to add a few more variables and some if statements to control how the "cursor" walks
    Code:
    #include <stdio.h>
    
    #define SIZE 5
    
    int main(void) {
    
        char m[SIZE][SIZE] = {
            {"AFJMO"},
            {"6BGKN"},
            {"37CHL"},
            {"148DI"},
            {"0259E"}
        };
    
        int start = SIZE, col = 0, row = SIZE;
    
        for (int i = 0; i < SIZE*SIZE; i++) {
            if (row == SIZE && start > 0) {
                row = --start;
                col = 0;
            }
            else if (col == SIZE) {
                col = ++start;
                row = 0;
            }
            putchar(m[row++][col++]);
        }
    
        return 0;
    }
    Last edited by algorism; 03-31-2016 at 08:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Finding the diagonals of an array
    By Duncan Doll in forum C Programming
    Replies: 4
    Last Post: 04-11-2014, 11:56 PM
  3. Array Diagonals Reversion?
    By ieatcalculus in forum C++ Programming
    Replies: 3
    Last Post: 07-28-2010, 10:18 PM
  4. Diagonals
    By the_contractor in forum C Programming
    Replies: 7
    Last Post: 11-26-2009, 06:22 AM
  5. diagonals
    By the_contractor in forum C Programming
    Replies: 1
    Last Post: 11-23-2009, 03:35 AM

Tags for this Thread