【问题标题】:How can I search a value in all lines of matrix?如何在矩阵的所有行中搜索一个值?
【发布时间】:2022-07-29 00:35:35
【问题描述】:

我想找到我在array 中输入的第一个值,在 matrix。所以,例如我的第一个值array5,我想搜索5matrix 中使用function。但是,如果我在第一个输入 5matrix 的行,代码找到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]

标签: c matrix


【解决方案1】:

除非用户为矩阵列号准确输入100,否则传递给函数的 VLA 矩阵不具有预期的维度。

你应该这样修改函数原型:

#include <stdio.h>

int functionmatrix1(int value, int lines, int columns, int mat[lines][columns]) {
    int count = 0;
    for (int a = 0; a < lines; a++) {
        for (int b = 0; b < columns; b++) {
            if (value == mat[a][b]) {
                printf("The value %d was found in [%d %d]\n", value, a, b);
                count++;
            }
        }
    }
    if (count == 0) {
        printf("Value %d in not in the matrix\n", value);
    } else {
        printf("Value %d was found %d times\n", value, count);
    }
    return count;
}

int main() {
    int matrixLines, matrixColumns;
    
    printf("Enter matrix line and column numbers with the queue:");
    if (scanf("%d %d", &matrixLines, &matrixColumns) != 2)
        return 1;

    int matrix[matrixLines][matrixColumns];
    
    for (int i = 0; i < matrixLines; i++) {
        for (int k = 0; k < matrixColumns; k++) {
            printf("Enter %d. line %d. column value of matrix:", i, k);
            if (scanf("%d", &matrix[i][k]) != 1)
                return 1;
            
            while (matrix[i][k] > 99 || matrix[i][k] < -99) {
                printf("The elements of matrix can be the most 2 digits, please enter new value: ");
                if (scanf("%d", &matrix[i][k]) != 1)
                    return 1;
            }
        }
    }
    int sizeofarray;
    printf("Enter the size of the array:");
    if (scanf("%d", &sizeofarray) != 1)
        return 1;
        
    int array1[sizeofarray];
        
    printf("Enter the array that will searched:");
    for (int s = 0; s < sizeofarray; s++) {
        printf("Enter the element %d of array: ", s);
        if (scanf("%d", &array1[s]) != 1)
            return 1;
    }
    for (int s = 0; s < sizeofarray; s++) {
        functionmatrix1(array1[s], matrixLines, matrixColumns, matrix);
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2015-10-21
    相关资源
    最近更新 更多