Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
need help with old CCC question (2001 J4S2)
Author Message
StrikerMagazine

Posted: 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..?)
Sponsor
Sponsor

Tony

Posted: Sat Nov 08, 2003 3:53 pm   Post subject: (No subject)

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

besides, to compare results for this type of problem, they should skip whitespaces
Tony's programming blog. DWITE - a programming contest.
StrikerMagazine

Posted: Sun Nov 09, 2003 10:22 am   Post subject: (No subject)

ok.. thanks =)
Andy

Posted: Sun Nov 09, 2003 5:16 pm   Post subject: (No 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
StrikerMagazine

Posted: Tue Nov 11, 2003 1:02 pm   Post subject: (No 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
Andy

Posted: Fri Dec 19, 2003 10:29 am   Post subject: (No subject)

claire ur so smart 0_____0
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 6 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: