Computer Science Canada

need help with old CCC question (2001 J4S2)

Author:  StrikerMagazine [ Sat Nov 08, 2003 12:31 pm ]
Post subject:  need help with old CCC question (2001 J4S2)

i feel really stupid posting this.. (this probably isn't actually that hard a question for people here, but..)

it's:


A spiral of numbers can start and end with any positive integers less than 100. Write a program which will accept two positive integers x and y as input, and output a list of numbers from x to y inclusive, shown in a spiral. You may assume that the end value is greater than or equal to the start value.

A spiral starts with the first number in the centre. The next number appears immediately below the first number. The spiral continues with the numbers increasing in a counter-clockwise direction until the last number is printed.

Read the input from the keyboard and display the output on the screen.

Sample session:

Start value:
10
End value:
27
27 26
16 15 14 25
17 10 13 24
18 11 12 23
19 20 21 22

Start value:
10
End value:
12

12 11
7 10
8 9


i did it in turing and got a solution that works.. it's:

code:
var start, finish, r, c, counter, i, rchange, cchange : int
get start, finish
r := 13
c := 40
counter := 1
i := start
rchange := 1
cchange := 4
loop
    exit when i >= finish
    for j : 1 .. counter
        locate (r, c)
        if i < 10 then
            put " ", i ..
        else
            put i ..
        end if
        exit when i >= finish
        i += 1
        r += rchange
    end for
    for j : 1 .. counter
        locate (r, c)
        if i < 10 then
            put " ", i ..
        else
            put i ..
        end if
        exit when i >= finish
        i += 1
        c += cchange
    end for
    rchange *= -1
    cchange *= -1
    counter += 1
end loop


but i think there's a problem with spacing. it outputs a spiral that's by default in the center of the screen, but in the examples they somehow managed to align it to the left. i thought that maybe (look at a spiral that goes from 1 to 99 as reference?) before the spirals are even drawn i could set 'r' and 'c' equal to some other values other than the center of the screen; that way the spiral could be left-aligned. but i don't know any other way to set the values for r and c than writing them down (as compared to the start value) and making one huge if-statement... is there an easier way than that? (or, would i even need to bother with this sort of thing for a solution..?)

Author:  Tony [ Sat Nov 08, 2003 3:53 pm ]
Post subject: 

don't worry, your solution is more correct then the example Laughing

besides, to compare results for this type of problem, they should skip whitespaces

Author:  StrikerMagazine [ Sun Nov 09, 2003 10:22 am ]
Post subject: 

ok.. thanks =)

Author:  Andy [ Sun Nov 09, 2003 5:16 pm ]
Post subject: 

claire, what u can do is instead of out putting as you go, create a 2d array of structure containing a boolean var telling wether if that location is filled and an int to store a number, after putting values in it just output it using two for loops

Author:  StrikerMagazine [ Tue Nov 11, 2003 1:02 pm ]
Post subject: 

i guess that would work X_X hopefully they won't be checking that sort of thing, it'd seem to take more time than it's worth.. like, how i'm imagining it, i make a 2-dim 10x10 array to hold the values, initialize the whole thing to zeros and then make the spiral start recording values in the middle (i.e. spiral(5,5)) so rather than increasing rows and columns i could increase (say, x and y) just by one to move one spot over in the array each time a number is recorded. so at the end, i could output whatever spaces in the array have positive numbers in them... that would work, right? it kind of seems easier than making a 2-dim array where one is for boolean values, because it's seem like i'd lose that aspect of outputting it as a spiral.. (?) if the numbers are only held in one dimension

Author:  Andy [ Fri Dec 19, 2003 10:29 am ]
Post subject: 

claire ur so smart 0_____0


: