S
S
Steve2019-04-09 16:49:42
C++ / C#
Steve, 2019-04-09 16:49:42

How to count the number of negative elements in a matrix?

I want to display the number of negative numbers of each column in a separate array, but I don't understand what is wrong

int row, column;
    printf("Enter number of rows: ");
    scanf("%d", &row);
    printf("Enter number of column: ");
    int **matrix = (int**)malloc(row * sizeof(int));
    scanf("%d", &column);
    int *arr = (int*)(malloc(sizeof(int) * column));
    printf("Enter value of k: ");
    scanf("%d", &k);
    for (int i = 0; i < row; ++i)
    {
        matrix[i] = (int*)malloc(column * sizeof(int));
    }
    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < column; ++j)
        {
            printf("Element matrix[%d][%d] = ", i+1, j+1);
            scanf("%d", &matrix[i][j]);

        }
    }
    printf("Your matrix: \n");
    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < column; ++j)
        {
            printf("[%d]\t", matrix[i][j]);
        }
        printf("\n\n");
    }

    for (int i = 0; i < column; ++i)
    {
        for (int j = 0; i < row; ++j)
        {
            if(matrix[i][j] < 0)
                arr[j]++;
        }
    }

    for (int i = 0; i < column; ++i)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ronald McDonald, 2019-04-09
Gorton

for (int i = 0; i < column; ++i)
    {
        for (int j = 0; i < row; ++j)
        {
            if(matrix[i][j] < 0)
                arr[j]++;
        }
    }

Isn't it
:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question