Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 期望:代码打印出x(您键入的)在矩阵中出现的次数。。。实际值:代码打印出值的数量_C++_Matrix - Fatal编程技术网

C++ 期望:代码打印出x(您键入的)在矩阵中出现的次数。。。实际值:代码打印出值的数量

C++ 期望:代码打印出x(您键入的)在矩阵中出现的次数。。。实际值:代码打印出值的数量,c++,matrix,C++,Matrix,期望:代码打印出x(您键入的)在矩阵中出现的次数。。。实际值:代码打印出值的数量 嘿,伙计们,你们中有些人可能是白痴,但我刚刚开始学习C++,所以请同情我((我为我的英语不好)道歉。 #include <iostream> using namespace std; int NhapMang(int A[100][100], int &n) { for(int i=0; i<n; i++) { for(int j=0; j<n; j

期望:代码打印出x(您键入的)在矩阵中出现的次数。。。实际值:代码打印出值的数量 嘿,伙计们,你们中有些人可能是白痴,但我刚刚开始学习C++,所以请同情我((我为我的英语不好)道歉。
#include <iostream>
using namespace std;

int NhapMang(int A[100][100], int &n)
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
            {
                cout << "Nhap A[" << i << "][" << j << "]: ";
                cin >> A[i][j];
            }
    }
    return 0;
}
int XuatMang(int A[100][100], int &n)
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}
int SoLanXuatHien(int A[100][100], int &n, int &x)
{
    int dem=0;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(A[i][j]==x);
            {
                dem++;
            }
        }
    }
    return dem;
}

int main()
{
    int n, A[100][100],x;
    cout << "moi nhap n: ";
    cin >> n;
    NhapMang(A,n);
    XuatMang(A,n);
    cout << "moi nhap x: ";
    cin >> x;
    cout << "\nso lan xuat hien: \n";
    cout << SoLanXuatHien(A,n,x);
    return 0;
}
#包括
使用名称空间std;
尼亚普芒国际机场(国际机场[100][100],国际和北)
{
对于(inti=0;i而言,问题在于这一行

if(A[i][j]==x);
在C++中,即使是简单的分号也被当作一个语句来处理,实际上它等价于:

if (A[i][j] == x)
    ;
....
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if(A[i][j]==x) {

        }

        {
            dem++; // dem will be incremented every time loop iterates. That's why you got 9
        }
    }
}
在您的情况下,因为您没有在语句周围加大括号,所以您的代码在某种程度上等同于以下内容:

if (A[i][j] == x)
    ;
....
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if(A[i][j]==x) {

        }

        {
            dem++; // dem will be incremented every time loop iterates. That's why you got 9
        }
    }
}
。。。。
对于(int i=0;i
去掉分号,一切都会好的

更正代码:

#include <iostream>
using namespace std;

int NhapMang(int A[100][100], int &n)
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
            {
                cout << "Nhap A[" << i << "][" << j << "]: ";
                cin >> A[i][j];
            }
    }
    return 0;
}
int XuatMang(int A[100][100], int &n)
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}
int SoLanXuatHien(int A[100][100], int &n, int &x)
{
    int dem=0;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(A[i][j]==x)
            {
                dem++;
            }
        }
    }
    return dem;
}

int main()
{
    int n, A[100][100],x;
    cout << "moi nhap n: ";
    cin >> n;
    NhapMang(A,n);
    XuatMang(A,n);
    cout << "moi nhap x: ";
    cin >> x;
    cout << "\nso lan xuat hien: \n";
    cout << SoLanXuatHien(A,n,x);
    return 0;
}
#包括
使用名称空间std;
尼亚普芒国际机场(国际机场[100][100],国际和北)
{

对于(inti=0;iTBH),我不想麻烦粘贴OP的100行代码,它不会添加任何内容fine@Tas根据你的建议,我添加了一些解释。谢谢。