Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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..

#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 10 No

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

#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..

#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;
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

More C Code Interview Questions

What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

C program to print magic square of order n where n > 3 and n is odd

main() { char a="HELLO"; printf("%s",a); }

#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }

Give a oneline C expression to test whether a number is a power of 2?

What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

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.

main( ) { int a = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); }

source code for delete data in array for c

write a c program to Create employee record by taking details like name, employee id, address and phone number. While taking the phone number, take either landline or mobile number. Ensure that the phone numbers of the employee are unique. Also display all the details

Categories
• C Code (423)
• C++ Code (128)
• VC++ Code (17)
• Java Code (84)
• Dot Net Code (114)
• Visual Basic Code (11)
• Programming Code AllOther (62)