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
State the difference between x3 and x[3].
Do pointers take up memory?
What are the different types of control structures in programming?
What is getche() function?
Write a program to generate random numbers in c?
Is fortran faster than c?
Explain the difference between strcpy() and memcpy() function?
What are the loops in c?
Why does notstrcat(string, "!");Work?
What is the difference between %d and %i?
How can I remove the trailing spaces from a string?
What does calloc stand for?
can anyone suggest some site name..where i can get some good data structure puzzles???
What is union in c?
Differentiate between null and void pointers.