简体   繁体   English

判断一个矩形矩阵是否有两行正元素

[英]Determine if a rectangular matrix has two rows of positive elements

I need to determine if a rectangular matrix has two rows of positive elements in C.我需要确定一个矩形矩阵在 C 中是否有两行正元素。 I write part code for the set matrix and output its elements.我为集合矩阵和 output 它的元素编写部分代码。 I don't know how to check the positive elements in the row.我不知道如何检查行中的积极元素。 Please help me请帮我

#include <stdio.h>
#include <conio.h>
#include <locale.h>

#define M 3
#define N 4

int main() {
    setlocale(LC_ALL, "Rus");
    float a[M][N]; //set matrix with 3 row and 4 column
    int i, j;     // row and column index
    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++)
            scanf_s("%f", &a[i][j]);
    }
    for (i = 0; i < M; i++) {
        printf("%d line:", i + 1);
        for (j = 0; j < N; j++)
            printf("%f", a[i][j]);
        printf("\n");
    }
    _getch();
    return 0;
}

So, I make some changes in my code after reading comments.因此,我在阅读评论后对代码进行了一些更改。 Thanks a lot.非常感谢。 But it's not working.但它不起作用。

#include <stdio.h>
#include <conio.h>
#include <locale.h>

#define M 3
#define N 4

int main(){
    setlocale(LC_ALL, "Rus");
    float a[M][N]; //обьявление матрицы 3 строки и 4 столбца
    int i, j;     // индексы строки и столбца
    int count;
    for (i = 0; i < M; i++){
        for (j = 0; j < N; j++)
            scanf_s("%f", &a[i][j]);
    }
    for (i = 0; i < M; i++){
        printf("%d-я строка:", i + 1);
        for (j = 0; j < N; j++)
            printf("%f", a[i][j]);
        printf("\n");
    }
    count = 0;
    for (i = 0; i < M; i++){
        for (j = 0; j < N; j++)
            if (a[i][j] > 0){
                count++;
                printf("%d", count);
        }
    }
    _getch();
    return 0;
}

What you need to do is to set a counter to 0. Every time all the values are positive in row add 1 your counter.您需要做的是将计数器设置为 0。每次行中的所有值都是正数时,您的计数器加 1。 You can do that by a loop.你可以通过循环来做到这一点。 If a value of your matrix a[i][j] is less than 0 just continue and move on to then next row.如果矩阵 a[i][j] 的值小于 0,则继续并移至下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM