Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++ C++;数组值_C++_Arrays_Matrix - Fatal编程技术网

C++ C++;数组值

C++ C++;数组值,c++,arrays,matrix,C++,Arrays,Matrix,我想在程序结束时算出A,B和C的值。当我尝试使用for循环时,我得到了一个完整的值列表。我只需要一个矩阵格式 #include <iostream> using namespace std; int main() { int m = 3; int A[m] [m] = {}; int B[m] [m + 1] = {}; int C[m + 1] [m] = {}; int counter = 0, k = 2; for

我想在程序结束时算出A,B和C的值。当我尝试使用for循环时,我得到了一个完整的值列表。我只需要一个矩阵格式

#include <iostream>
using namespace std;

int main()
{
    int m = 3;
    int A[m] [m] = {};
    int B[m] [m + 1] = {};
    int C[m + 1] [m] = {};

    int counter = 0, k = 2;
    
    for (int i=1; i<=m; i++)
    {
        for (int j=1; j<=m; j++) 
        {
            A[i-1][i-1] = counter;
            B[i][j] = counter;
            C[j-1][j-1] = k;
        }
        counter++;
        k += 3;
    }

    //Printing C
    //get array columns and rows
    int rows =  sizeof C/ sizeof A[0]; 
    int cols = sizeof C[0] / sizeof(int); 
      
    // Print 2d Array
    cout << "your_array data "<<endl<<endl;
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            std::cout << C[i][j] << std::endl;
        }
    }

    return 0;
}
#包括
使用名称空间std;
int main()
{
int m=3;
int A[m][m]={};
int B[m][m+1]={};
int C[m+1][m]={};
int计数器=0,k=2;
对于(inti=1;i`


for(i=0;i您的
A
C

int A[3][3] = {};
int C[4][3] = {};
然后你用

int rows =  sizeof C/ sizeof A[0]; 
int cols = sizeof C[0] / sizeof(int); 
要知道它的尺寸,那是

int rows = 3; 
int cols = 4;
然后在循环中

for (int i = 0; i < rows; ++i)
{
    for (int j = 0; j < cols; ++j)
    {
        std::cout << C[i][j] << std::endl;
    }
}
还请注意,这不是标准C++

int m = 3;
int A[m] [m] = {};
int B[m] [m + 1] = {};
int C[m + 1] [m] = {};

阅读更多信息。

您发布的代码中没有
cout
。请在问题
int a[m][m]={}中包含有问题的代码的详细信息以及实际和预期输出“< /代码>不是标准C++,而是编译器扩展。使用<代码> STD::向量< /代码>可变大小数组。它应该是代码> const int m=3;< /Calp> @ jHBoURIUS实际上大小是常数<代码> 3代码/代码>代码4 >代码>不需要<代码> STD::向量< /代码>,它可以被认为是TyPOYU必须做一些工作。LF来制作矩阵。C++不是Matlab或Python。你可以使用像EGIN或Boost UBLAS.you这样的库来解释你的答案,而不仅仅是为他做作业。
for (int i = 0; i < rows; ++i)
{
    for (int j = 0; j < cols; ++j)
    {
        std::cout << C[i][j] << " ";
    }
    std::cout << "\n";
}
int m = 3;
int A[m] [m] = {};
int B[m] [m + 1] = {};
int C[m + 1] [m] = {};