how to display 2-D array elements in spiral

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between c and python?

586


Why do we use int main instead of void main in c?

621


Do pointers take up memory?

658


What does emoji p mean?

603


What is output redirection?

692






How do you list files in a directory?

563


Differentiate between null and void pointers.

633


What are structural members?

574


hi folks i m approching for h1 b interview on monday 8th of august at montreal and i m having little problem in my approval notice abt my bithdate my employer has made a mistake while applying it is 12th january and istead of that he had done 18 the of january do any body have any solution for that if yes how can i prove my visa officer abt my real birthdate it urgent please let me know guys thaks dipesh patel

1413


Why doesnt that code work?

601


Should I learn data structures in c or python?

583


What are the string functions? List some string functions available in c.

605


I completed my B.tech (IT). Actually I want to develop virtual object that which will change software technology in the future. To develop virtual object what course I have to take. can I any professor to help me.

1739


Explain what is the advantage of a random access file?

666


What will the preprocessor do for a program?

592