Answer the question
In order to leave comments, you need to log in
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
for (int i = 0; i < column; ++i)
{
for (int j = 0; i < row; ++j)
{
if(matrix[i][j] < 0)
arr[j]++;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question