problem on arrays plz help its urgent

P

Poornima

Hello

Let A be an array of n elements.Procedure which takes this array as i/p
and produce a matrix which shuold be spiral in nature i.i

if A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13

if n is not a full square then dummy elements can be included to make
the o/p matrix complete.

can any one of you help me in how to write the above in C

Thanks In Advance
Poornima
 
R

Richard Heathfield

Poornima said:

can any one of you help me in how to write the above in C

How would you do it on paper?

1. Fully document your method.
2. Examine each step in your method. Is it sufficiently simple that you
could explain it to a computer? If not, split it into two or more simpler
descriptions, and return to Step 2.
3. Translate your description into C.
 
S

santosh

Poornima said:
Hello

Let A be an array of n elements.Procedure which takes this array as i/p
and produce a matrix which shuold be spiral in nature i.i

if A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13

if n is not a full square then dummy elements can be included to make
the o/p matrix complete.

can any one of you help me in how to write the above in C

The real meat of the program would be the algorithm or set of
algorithms that inject the array elements into the matrix in a spiral
fashion. It's not hard to do, but it requires a bit of thinking. As
Richard suggests, it's better to sit down with a pencil and paper and
write the generic, (i.e. independant of any particular syntax), steps
needed to accomplish your task. Once that's done, translating the steps
into C will be fairly straightforward.

I suggest making a first attempt and posting here or in
comp.programming. You'll generally get better responses to an actual
attempt with code than simple pleas for help.
 
S

Sourcerer

Poornima said:
Hello

Let A be an array of n elements.Procedure which takes this array as i/p
and produce a matrix which shuold be spiral in nature i.i

if A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13

if n is not a full square then dummy elements can be included to make
the o/p matrix complete.

can any one of you help me in how to write the above in C

No, but I can give you a hint as to where to start. Suppose n is a full square.
Matrix size is sqrt(n) by sqrt(n) fields.
Number 1 should be at the position: ((int) sqrt(n)) / 2, ((int) sqrt(n)) / 2
Now, note the pattern in the spiral matrix: to make a spiral, you first move
(inside the matrix) once horizontally to the right, then once vertically down.
After that, you move twice horizontally to the left and twice vertically up.
Then three times horizontally to the right and three times vertically down, etc.
(Get the pattern?)
At each step, you write down the number which is next in the input array.
Now translate this into C by the method given to you by Richard.

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
S

Sourcerer

Sourcerer said:
Number 1 should be at the position:

Sorry, this should be "The first number in the list should be at the position:"

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

plz help its urgent 3
SENTINEL CONTROL LOOP WHEN DEALING WITH TWO ARRAYS 1
Help with code 0
Help with Loop 0
Help with my responsive home page 2
arrays 5
Minimum Total Difficulty 0
Python code problem 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top