how to display 2-D array elements in spiral

Answers were Sorted based on User's Feedback



how to display 2-D array elements in spiral..

Answer / om

#include<iostream>

using namespace std;
//----------------------
void spiral_way(int Row_size,int Column_size,int a[][4])
{
int
i,j,d=0,Current_row_size=Row_size,Current_column_size=Column_size,
Counter=Row_size*Column_size;



while(Counter>0) //Initailly my "Counter" is set to
total no. of elemnet in my 2-D array. so now I will
decrement it as I cover a element.

{

i=d;j=d;

while(j<Current_column_size-1) {
printf("%d\t",a[i][j]); j++; Counter--;}
//this is for printing the first row in forward direction.

while(i<Current_row_size-1) {
printf("%d\t",a[i][j]); i++; Counter--;}
//this is for printing the last cloumn in downward direction.

while(j>d) {
printf("%d\t",a[i][j]); j--; Counter--; }
//this is for printing the last row in backward direction.

while(i>d) {
printf("%d\t",a[i][j]); i--; Counter--; }
//this is for printing the first column in upward direction.

//When I completed the outer rectangle I move in to inner
rectangle by incrementing "d".

//and decrementing the "Current_row_size" and
"Current_column_size".
d++;
Current_row_size--;
Current_column_size--;

}
}

//-------------------------

int main()

{

int a[5][4]={{1,2,3,4},

{5,6,7,8},

{9,10,11,12},

{13,14,15,16},

{17,18,19,20}

};

spiral_way(5,4,a);

//system("pause");

return 0;

}

Is This Answer Correct ?    2 Yes 0 No

how to display 2-D array elements in spiral..

Answer / raj

#define subscript 5


void spiral()
{
int a[subscript][subscript],i,j,k,l,m,p,q;
p=q=subscript ;
for(i=0;i<p;i++,p--)
{
for(j=i;j<n-1;j++)
printf("%d",a[i][j]);
for(k=i;k<j;k++)
printf("%d",a[k][j]);
for(l=k;l>i;l--)
printf("%d",a[l][k]);
for(m=k;m>i;m--)
printf("%d",a[i][m]);
}
if(q%2!=0)
printf("%d",a[j][j]);
}

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More C Interview Questions

Explain what is the concatenation operator?

0 Answers  


How do you write a program which produces its own source code as output?

0 Answers  


What is int main () in c?

0 Answers  


Why cann't whole array can be passed to function as value.

1 Answers  


1. Write a program to reverse every second word in a given sentence.

1 Answers  






Explain what are binary trees?

0 Answers  


what is the most appropriate way to write a multi-statement macro?

1 Answers  


code for quick sort?

0 Answers  


What are the disadvantages of c language?

0 Answers  


How to removing white spces in c programming only bu using loops

2 Answers  


what are advantages of U D F?

1 Answers   Google,


3.write a simple program that will output your name,phone number,e-mail address,and academic major on separate lines 1.create an account and a personal directory for your work b.find out how to create a subdirectory on your system.create one called info c.you will use a text editor to type in your programs and data files.some C systems have a built in text editor;others do not.Find out what text editor you will be using and how to access it.create a text file(not a program) containing your name ,address,and telephone number on separate lines.Next,write the brand of computer you are using and the name of the text editor.Then write a paragraph that describes your past experience with computers.save this file in your info directory. d. find out how to print a file on your system .print out and turn in the file you created in (c).

0 Answers   TCS,


Categories