i have written a program which should print the matrix in the following fashion.
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
code is :
Code:
#include <stdio.h>
main()
{
int a[10][10];
int i, j;
int r, c;
int count = 0, loop;
printf("enter the order of square matrix\n");
scanf("%d%d", &r, &c);
if(r == c) {
for(loop = 0; loop < (r + 1)/ 2; loop++) { //for circular loop
for(i = loop; i < (c - loop); i++) { //row loop th row
if(count < r * r) //condition for to come out of loop
a[loop][i] = ++count; //assigning the counter value
}
for(i = loop + 1; i < (r - loop); i++) { //for n - loop th row
if(count < r * r) //same as above
a[i][r - loop - 1] = ++count;
}
for(i = (c - loop - 2); i > loop; i--) {
if(count < r * r)
a[c - loop - 1][i] = ++count;
}
for(i = (r - loop - 1); i > loop; i--) {
if(count < r * r)
a[i][loop] = ++count;
}
if(count == r * r)
break;
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
else
printf("invalid order for computation\n");
}
but with this code every thing is going fine till the matrix order 10 * 10. hence forth it is showing some garbage values in the matrix... some one please tell me what changes i have to make to make this program generic.....