【发布时间】:2022-07-29 00:35:35
【问题描述】:
我想找到我在array 中输入的第一个值,在
matrix。所以,例如我的第一个值array5,我想搜索5
在matrix 中使用function。但是,如果我在第一个输入 5 值
matrix 的行,代码找到5,这里没有问题。
但是如果我在第二行或第三行输入5 值... 行我的代码
找不到这个值。我在哪里做我的代码中的错误。我
认为这是for loop 中的相关“a”,但我找不到
那里的问题。谢谢。
#include <stdio.h>
#include <stdlib.h>
void functionmatrix1(int startingvalue1,
int thematrixthatwillthrowtofunction1[][100],
int linevalue1, int columnvalue1)
{
int a = 0, b = 0;
int counter1 = 0;
for (a = 0; a < linevalue1; a++) {
for (b = 0; b < columnvalue1; b++) {
if (startingvalue1 == thematrixthatwillthrowtofunction1[a][b]) {
printf("The array was found in [%d %d] \n", a, b);
counter1++;
}
}
}
if (counter1 == 0) {
printf("There aren't in matrix'");
}
printf("%d", counter1);
printf("%d", a);
}
int main() {
int matrixLine, matrixColumn;
int i, k, s;
printf("Enter matrix line and column with the queue:");
scanf("%d %d", &matrixLine, &matrixColumn);
int matrix[matrixLine][matrixColumn];
for (i = 0; i < matrixLine; i++) {
for (k = 0; k < matrixColumn; k++) {
printf("Enter %d. line %d. column value of matrix:", i, k);
scanf("%d", &matrix[i][k]);
while (matrix[i][k] > 99 || matrix[i][k] < -99) {
printf("The elements of matrix can be the most 2 digits, please enter new value :");
scanf("%d", &matrix[i][k]);
}
}
}
int sizeofarray;
printf("Enter the size of the array:");
scanf("%d", &sizeofarray);
int sizeofarray1[sizeofarray];
printf("Enter the array that will searched:");
for (s = 0; s < sizeofarray; s++) {
printf("Enter the %d. element of array:", s + 1);
scanf("%d", &sizeofarray1[s]);
}
functionmatrix1(sizeofarray1[0], matrix, matrixLine, matrixColumn);
return 0;
}
【问题讨论】:
-
函数参数
int thematrixthatwillthrowtofunction1[][100]只有在调用者的数组也是宽度100时才会起作用。切换参数,所以这是最后一个,并将其设为int thematrixthatwillthrowtofunction1[][columnvalue1]