“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
int a[][]=new int[3][4];
a[0][]={A,B,C,D,E};
a[1][]={1,2,3,4,5};
a[2][]={A,B,C,D,E};
A[3][]={1,2,3,4,5};
int n=3;
int m=4;
int k=0;
for(i=0;i=n;i++)
{
if(i==0)
{
for(j=0;j=m;j++)
{
printf(”%c”,a[i][j]);
}
}
if(i!=0 && i
class ALG_TEST
{
public static char [,]str={{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′},{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′},{’A',’B',’C',’D',’E'}};
public static char [,]str={{’A',’B',’C',’D'},{’1′,’2′,’3′,’4′},{’A',’B',’C',’D'},{’1′,’2′,’3′,’4′}};
public static void Top(int r,int c,int t)//r is row number c is max no of columns, t is an initialization variable.
{
int i;
for(i=t;it;i–)
Console.Write(str[r-1,i]);
}
public static void Left(int r,int c,int t)//r is max no of rows and c is column number, t is an initialization variable.
{
int i;
for(i=r-1;i>t;i–)
Console.Write(str[i,c]);
}
}
Note: This program is written in C# but shouldnt matter you can simply replace Console.Write with cout
in TURBO PASCAL:
program print;
uses wincrt;
const m=?
n=?
procedure print(t:array[1..m,1..n]of char;i,j,n:integer);
var k:integer;
begin
if(i>0)then
begin
for k:=n to j do write(t[nb,k]);
for k:=n+1 to i do write(t[k,j]);
for k:=j-1 downto n do write(t[i,k]);
for k:=i-1 downto n+1 do write(t[k,nb]);
print(t,i-1,j-1,n+1);
end;
end;
begin print(t,m,n,1);end.
void print_spiral(char array[MAX_ROWS][MAX_COLS])
{
int curc = 0,curr=0,maxc=MAX_COLS,maxr=MAX_ROWS;
int i;
do
{
for(i=curc;icurc;i–)
print(array[maxr][i]);
maxr–;
void spiral_traversal(int nrows, int ncols) {
int row = 0; // row to traverse
int col = 0; // column to traverse
int rows = nrows; // # of rows to traverse
int cols = ncols; // # of cols to traverse
while (row
Here is the full compiled working code. Program need the size of the Matrix mXn (m,n
int main()
{
int i=0,j=0,c=0,max,m,n;
int r=0,l=0,d=0,u=1,flag;
int direction=1; //Right=1 Down=2 Left=3 Up=4
int v[10][10];
printf(”Enter the MATRIX size:m n”);
scanf(”%d %d”,&m,&n);
#region TRAVERSE FUNCTION
public static void Traverse(int m, int n, bool bGoingUP)
{
// print the current node ignore visited node
if (Array[m, n] != -1)
{
Console.Write(”{0} “, Convert.ToString(Array[m, n]));
}
// mark the current node as visited
Array[m, n] = -1;
// Recurse right
if ((n 0) && (Array[m, n - 1] != -1) && !bGoingUP)
{
Traverse(m, n - 1, false);
return;
}
// Recurse up
// Note: without bGoingUp controller, it will traverse in zigzag style rather than spiral.
if ((m > 0) && (Array[m - 1, n] != -1))
{
Traverse(m - 1, n, true);
return;
}
int arr[4][5];
main()
{
int i, count;
for( i=0; i0)
{
printf(” %d “, arr[i][count]);
if (i%2 = 0 && count 0) // odd row
–count;
}
}
}
/* soory for the previous post*/
main()
{
int arr[4][5];
int i, count;
for( i=0; i0) {
printf(” %d “, arr[i][count]);
if (i%2 = 0 && count 0) // odd row
–count;
}
}
hey Deepanshu
did you try compiling this?
its already answered here on this same forum.
Write a routine that prints out a 2-D array in spiral order.
http://www.acetheinterview.com/questions/cats/index.php/algorithm/2006/10/06/write-a-routine-that-prints-out-a-2-d-array-in-spi-by#comments
int a[][]=new int[3][4];
a[0][]={A,B,C,D,E};
a[1][]={1,2,3,4,5};
a[2][]={A,B,C,D,E};
A[3][]={1,2,3,4,5};
int n=3;
int m=4;
int k=0;
for(i=0;i=n;i++)
{
if(i==0)
{
for(j=0;j=m;j++)
{
printf(”%c”,a[i][j]);
}
}
if(i!=0 && i
#include
#define row 4
#define column 5
char a[row][column] = {{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′},{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′}};
void print (int ibegin,int iend,int jbegin,int jend){
int i,j;
for(j=jbegin;j=jbegin;j–)
printf(”%c “,a[iend][j]);
printf(”\n”);
for(i=iend-1;i>=ibegin+1;i–)
printf(”%c “,a[i][jbegin]);
printf(”\n”);
ibegin++;iend–;
jbegin++;jend–;
if ((ibegin
void print (int ibegin,int iend,int jbegin,int jend){
for(j=jbegin;j=jbegin;j–)
printf(”%c “,a[iend][j]);
for(i=iend-1;i>=ibegin+1;i–)
printf(”%c “,a[i][jbegin]);
ibegin++;iend–;jbegin++;jend–;
if ((ibegin
int nrows = 4;//no of rows
int ncols = 5;//no of cols
int i=0;
for(int j=0;j0;j–)
print a[i][j];
j=0;
for(i=nrows;i>0;i–)
print a[i][j];
i=1;
for(j=1;j0;j–)
print a[i][j];
2nd attempt cutting a pasting does not work so good hope this works.
char data[4][5]={ {’A',’B',’C',’D',’E'}, {’1′,’2′,’3′,’4′,’5′}, \
{’A',’B',’C',’D',’E'}, {’1′,’2′,’3′,’4′,’5′} };
void across ( int & x, const int y, int & limit, int & count ) {
while ( x limit ) {
std::cout limit) {
std::cout = 0 ) {
across(x,y,xlimit,count);
down(x,y,ylimit,count);
back(x,y,xbaselimit,count);
up(x,y,ybaselimit,count);
}
std::cout
public class Spiral {
public static void main(String[] args) {
char[][] arr = { { ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ }, { ‘1′, ‘2′, ‘3′, ‘4′, ‘5′ },
{ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ }, { ‘1′, ‘2′, ‘3′, ‘4′, ‘5′ } };
int i = 0;
Direction direction = Direction.values()[i];
int x = 0;
int y = 0;
int x0 = 0;
int y0 = 0;
int radiusX = 4;
int radiusY = 3;
int stepX = 0;
int stepY = 0;
while (radiusX > 0 || radiusY > 0) {
System.out.print(arr[y][x]);
if (stepX >= radiusX || stepY >= radiusY) {
stepX = 0;
stepY = 0;
i = (i + 1) % Direction.values().length;
direction = Direction.values()[i];
}
switch (direction) {
case DOWN:
stepY++;
y++;
break;
case LEFT:
stepX++;
x–;
break;
case RIGHT:
stepX++;
x++;
break;
case UP:
stepY++;
y–;
break;
default:
break;
}
if (x == x0 && y == y0) {
direction = Direction.RIGHT;
i = 0;
radiusX -= 2;
radiusY -= 2;
stepX = 0;
stepY = 0;
x0++;
y0++;
x = x0;
y = y0;
}
}
}
private enum Direction {
RIGHT, DOWN, LEFT, UP
}
}
#include
char a[4][4]={
{ ‘a’,'b’,'c’,'d’},
{ ‘1′,’2′,’3′,’4′},
{ ‘a’,'b’,'c’,'d’},
{ ‘1′,’2′,’3′,’4′}
};
#define RIGHT 0
#define DOWN 1
#define LEFT 2
#define UP 3
int main()
{
int dir;
int i,j,k;
i=0;//initial
j=3;//size
dir = RIGHT;
while(i=i;k–)
{
printf(” %c “,a[j][k]);
}
dir = UP;
printf(” * “);
break;
case UP:
for(k=j-1;k>i;k–)
{
printf(” %c “,a[k][i]);
}
printf(” * “);
//new array size
i++;
j–;
dir = RIGHT;
//printf(”one round \n”);
break;
} //end of switch
}//end of while
}//end of main
#include
#define row 4
#define col 5
char array[4][5]=
{{’A',’B',’C',’D',’E'},
{’1′,’2′,’3′,’4′,’5′},
{’A',’B',’C',’D',’E'},
{’1′,’2′,’3′,’4′,’5′}};
int main()
{
int ibegin = 0;
int iend = row-1;
int jbegin = 0;
int jend = col-1;
int d = 0;
int k;
int i, j;
i = j = 0;
for (k = 0; k
void spiraltraversal()
{
char a[5][5] = {{’a',’b',’c',’d',’e'},{’1′,’2′,’3′,’4′,’5′},{’a',’b',’c',’d',’e'},{’1′,’2′,’3′,’4′,’5′},{’a',’b',’c',’d',’e'}};
int cols = 5;int rows = 5;
int current = -1;
int i = 0;
int n = 0;
while(n lessthan rows)
{
for(i = 0 ; i lessthan (cols-n) ; i++)
{
if(n%2 == 0)
current ++;
else
current –;
printf(”Index: %c\n”, a[current/c][current%c]);
}
for(i = 0 ; i lessthan (rows-1-n) ; i++)
{
if(n%2 == 0)
current +=c;
else
current -=c;
printf(”Index: %c\n”, a[current/c][current%c]);
}
n++;
}
}
#include
using namespace std;
int main()
{
int arr[5][5] = {{0,1,2,3,4}, {15,16,17,18,5}, {14,23,24,19,6}, {13,22,21,20,7}, {12,11,10,9,8}};
int i, j, x, y;
for (x=0, y=4; xx; j–)
{
printf(”%d “, arr[i][j]);
}
for (j=x, i=y; i>x; i–)
{
printf(”%d “, arr[i][j]);
}
}
if (x == y)
{
printf(”%d “, arr[x][y]);
}
return 1;
}
class ALG_TEST
{
public static char [,]str={{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′},{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′},{’A',’B',’C',’D',’E'}};
public static char [,]str={{’A',’B',’C',’D'},{’1′,’2′,’3′,’4′},{’A',’B',’C',’D'},{’1′,’2′,’3′,’4′}};
static void Main(string[] args)
{
int count=0,rmax=4,cmax=4,i,temp;
temp=rmax/2;
for(i=0;i=0)&&(cmax>=0))
{
Top(count+i,cmax-i,count+i);
Right(rmax-i,cmax-i,count+i);
Bottom(rmax-i,cmax-i,count+i);
Left(rmax-i,count+i,count+i);
}
}
if((rmax==cmax)&&((rmax%2)!=0))
Console.Write(str[rmax/2,cmax/2]+”\n”);
}
public static void Top(int r,int c,int t)//r is row number c is max no of columns, t is an initialization variable.
{
int i;
for(i=t;it;i–)
Console.Write(str[r-1,i]);
}
public static void Left(int r,int c,int t)//r is max no of rows and c is column number, t is an initialization variable.
{
int i;
for(i=r-1;i>t;i–)
Console.Write(str[i,c]);
}
}
Note: This program is written in C# but shouldnt matter you can simply replace Console.Write with cout
in TURBO PASCAL:
program print;
uses wincrt;
const m=?
n=?
procedure print(t:array[1..m,1..n]of char;i,j,n:integer);
var k:integer;
begin
if(i>0)then
begin
for k:=n to j do write(t[nb,k]);
for k:=n+1 to i do write(t[k,j]);
for k:=j-1 downto n do write(t[i,k]);
for k:=i-1 downto n+1 do write(t[k,nb]);
print(t,i-1,j-1,n+1);
end;
end;
begin print(t,m,n,1);end.
A general soln
#define MAX_ROWS
#define MAX_COLS
void print_spiral(char array[MAX_ROWS][MAX_COLS])
{
int curc = 0,curr=0,maxc=MAX_COLS,maxr=MAX_ROWS;
int i;
do
{
for(i=curc;icurc;i–)
print(array[maxr][i]);
maxr–;
for(i=maxr;i>curr;i–)
print(array[i][curc]);
curc++;
}while(curc
#include “stdafx.h”
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char arr[4][5] = { { ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ }, { ‘1′, ‘2′, ‘3′, ‘4′, ‘5′ },{ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ }, { ‘1′, ‘2′, ‘3′, ‘4′, ‘5′ } };
int rowMax = 3, rowMin = 0, colMax = 4, colMin = 0, rInc = 0, cInc = 1, rCurr = 0, cCurr = 0;
bool cond=false;
while (true)
{
cout colMax)
{
cInc = 0; rInc = 1; rowMin++;
}
if ((cCurr + cInc) rowMax)
{
rInc = 0; cInc = -1; colMax–;
}
if (((rCurr + rInc) = rowMax) && (colMin >= colMax)) // Stop condition
break;
rCurr += rInc;
cCurr += cInc;
}
return 0;
}
without much trickery:
Assuming a global array
void spiral_traversal(int nrows, int ncols) {
int row = 0; // row to traverse
int col = 0; // column to traverse
int rows = nrows; // # of rows to traverse
int cols = ncols; // # of cols to traverse
while (row
Here is the full compiled working code. Program need the size of the Matrix mXn (m,n
int main()
{
int i=0,j=0,c=0,max,m,n;
int r=0,l=0,d=0,u=1,flag;
int direction=1; //Right=1 Down=2 Left=3 Up=4
int v[10][10];
printf(”Enter the MATRIX size:m n”);
scanf(”%d %d”,&m,&n);
for(i=0;i=l )
{
printf(” %d”,v[i][j]); j–;
flag=1;
}
l++; direction = 4; j++;i–;
break;
case 4 :while ( i>=u )
{
printf(” %d”,v[i][j]); i–;
flag=1;
}
u++; direction = 1; i++;j++;
break;
default: return(0);}
}
return(0);
}
//JAVA code
public String doIt(char[][] m){
String str = “”;
int row = m.length - 1;
int col = m[0].length - 1;
int size = m.length * m[0].length;
for(int i = 0; i = i && str.length() = 1 + i && str.length()
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace Microsoft.SpiralBusted
{
class Program
{
#region SETUP ARRAY
public const int ROW_SIZE = 5;
public const int COLUMN_SIZE = 6;
static int[,] Array = new int[ROW_SIZE, COLUMN_SIZE]
{ {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, 26, 27, 28, 29, 30} };
#endregion
#region TRAVERSE FUNCTION
public static void Traverse(int m, int n, bool bGoingUP)
{
// print the current node ignore visited node
if (Array[m, n] != -1)
{
Console.Write(”{0} “, Convert.ToString(Array[m, n]));
}
// mark the current node as visited
Array[m, n] = -1;
// Recurse right
if ((n 0) && (Array[m, n - 1] != -1) && !bGoingUP)
{
Traverse(m, n - 1, false);
return;
}
// Recurse up
// Note: without bGoingUp controller, it will traverse in zigzag style rather than spiral.
if ((m > 0) && (Array[m - 1, n] != -1))
{
Traverse(m - 1, n, true);
return;
}
// make another round till it’s finished
if ((m
char ring[4][6] =
{
“ABCDE” ,
“12345″ ,
“ABCDE” ,
“12345″ ,
};
int bounds[2][2];
bounds[0][0] = 0; bounds[0][1] = 5;
bounds[1][0] = 0; bounds[1][1] = 4;
do{
// right
for ( int c = bounds[0][0] ; c = bounds[0][0] ; c– ) {
printf(”%c”, ring[bounds[1][1]-1][c]);
}
bounds[1][1]– ;
// up
for ( int l = bounds[1][1]-1 ; l >= bounds[1][0] ; l– ) {
printf(”%c”, ring[l][bounds[0][0]]);
}
bounds[0][0]++ ;
} while ( bounds[0][0] != bounds[0][1] && bounds[1][0] != bounds[1][1] );
#include “stdafx.h”
#include
#define row 4
#define col 5
using namespace std;
int main(void)
{
int climit, rlimit;
char a[row][col]={{’A',’B',’C',’D',’E'},{’1′,’2′,’3′,’4′,’5′}, {’A',’B',’C',’D',’E'}, {’1′,’2′,’3′,’4′,’5′}};
int rmin=0; int rmax=row-1;
int cmin=0; int cmax=col-1;
int element=sizeof(a)/sizeof(a[0][0]);
int count=0; int sign=1;
int i=rmin; int j=cmin;
while(element>count)
{
if(element>count)
{
if(sign==-1) {climit=cmin; rlimit=rmin;}
else {climit=cmax; rlimit=rmax;}
for(; (j*sign)count)
{
for(; (i*sign)
Leave an Answer/Comment