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

How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,


how can u draw a rectangle in C

53 Answers   Accenture, CO, Codeblocks, Cognizant, HCL, Oracle, Punjab National Bank, SAP Labs, TCS, University, Wipro,


Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector

2 Answers  


int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }

1 Answers  


Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable.

5 Answers   Amazon, Microsoft,






what is the output of the below program & why ? #include<stdio.h> void main() { int a=10,b=20,c=30; printf("%d",scanf("%d%d%d",&a,&b,&c)); }

6 Answers   CSC, IIIT,


void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }

1 Answers  


union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0

1 Answers   HCL,


typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

1 Answers  


Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

5 Answers   IITR, Microsoft, Nike,


#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }

2 Answers  


how to return a multiple value from a function?

2 Answers   Wipro,


Categories