How to print numbers in a spiral order?
Asked Answered
A

4

0

Thank you , i am trying to solve a project euler problem it wants me to print the sum of

21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13

this is formed by starting with the number 1 and moving to the right in a clockwise direction for a 5 by 5 matrix but i am in trouble writing a code for the spiral matrix !!

Abelabelard answered 4/1, 2014 at 20:0 Comment(2)
how far have you reached in coding this ?Overrun
i tried to write it in C++ but cant get anything.Abelabelard
O
0

It is Highly recommended to do project Euler problems on your own and ask for help if you are really stuck

here is how i will write a code in c to print a spiral as suggested in the question

#include<stdio.h>
main()
{
int i,j,nq=9;//nq is a odd number which represents the order of the matrix
int lim=(int)nq/2,cnt=2;
int a[nq][nq];
 for(i=0;i<nq;i++){
    for(j=0;j<nq;j++)
    a[i][j]=0;
    }
a[lim][lim]=1;
a[lim][lim+1]=2;
int i1=lim,j1=lim+1;i=lim,j=lim;
while(1){
       if(cnt>(nq*nq))
         break;
        cnt++;
if(i==i1)
{  j=j1;
   if(i<=lim)
   {
       i=i1;
    if(a[i1+1][j1]==0)
        a[++i1][j]=cnt;
    else
       a[i1][++j1]=cnt;
   }
   else
   {   i=i1;
    if(a[i1-1][j1]==0)
       a[--i1][j1]=cnt;
    else
        a[i1][--j1]=cnt;
   }
}
else
{   i=i1;
    if(j<lim)
   {
        j=j1;
       if(a[i1][j+1]==0)
        a[i1][++j1]=cnt;
       else
        a[--i1][j1]=cnt;
   }
   else
   {    j=j1;
       if(a[i1][j1-1]==0)
        a[i1][--j1]=cnt;
       else
        a[++i1][j1]=cnt;
   }
  }
}
for(i=0;i<nq;i++){
    for(j=0;j<nq;j++)
    printf(" %d    ",a[i][j]);
    printf("\n");
}

}

I Googled your question http://projecteuler.net/problem=28 this can also be solved by taking advantage of its mathematical nature note that

Top right corner is n^2 and other corners can be shown to be n^2-2n+2 ,n^2-n+1, and n^2-3n+3. you just need to sum those corners which comes to be

= 4*n^2 - 6*n + 6

hence the final answer can be calculated by iterating over every second number from 1001 to 3

long int sum(int n){
    long int sum=1;
    while(n>1){
      sum=sum+4*n*n-6*n+6;
      n=n-2;
    }
    return sum;
    } 
Overrun answered 4/1, 2014 at 20:35 Comment(0)
V
0

I dont know whether you actually want to print the spiral but see below for my solution for #28 written in Python 2.7.

l = [1]
def corners(step,l):
    counter = 0
    while counter < 4:
        l.append(max(l)+step)
        counter +=1
    return l

step = 2
while step < 1001:
    l = corners(step, l)
    step += 2

print sum(l)
Versicular answered 4/6, 2016 at 13:5 Comment(0)
C
0
    void printSpiral(int A[3][5],int m, int n)
{
    int T=0; int B=m-1; int L=0; int R=n-1;
    int dir=0;
    int i =0; int j=0; int k=0; int l=0;
    while(T<=B && L<=R)    
    {
        //printf("dir %d ",dir);
        if(dir == 0)
        {
        for( i=L;i<=R;i++)
        {
        printf("%d ",A[T][i]);
        //printf("\n");
        }
        T++;
        dir=1;  
        }
        else if(dir == 1)
        {
          //  printf("%d R ",R);
        for( j=T;j<= B;j++)
        {
        printf("%d ",A[j][R]);
        //printf("\n");
        //printf("dir1");
        }
        dir=2;
        R--;
        }
        else if(dir == 2)
        {
        for(k=R;k>= L;k--)
        {
        printf("%d ",A[B][k]);
        }
        dir=3;
        B--;
        }
        else if(dir == 3)
        {
        for( l=B;l>= T;l--)
        {
            printf("%d ",A[l][L]);
        }
        L++;
        dir=0;
        }    
    }
}
Circumstantiality answered 15/9, 2018 at 18:42 Comment(0)
L
0

Here is my implementation of the problem. It works with any odd number of matrix sizes. I realized once you start filling the matrix from the center, and going in right-down-left-up order, the number of entries to print increases after 2 iterations in two directions. It works like:

place 1
place 2
place 3 4
place 5 6 
place 7 8 9
place 10 11 12 
...

Here is the implementation in C.

#include <stdio.h>

#define MATRIX_SIZE 9

enum direction{
    RIGHT,
    DOWN,
    LEFT,
    UP
};

void incrementDirection(enum direction *dir){
    (*dir)++;
    if(*dir > UP){
        *dir = RIGHT;
    }
}

void nextIndices(enum direction *dir, int* x, int* y){
    if(*dir == RIGHT){
        (*x)++;
    }
    else if(*dir == DOWN){
        (*y)++;
    }
    else if(*dir == LEFT){
        (*x)--;
    }
    else if(*dir == UP){
        (*y)--;
    }
}

void printMatrix(int* matrix){
    for(int i = 0; i < MATRIX_SIZE; i++){
        for(int j = 0; j < MATRIX_SIZE; j++){
            printf("%d\t", matrix[i*MATRIX_SIZE + j]);
        }
        printf("\n");
    }
}


int main(){
    int matrix[MATRIX_SIZE][MATRIX_SIZE];

    if(!(MATRIX_SIZE % 2)){
        printf("Matrix size must be odd.\n");
        return 1;
    }

    int x = (MATRIX_SIZE / 2);
    int y = x;

    int num = 1;
    int printAmount = 1;
    int stopFlag = 0;
    enum direction dir = RIGHT;
    int oneTwoCounter = 0;

    for(;;){
        for(int j = 0; j < printAmount; j++){
            matrix[y][x] = num;
            num++;

            if(num > MATRIX_SIZE*MATRIX_SIZE){
                stopFlag++;
                break;
            }
            
            nextIndices(&dir, &x, &y);
        }

        if(stopFlag){
            break;
        }

        oneTwoCounter++;
        if(oneTwoCounter == 2){
            oneTwoCounter = 0;
            printAmount++;
        }

        incrementDirection(&dir);
    }

    printMatrix((int*)matrix);

    return 0;
}
Labor answered 30/3 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.