how to display 2-D array elements in spiral
Answers were Sorted based on User's Feedback
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 |
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 |
can i know the source code for reversing a linked list with out using a temporary variable?
struct screen_pos{ int row, col } ;move_right(cursor)struct screen_pos *cursor;{ cursor.col++; } /* This statementhas a syntax error */What is the correct statement a) cursor.col = cursor.col + 1; b) col.cursor++; c) *cursor.col++; d) pointer
explain memory layout of a C program
Write a program for print infinite numbers
write a c program in such a way that if we enter the today date the output should be next day's date.
#define MAX 3 main() { printf("MAX = %d ",MAX ); #undef MAX #ifdef MAX printf("Vector Institute”); #endif }
What is information technology.
Why is void main used?
What are the properties of union in c?
Why is malloc used?
Explain what is the purpose of "extern" keyword in a function declaration?
1. Write a c pgm to print 1 to 100 without using loops. 2. Write a c pgm for leap year 3. Write a c pgm fibbonacci series,factorial 4. Write a c pgm count no of lines , blanks, tabs in a para(File concept) 5. Write a c pgm to print the letter as per given condition i.e.. if u give 4 out put should b 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6.how do get the o/p in number from 1 to 100 in the screen without using control statement? 7. who do u print the word "hello world" without using "printf" statement? 8. write sql program to get the detail of student in a class? Definitions: structure union arrays linkedlist macros directives difference b/w pre processorsDiffrence: 1.Constructors and destructors 2.Structure and Union 3.Array and Lists 4.pre processor... 5. Privillages in C++ 6.structure and union 7.break and continue 8.while and dowhile Pgm..