GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Sep-2009, 02:49
pratikasthana17 pratikasthana17 is offline
New Member
 
Join Date: Sep 2009
Posts: 4
pratikasthana17 is an unknown quantity at this point

Array in spiral order


Your program should print the contents of a 2-dimensional array in spiral order. Suppose your array is
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
The output should be:
1 2 3 4 5 6 12 18 24 23 22 21 20 19 13 7 8 9 10 11 17 16 15 14
Your program should take m and n as inputs, where m and n specify the dimensions of the array. Once the user inputs m and n, the program should initialize the array with random numbers using the rand() function.
Once the array is initialized, print the actual contents of the array. Then, compute and print the array in spiral order.
  #2  
Old 16-Sep-2009, 06:20
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Array in spiral order


Quote:
Originally Posted by pratikasthana17
Your program should print the contents of a 2-dimensional array in spiral order.
This is the third post you have made which simply states a problem without showing any attempt at solution. See my following response:

http://www.gidforums.com/showpost.ph...02&postcount=2
  #3  
Old 17-Sep-2009, 15:01
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 757
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Array in spiral order


The way that I would do it is:
  1. Declare an array of the exact same size as your 2D array and fill it with zeros. I will call it Map.
  2. Declare a variable to keep track of your direction.
  3. Set the starting conditions: Position is 0,0 and direction is RIGHT.
  4. Print the value at the current position and set the value of the same position of Map to a 1.
  5. If the next position (depending on the direction) of Map is a 1 or if it is outside the bounds of the array, change the direction.
  6. If the next position (depending on the direction) of Map is a 1 or if it is outside the bounds of the array, you are done.
  7. Set position to the next position.
  8. Repeat steps 4-7 until done.
  #4  
Old 18-Sep-2009, 09:38
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 334
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Array in spiral order


Quote:
Originally Posted by pratikasthana17
Your program should print the contents of a 2-dimensional array in spiral order. Suppose your array is
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
The output should be:
1 2 3 4 5 6 12 18 24 23 22 21 20 19 13 7 8 9 10 11 17 16 15 14
Your program should take m and n as inputs, where m and n specify the dimensions of the array. Once the user inputs m and n, the program should initialize the array with random numbers using the rand() function.
Once the array is initialized, print the actual contents of the array. Then, compute and print the array in spiral order.

Here is a solution that works for square dimensions for your array. I'll leave it to you to decide how to implement m != n dimensioned array spiral printing.

You may want to consider fakepoo's "Map" strategy, though. Only twice the storage and processing!


CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void print_array(const int** pp, const size_t m, const size_t n)
{
    size_t i, j;
    if(m > 0 && n > 0 && pp != NULL)
    {
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                printf("%4d ", pp[i][j]);
            }
            printf("\n");
        } 
    }
}

void print_spiral(const int** pp, const size_t m, const size_t n)
{
    size_t i, j, k;
    if(m == n && n > 0 && pp != NULL)
    {
        for(i = (m-1), j = 0; i > 0; i--, j++)
        {
            for(k = j; k < i; k++)
            {
                printf("%4d ", pp[j][k]);
            }
            for(k = j; k < i; k++)
            {
                printf("%4d ", pp[k][i]);
            }
            for(k = i; k > j; k--)
            {
                printf("%4d ", pp[i][k]);
            }
            for(k = i; k > j; k--)
            {
                printf("%4d ", pp[k][j]);
            }
        }
        if(m % 2 == 1)
        {
            printf("%4d ", pp[(m-1)/2][(m-1)/2]);
        }
        printf("\n");
    }
}

int main()
{
    int     i;
    int     j;
    int     m;
    int     n;
    int**   pp;
    int     r;

#ifdef I_WANT_TO_SEE_RECOGNIZABLE_VALUES
    int     d = 1;
#endif

    printf("Enter 'm' dimension: ");
    scanf("%d", &m);

    printf("Enter 'n' dimension: ");
    scanf("%d", &n);

    r = m * n * 100;

    if(r > 1)
    {
        srand((unsigned)time(0));
        pp = (int**)malloc(sizeof(int*) * m);
        if(pp != NULL)
        {
            for(i = 0; i < m; i++)
            {
                pp[i] = (int*)malloc(sizeof(int) * n);
                for(j = 0; j < n; j++)
                {
#ifdef I_WANT_TO_SEE_RECOGNIZABLE_VALUES
                    pp[i][j] = d++;
#else
                    pp[i][j] = 1 + (int)(r * (rand() / (RAND_MAX + 1.0)));
#endif
                }
            }
        }
        print_array((const int**)pp, m, n);
        printf("\n\n");
        if(m == n)
        {
            print_spiral((const int**)pp, m, n);
        }

        for(i = 0; i < m; i++)
        {
            free(pp[i]);
        }
        free(pp);
    }
    return 0;
}


Output:

Code:
// with I_WANT_TO_SEE_RECOGNIZABLE_VALUES defined $ ./prati Enter 'm' dimension: 5 Enter 'n' dimension: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 3 4 5 10 15 20 25 24 23 22 21 16 11 6 7 8 9 14 19 18 17 12 13 // without I_WANT_TO_SEE_RECOGNIZABLE_VALUES defined $ ./prati Enter 'm' dimension: 4 Enter 'n' dimension: 4 172 619 568 405 1054 944 291 618 845 1450 409 459 1265 11 594 525 172 619 568 405 618 459 525 594 11 1265 845 1054 944 291 409 1450


MxB
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
What is an array? Howard_L C Programming Language 3 05-Oct-2007 06:11
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 15:43
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:09.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.