【问题标题】:C++ Multidimensional array program shows garbage value in outputC ++多维数组程序在输出中显示垃圾值
【发布时间】:2016-07-10 03:25:26
【问题描述】:

我正在编写这个简单的代码来自己设置矩阵并显示它。当我执行这个程序时,它会在第一行给出垃圾值。那个怎么样?我的程序有什么错误吗?

#include<iostream>
using namespace std;

void setOneMatrix();
//void getOneMatrix(int mat[6][5]);
int display(int mat[6][5]);

int main() {

int setMat[6][5]={};
setOneMatrix();
display(setMat);


}

void setOneMatrix() {
/*int setMat[6][5] = {1,2,3,4,5,
                    6,7,8,9,10,
                    11,12,13,14,15,
                    16,17,18,19,20,
                    21,22,23,24,25,
                    26,27,28,29,30};*/

int setMat[6][5] = {{1,2,3,4,5},
                    {6,7,8,9,10},
                    {11,12,13,14,15},
                    {16,17,18,19,20},
                    {21,22,23,24,25},
                    {26,27,28,29,30}};

}

int display(int mat[6][5]) {
int i,j,setMat[6][5];
for(i=0;i<6;i++){
    for(j=0;j<5;j++) {
        cout << setMat[i][j] << "\t";
    }
    cout << endl;
}
}

输出:

4665744 4687848 6946296 4257625 0
1   2   3   4   5
6   7   8   9   10
11  12  13  14  15
16  17  18  19  20
21  22  23  24  25

【问题讨论】:

    标签: c++ arrays multidimensional-array garbage


    【解决方案1】:

    很抱歉,您的整个程序都有未定义的行为!这是我的输出:

    -858993460      -858993460      -858993460      -858993460      -858993460
    -858993460      -858993460      -858993460      -858993460      -858993460
    -858993460      -858993460      -858993460      -858993460      -858993460
    -858993460      -858993460      -858993460      -858993460      -858993460
    -858993460      -858993460      -858993460      -858993460      -858993460
    -858993460      -858993460      -858993460      -858993460      -858993460
    

    你很幸运,除了第一行之外的数字都被打印出来了。那不应该发生,请参阅我的输出。


    一些事实:

    • setMatmain 中只包含0
    • setOneMatrix 不会在main 中初始化setMat,它只是初始化另一个setMat。这个函数基本上什么都不做。
    • displayMath 好一点,因为您将setMat 传递给它,但随后该函数甚至使用 setMat,它只是创建另一个数组mat 并打印那个数组出来。该数组未初始化,因此它包含垃圾值。

      所以当你打印出来的时候,你可以得到任何东西

      (它也不返回int,所以程序已经不正确了,为什么要它?让它返回void。)

    【讨论】:

    • 你能给我写一个简单的 C++ 程序来做这个操作吗初始化 6 x 5 维矩阵并制作一个函数来显示它谢谢
    • @user5941106 我可以做到,但是你不会学到任何东西 :) 根据你发布的代码,我绝对相信你可以做到。只是有一些小错误,我相信你可以处理它们 :) Here 的小技巧让你开始使用 setOneMatrix。祝你好运:)
    • @phuclv 有趣的是,我只知道 0xCDCDCDCD。谢谢:)
    【解决方案2】:

    您的代码有问题。
    1.“显示”函数必须返回一个值。你只是想念它。而不是返回一个值,只需将其更改为“void”函数。
    2. 在函数 setOneMatrix() 中重新定义矩阵“setMat”。这意味着 main() 中的矩阵“setMat”和函数 setOneMatrix() 中的矩阵“setMat”在计算机内存中的地址不同。当您尝试打印出矩阵时,您只需在 main() 中打印矩阵。而且它的元素的值是垃圾。

    【讨论】:

      【解决方案3】:

      这里, 我更正了你的代码

      #include<iostream>
      using namespace std;
      
      
      int mat[6][5]; //Global variable, you can access this anywhere in your program
      
      void init_mat();
      void setOneMatrix();
      void display(int mat[6][5]);
      
      int main() {
          init_mat(); /* Init mat array */
          setOneMatrix();
      
          display(mat);//Pass array that is global as argument
      
      
      }
      
      void init_mat()
      {
          //Give fields in array default value
          //Easyer to do field by field
          for (int i = 0; i < 6; i++)
              for (int j = 0; j < 5; j++)
                  mat[i][j] = 0;
      }
      void setOneMatrix() {
          /*int setMat[6][5] = { { 1,2,3,4,5 },
          { 6,7,8,9,10 },
          { 11,12,13,14,15 },
          { 16,17,18,19,20 },
          { 21,22,23,24,25 },
          { 26,27,28,29,30 } };*/
      
          //Now you need to set value of fields field by field, 
          for (int i = 0; i < 6; i++)
              for (int j = 0; j < 5; j++)
                  mat[i][j] = 1+i*5+j;
      }
      
      void display(int pMat[6][5]) {
          int i, j;
          for (i = 0; i<6; i++) {
              for (j = 0; j<5; j++) {
                  cout << pMat[i][j] << "\t";
              }
              cout << endl;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 2013-05-07
        • 2020-02-03
        • 1970-01-01
        • 2010-09-20
        相关资源
        最近更新 更多