Write a routine that prints out a 2-D array in spiral order

Answers were Sorted based on User's Feedback



Write a routine that prints out a 2-D array in spiral order..

Answer / rajendra kumar

#include<stdio.h>
#include<conio.h>
#define n 4
void main()
{
int A[n][n]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int min=0,max=n-1,i,j;
clrscr();
while(min<max)
{
for(i=min;i<=max;i++)
printf("%d,",A[min][i]);
for(i=min+1;i<=max;i++)
printf("%d,",A[i][max]);
for(i=max-1;i>=min;i--)
printf("%d,",A[max][i]);
for(i=max-1;i>min;i--)
printf("%d,",A[i][min]);
min++;
max--;
}
getch();
}

Is This Answer Correct ?    27 Yes 11 No

Write a routine that prints out a 2-D array in spiral order..

Answer / mahfooz

#include<stdio.h>
#define n 3
int main()
{
int arr[n][n]={{1,2,3},{4,5,6,},{7,8,9}};
int min=0,max=n-1,i;
while(min<=max)
{
for(i=min;i<=max;i++)
printf("%d ",arr[min][i]);
for(i=min+1;i<=max;i++)
printf("%d ",arr[i][max]);
for(i=max-1;i>=min;i--)
printf("%d ",arr[max][i]);
for(i=max-1;i>min;i--)
printf("%d ",arr[i][min]);
min++;
max--;
}
if(min==max) //this for odd case as n=3,5 etc
printf("%d \n",arr[min][min]);
return 0;
}

Is This Answer Correct ?    8 Yes 7 No

Write a routine that prints out a 2-D array in spiral order..

Answer / raghuram.a

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
main()
{
int n,m,i=0,j=0,k=0,l=0,p=0,q=1,r=0,s=1,a=40,b=25,ar[100];
clrscr();
cout<<"enter n:";
cin>>n;
printf("enter array elements:");
for(m=0;m<n;m++)
scanf("%d",&ar[m]);
gotoxy(a,b);
m=0;
printf("%d",ar[m++]);
while(m<n)
{
while(i<=j)
{
i++;
a+=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;
}

while(k<=l)
{ k++;
b+=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;
}
while(p<=q)
{
p++;
a-=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;

}
while(r<=s)
{
r++;
b-=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;
}
j+=2;
l+=2;
q+=2;
s+=2;
i=0;
k=0;
p=0;
r=0;

}
next:getch();
return 0;
}



Is This Answer Correct ?    1 Yes 13 No

Post New Answer

More C Code Interview Questions

main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)

7 Answers   Microsoft,


main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }

1 Answers  


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,


main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error

1 Answers   HCL,






main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } }

1 Answers  


How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw...

6 Answers   Microsoft, MSD, Oracle,


Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.

2 Answers  


How to return multiple values from a function?

7 Answers  


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

0 Answers  


void main() { int i=5; printf("%d",i+++++i); }

3 Answers  


How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.

1 Answers  


Categories