My challenge to everyone on this forum

+7 Lazar Bulic · January 24, 2015
The task is simple:

Users inserts an whole number N. Print a square  matrix using this pattern. Example is give for N = 4 

/images/forum/upload/2015-01-24/ef634fbc6b01a4b4ba36e74b01955c31.png

Let the games begin :D

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Bernhard Riemann Georg Friedrich · July 6, 2015
Looks like a Pascal's triangle, problem solved ;),
I also stumbled across that while watching the table video on C.
0 Kevin Faust · February 9, 2015
I solved it in C!
EDIT: I just realized it won't work for some numbers like 5 as shown below. God dam!


#include <stdio.h>
#include "string.h"
#define NAME "KEVIN"

int main(int argc, const char * argv[]) {
    int number;
    
    printf("Enter your desired number! \n");
    scanf(" %d", &number);
    
    int square[number][number];
    
    for(int i = 0; i != number; i ++) {
        for(int j = 0; j != number; j ++) {
            square[j] = 0;
        }
    }
    
    int direction = 0;
    /*
     0 | RIGHT
     1 | DOWN
     2 | LEFT
     3 | UP
     */
    
    int counter = 0;
    int currentX = 0;
    int currentY = 0;
    for(int i = 0; i != number * number + number; i ++) {
        counter ++;
        
        square[currentX][currentY] = counter;
        
        switch (direction) {
            case 0:
                if(square[currentX + 1][currentY] == 0) {
                    currentX ++;
                    break;
                } else {
                    direction ++;
                }
            case 1:
                if(square[currentX][currentY + 1] == 0) {
                    currentY ++;
                    break;
                } else {
                    direction ++;
                }
            case 2:
                if(square[currentX - 1][currentY] == 0) {
                    currentX--;
                    break;
                } else {
                    direction ++;
                }
            case 3:
                if(square[currentX][currentY - 1] == 0) {
                    currentY --;
                    break;
                } else {
                    counter --;
                    direction = 0;
                }
        }
    }
    
    for(int i = 0; i != number; i ++) {
        for(int j = 0; j != number; j ++) {
            square[j] > 9 ? printf(" %d", square[j]) : printf("  %d", square[j]);
        }
        printf(" \n");
    }
    
    return 0;
}




/images/forum/upload/2015-02-09/e8d92a131a97a0b311234bb82bf8550e.png
0 Lazar Bulic · February 9, 2015
Also @Luka Lopotar thanks for your solution of second matrix. I never saw the this way of declaring a matrix. Will need to dig in to it a bit :)
I think its a little more complicated the mine but its really good optimized.

And don't forget to free you memory!!!!
0 Lazar Bulic · February 9, 2015
---------------------------------------------Spoiler------------------------------------------------------
/images/forum/upload/2015-02-09/b5d1fd5fc79aa556ede1b42c2c699a98.jpg

---------------------------------------------Spoiler------------------------------------------------------
0 Luka Lopotar · February 9, 2015
Okay, here is the code for the second one: http://pastebin.com/Mn9DJpe4

Still figuring out the first one though. Not giving up yet :) . Some hints?
0 Lazar Bulic · February 9, 2015
Can you post the C code Kevin?
0 Kevin Faust · February 9, 2015
Nishant huh we think alike! I think thats very cool. I got mine to work in the same way you did but in C however mines a bit broken since when it goes into the inner wrapping, it breaks.

/images/forum/upload/2015-02-09/1686a197977ca4e1b1d30ff4ce48546f.png
/images/forum/upload/2015-02-09/cd2d252249d55df3ca49be4302f885af.png
+1 Lazar Bulic · February 9, 2015
Yes  Luka Lopotar you can just post the code here but this forum teds to mess up some character ([*i*] [*b*] ....) so its much better to paste theme at other site like http://pastebin.com/
+2 Nishant Kumar Thakur · February 9, 2015
Solution in python 2.7:-

from __future__ import print_function

def printMatrix(inputList):
    for rowItem in inputList:
        for colItem in rowItem:
            print(colItem, end=' ')
        print(" ")

def generateEmptyMatrix(N):
    outputMatrix = []
    for i in range(N):
        rowList = []
        for j in range(N):
            rowList.append(0)
        outputMatrix.append(rowList)
    return outputMatrix
            
        

def generateMatrix(N):
    outPutMatrix = generateEmptyMatrix(N)
    dir = 0
    val = 1
    
    #0 - West to East
    #1 - North to South
    #2 - East to West
    #3 - South to North

    j=0
    i=0
    while(outPutMatrix[i][j] == 0):
        outPutMatrix[i][j] = val
        val = val + 1
        if dir == 0:
            if j+1 < N: # next elem is within matrix
                if outPutMatrix[i][j+1] == 0: #if next elem is zero
                    j = j+1
                else:
                    dir = 1
                    i = i+1
            else:
                dir = 1
                i = i+1
        elif dir == 1:
            if i+1 < N: #next elem is within matrix
                if outPutMatrix[i+1][j] == 0:
                    i = i+1
                else:
                    dir = 2
                    j = j-1
            else:
                dir = 2
                j = j-1
        elif dir == 2:
            if j-1 >= 0 :
                if outPutMatrix[i][j-1] == 0:
                    j = j-1
                else:
                    dir = 3
                    i = i-1
            else:
                dir = 3
                i = i-1
        elif dir == 3:
            if i-1 >= 0 :
                if outPutMatrix[i-1][j] == 0:
                    i = i-1
                else:
                    dir = 0
                    j = j+1
            else:
                dir = 0
                j = j+1
        
    return outPutMatrix
            

if __name__ == '__main__':
    printMatrix(generateMatrix(5))
0 Luka Lopotar · February 9, 2015
Ok, I'm done with second one, still need to figure out the pattern for first one xD.  I assume I just post the code here? (Sorry, I'm new on this forum)
  • 1
  • 2

C

103,636 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator