c语言中递归的二维数组
这是我写的一段代码,用c以螺旋形式打印矩阵。但在编译过程中,它开始向我显示一个错误。i、 基本上,我不知道如何正确地编写递归函数。有人能修改代码吗?请 这段代码中的错误是 解决方案c:23:1:错误:应为标识符或“{”标记之前的“(” 谁能告诉我这是什么意思c语言中递归的二维数组,c,recursion,C,Recursion,这是我写的一段代码,用c以螺旋形式打印矩阵。但在编译过程中,它开始向我显示一个错误。i、 基本上,我不知道如何正确地编写递归函数。有人能修改代码吗?请 这段代码中的错误是 解决方案c:23:1:错误:应为标识符或“{”标记之前的“(” 谁能告诉我这是什么意思 #include <stdio.h> matrix(int B[20][20],int M,int N); main() { int M,N,j,i=1; sca
#include <stdio.h>
matrix(int B[20][20],int M,int N);
main()
{
int M,N,j,i=1;
scanf("%d %d",&M,&N);
int B[M][N];
for(i=1; i<=M; i++)
{
for(j=1; j<=N; j++)
{
scanf("%d",&B[i][j]);
}
}
matrix(B[20][20],M,N);
return 0;
}
matrix(int B[20][20], int M,int N);
{
while(M!=0||N!=0)
{
for (i=1,j=1; j<=N; j++)
{
printf("%d ",(B[i][j]));
}
for(i=2,j=N; i<=M; i++)
{
printf("%d ",(B[i][j]));
}
for(i=M,j=N-1; j>0; j--)
{
printf("%d ",(B[i][j]));
}
for(i=M-1,j=1; i<=2; i--)
{
printf("%d ",(B[i][j]));
}
}
matrix(B[20][20],--M,--N);
}
#包括
矩阵(intb[20][20],intm,intn);
main()
{
int M,N,j,i=1;
scanf(“%d%d”,&M,&N);
int B[M][N];
对于(i=1;i当我们进入问题时,您的矩阵维度取决于用户的选择。因此,您将无法使用以下代码在堆栈中分配矩阵:
int B[N][M];
因为维度在编译时是未知的,所以需要使用malloc
使用malloc的代码:
#include <malloc.h>
#include <stdio.h>
void doSomething(int** matrix, int size0, int size1);
int main() {
int rows, cols, i, j;
int **matrix;
// scanf for row, cols
// allocate matrix (basically using malloc)
matrix = (int**)malloc(rows * sizeof(int*));
for (i = 0; i < rows; i++) {
matrix[i] = (int*)malloc(cols * sizeof(int));
}
// scanf for matrix values
// call recursive function
doSomething(matrix, rows, cols);
// free in reverse order
for (i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}
void doSomething(int** matrix, int size0, int size1) {
int i,j;
// stop condition
// some code here
}
#包括
#包括
无效剂量测量(整数**矩阵,整数尺寸0,整数尺寸1);
int main(){
int行,cols,i,j;
int**矩阵;
//行扫描,科尔斯
//分配矩阵(基本上使用malloc)
矩阵=(int**)malloc(行*sizeof(int*);
对于(i=0;i
我将尝试解决手头的问题,同时为您的问题提供递归解决方案
类型化:C是一种要求为所有变量和函数声明指定类型的语言。这会导致第一次编译错误
matrix(int B[20][20],int M,int N);
//should have been (though be careful there is another issue addressed later)
void matrix(int B[20][20],int M,int N);
以及
main() {
//shoud have been
int main() {
计数:作为程序员,你应该习惯于从0开始计数,因为大多数语言都是这样做的。大小为4的数组具有元素array[0]array[1]array[2]array[3]
。试图访问size-1
以上的地址将导致未定义的行为
因此,你的每一个循环都会给你带来意想不到的行为
for(i=1; i <= M; i++)
//should have been (as an example)
for(i=0; i < M; i++)
数组作为参数II:如果你想让函数将数组作为参数,你必须以一种特定的方式声明函数头。这很违反直觉。你可以阅读更多关于该主题的内容。但是最后
void matrix(int B[20][20],int M,int N);
//should have been
void matrix(int M,int N,int B[][N]);
范围:局部变量只有一个
void matrix(int B[20][20], int M,int N) {
while(M!=0||N!=0) {
for (i=1,j=1; j<=N; j++)
{ /*....*/
while循环:只要循环头的计算结果为false,while循环就会结束
while(M!=0||N!=0)
{
/*stuff*/
}
因此会导致一个无休止的循环,因为身体中的M
和N
都没有改变,或者根本就不会执行
递归:您确实编写了一个(理论上)递归函数,但没有任何附加规则。递归只不过是一个循环,因此它会一直进行,直到发出附加/中断。基本上,您必须有类似
if(/*aboard_condition*/) return;
以确保递归将在某个点结束
分号:解决问题的一部分
解决方案c:23:1:错误:应为标识符或“{”标记之前的“(”
你刚才用了一个分号来表示:
matrix(int B[20][20],int M,int N);
{ ....
//should have been (though do not forget about the other issues)
matrix(int B[20][20],int M,int N)
{
以分号分隔函数头和函数块
由于您的print函数已被完全破坏,我使用了从中借用的print_-sprial函数,并通过将变量m
和n
从正文中取出并放入标题将其转换为递归函数。我进一步将print函数移到主函数前面,以避免必须给出标题声明
#include <stdio.h>
void print_matrix(int matrix_y, int matrix_x, int a[matrix_y][matrix_x],int k,int l)
{
int i;
int m = matrix_y;
int n = matrix_x;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;
/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}
int main()
{
int M,N,j,i=1;
scanf("%d %d",&M,&N);
int B[M][N];
for(i=0; i != M; i++)
{
for(j=0; j != N; j++)
{
printf("%d/%d:",i,j);
scanf("%d",&B[i][j]);
}
}
print_matrix(M,N,B,0,0);
return 0;
}
#包括
无效打印矩阵(整数矩阵y,整数矩阵x,整数a[矩阵y][矩阵x],整数k,整数l)
{
int i;
int m=矩阵_y;
int n=矩阵x;
/*k-起始行索引
m-结束行索引
l-起始列索引
n-结束列索引
i-迭代器
*/
而(k=l;--i)
{
printf(“%d”,a[m-1][i]);
}
m--;
}
/*打印剩余列中的第一列*/
if(l=k;--i)
{
printf(“%d”,a[i][l]);
}
l++;
}
}
}
int main()
{
int M,N,j,i=1;
scanf(“%d%d”,&M,&N);
int B[M][N];
for(i=0;i!=M;i++)
{
对于(j=0;j!=N;j++)
{
printf(“%d/%d:”,i,j);
scanf(“%d”、&B[i][j]);
}
}
打印矩阵(M,N,B,0,0);
返回0;
}
更好的解决方案
#包括
#包括
int i=0,j=0,k=0,flag=0;
int函数_矩阵(int arr[][4],int i,int j)
{
如果(j==4 | i==3)
返回1;
如果(标志==0)
{
printf(“\t%d”,arr[i][j]);
}
函数_矩阵(arr,i,j+=1);
printf(“\n”);
函数_矩阵(arr,i+=1,j=0);
flag=1;
}
int main()
{
int x;
int arr[][4]={{1,2,3,4},
{5,6,7,8},
{9,7,6,5}};
函数_矩阵(arr,0,0);
}
流动
这意味着要做什么?你得到了什么错误?解决方案。c:35:8:错误:未知类型名称“B”矩阵(B[20][20],M,N);^解决方案。c:35:18:错误:未知类型名称“M”矩阵(B[20][20],M,N);^解决方案。
matrix(int B[20][20],int M,int N);
{ ....
//should have been (though do not forget about the other issues)
matrix(int B[20][20],int M,int N)
{
#include <stdio.h>
void print_matrix(int matrix_y, int matrix_x, int a[matrix_y][matrix_x],int k,int l)
{
int i;
int m = matrix_y;
int n = matrix_x;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;
/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}
int main()
{
int M,N,j,i=1;
scanf("%d %d",&M,&N);
int B[M][N];
for(i=0; i != M; i++)
{
for(j=0; j != N; j++)
{
printf("%d/%d:",i,j);
scanf("%d",&B[i][j]);
}
}
print_matrix(M,N,B,0,0);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int i=0,j=0,k=0,flag=0;
int function_Matrix(int arr[][4],int i,int j)
{
if(j==4||i==3)
return 1;
if(flag==0)
{
printf("\t%d",arr[i][j]);
}
function_Matrix(arr,i,j+=1);
printf("\n");
function_Matrix(arr,i+=1,j=0);
flag=1;
}
int main()
{
int x;
int arr[][4]={{1,2,3,4},
{5,6,7,8},
{9,7,6,5}};
function_Matrix(arr,0,0);
}