Thread: Could this idea work?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    Unhappy Could this idea work?

    Could this idea work?-54524450_448724935669210_6196963446722920448_n-jpg

    This thing here is a "rectangular progressive spiral". Each point (0, 1, 2, 3, 4, 5...) has x-y coordinates. The goal of my program is to allow the user to type the number of a point (0, 1, 2, 3, 4, 5...) and the program should print its x-y coordinates according to the progression of the spiral...

    I have come up with this code, but it doesn't seem to work. If someone can take a look at it and give me some advise, I would truley appreciate it

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    struct COORDINATES
    {
        int x;
        int y;
    };
    
    typedef struct COORDINATES coordinates;
    
    
    int main()
    {
        int i = 0, n = 0, a, N;
        coordinates spiral[10000];
    
        printf("Enter N= ");
        scanf("%d", &N);
    
        spiral[0].x = 0;
        spiral[0].y = 0;
    
        while (1)
        {
    
            for (a = 0; a < n + 1; a++)
            {
                spiral[1 + i].x = spiral[i].x + 1;
                spiral[1 + i].y = spiral[i].y;
                i++;
                if (i > N)
                    break;
            }
    
            a = 0;
    
            do {
                spiral[1 + i].x = spiral[i].x;
                spiral[1 + i].y = spiral[i].y + 1;
                i++; a++;
                if (i > N)
                    break;
            } while (a < n);
    
    
            if (n > 0)
    
            {
                for (a = 0; a < n; a++)
                {
                    spiral[1 + i].x = spiral[i].x;
                    spiral[1 + i].y = spiral[i].y + 1;
                    i++;
                    if (i > N)
                        break;
                }
    
            }
    
    
            for (a = 0; a < n + 1; a++)
            {
                spiral[1 + i].x = spiral[i].x - 1;
                spiral[1 + i].y = spiral[i].y;
                i++;
                if (i > N)
                    break;
            }
    
    
            for (a = 0; a < n + 1; a++)
            {
                spiral[1 + i].x = spiral[i].x - 1;
                spiral[1 + i].y = spiral[i].y;
                i++;
                if (i > N)
                    break;
            }
    
    
    
            for (a = 0; a < n + 1; a++)
            {
                spiral[1 + i].x = spiral[i].x;
                spiral[1 + i].y = spiral[i].y - 1;
                i++;
                if (i > N)
                    break;
            }
    
    
    
            for (a = 0; a < n + 1; a++)
            {
                spiral[1 + i].x = spiral[i].x;
                spiral[1 + i].y = spiral[i].y - 1;
                i++;
                if (i > N)
                    break;
            }
    
    
    
            for (a = 0; a < n + 1; a++)
            {
                spiral[1 + i].x = spiral[i].x + 1;
                spiral[1 + i].y = spiral[i].y;
                i++;
                if (i > N)
                    break;
            }
    
    
            n++;
    
        }
    
        printf("The coordinates of N= %d are: \n\n", N);
        printf("x = %d\n\n", spiral[N].x);
        printf("y = %d\n\n", spiral[N].y);
    
        system("pause");
    
        return 0;
    
    }
    Have fun

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Is this spiral finite as shown, or is it theoretically infinite? If the former, then maybe a lookup table would be the easiest option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    Well you've managed to ctrl-c/ctrl-v your way into a hole, without studying the problem.

    Careful observation would note the following.
    - a cycle of right, up, left, down
    - right and up are of length N, and left and down are of length N+1. The pattern repeats again at N+2.

    Code:
    #include <stdio.h>
    
    typedef struct coord {
        int x, y;
    } coord;
    
    coord createSide(coord start, coord direction, int steps, int *num) {
        for ( int i = 0 ; i < steps ; i++ ) {
            start.x += direction.x;
            start.y += direction.y;
            printf("%d coord=%d %d\n", *num, start.x, start.y);
            (*num)++;
        }
        return start;
    }
    
    int main() {
        coord directions[] = {
            { 1, 0 },   // right
            { 0, 1 },   // up
            { -1, 0 },  // left
            { 0, -1 },  // down
        };
        coord start = { 0, 0 };
        int num = 1;
        for ( int side = 1 ; side <= 9 ; side += 2 ) {
            start = createSide(start,directions[0],side,&num);
            start = createSide(start,directions[1],side,&num);
            start = createSide(start,directions[2],side+1,&num);
            start = createSide(start,directions[3],side+1,&num);
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    @Gabriel.K, just a thought: If you intend to solve the URI Online exercise this way, it will fail... too slow!

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    The trick to this problem is finding how far above a perfect square your number is...

    If the square is from an odd number, you know to start from the line going down/right; you know how far up you should go before turning left...

    If the square is from an even number, you know to start from the line going up/left; down... and then right...


    If I tell you any more, I will rob you of your learning experience!
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i have no idea why this does not work
    By bjl19901 in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2007, 08:39 AM
  2. Idea
    By Cosmic Diva in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-01-2002, 06:51 AM
  3. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. A new idea...and it just might work...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-04-2001, 10:54 PM

Tags for this Thread