Home > database >  How can I search a value in all lines of matrix?
How can I search a value in all lines of matrix?

Time:05-16

I want to find the value that I entered the first in array, in the matrix. So, for example my first value of array 5, I want to search 5 in matrix using function. But, if I enter the 5 value in the first line of matrix, the code is finding the 5, there aren't problem here. But if I enter the 5 value in second line or third... line my code can't find this value. Where am I doing the mistake in my code. I thought this is relevant "a" in the for loop but I couldn't find the problem there. Thank you.

#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;
}

CodePudding user response:

Unless the user enters 100 for the matrix column number, the matrix passed to the function does not have the expected dimensions.

You should modify the function prototype this way:

#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: ");
                scanf("%d", &matrix[i][k]);
            }
        }
    }
    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;
}
  •  Tags:  
  • c
  • Related