螺旋矩阵c语言用二维数组,二维数组c中的螺旋矩阵

我正在尝试编写二维数组中的sipral矩阵的c程序。其输出应该如下。我已经完成了矩阵输出的编码,但是不能完成螺旋矩阵的输出。我无法找到我的程序中的错误。请帮助二维数组c中的螺旋矩阵

输出

在给定的矩阵:

3 6 3 8

4 7 1 9

3 8 2 7

5 2 9 8

用螺旋输出的导线:

3 6 3 8 9 7 8 9 2 5 3 4 7 1 2 8

代码

#include

int main()

{

int row = 0, //top most row

b =row-1, // bottom row

col= 0, // left most column

r = col-1, // right column

size = 0,

matrix[10][10],

i;

int dir;

printf("\n Size of Matrix: "); // enter the size of Matrix//

scanf("%d", &size);

printf("\n Enter Matrix Elements: \n"); // Enter Matrix Element//

for(row=0;row

{

for(col=0;col

{

printf(" Element [%d][%d] :", row,col); // print output of element. say Element[1][0]= //

scanf("%d" , &matrix[row][col]); // input matrix element //

}

printf("\n");

}

printf("\n the given Matrix is :\n"); // print output of given Matrix //

for(row=0;row

{

for(col=0;col

{

printf("%4d",matrix[row][col]); //output of matrix//

}

printf("\n");

}

printf("output of helical traverse matrix is: \n");

while(col<=r && row<=b) //condition exist if left column<= right column && Topmost row<= bottom row.I set four direction. dir =0 for left to right , dir=1 for top to bottom, dir 2 for right to left, dir 4 for bottom to top.//

{

if(dir==0){

for(i=col;i<=r;i++){

printf("%4d",matrix[row][i]); //print out 3 6 3 8//

}

row++; //increase top row .so now 2 nd row become the top most row//

}

else if(dir==1){

for(i=row;i<=b;i++){ // looping for print output 9 7 8

printf("%4d",matrix[i][col-1]);

}

r--; //decrease left column. so now 2nd column from the right becomes the right most column//

}

else if(dir==2)

{

for(i=r;i>=col;i--){ //looping for print output of 9 2 5//

printf("%4d",matrix[b][i]);

}

b--; //decerase bottom row. so now 2 nd row from bottom becomes the bottom row.//

}

else if(dir==3)

{

for(i=b;i>=row;i--){ //looping for print out 3 4//

printf("%4d",matrix[i][col]);

}

col++; //icrease left column. so now 2nd column from left becomes left most column//

}

dir=(dir+1)%4;

}

return 0;

}

+0

ROW和COL总是相等的? –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值