0

I am writing this simple code for setting a matrix ourselves and displaying it. When I execute this program, it gives garbage value on the first row. How is that? Any errors in my program ?

#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;
}
}

Output:

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
SRT
  • 13
  • 5

3 Answers3

3

Sorry to say that, but your whole program has undefined behavior! This is my output:

-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

You are just lucky that the numbers get printed, except for the first row. That shouldn't happen, see my output.


Some facts:

  • setMat in main contains just 0.
  • setOneMatrix doesn't initialize setMat in main, it just initializes another setMat. This function basically does nothing.
  • displayMath is a bit better, because you are passing setMat to it, but then the function does even use setMat, it just creates another array, mat and prints that array out. That array isn't initialized, so it contains garbage values.

    So when you print it, you can get anything!

    (It also doesn't return an int, so the program is already ill-formed, why should it? Just make it return void.)

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • can you write me a simple program in C++ to do this operation initialize the matrix of dimension 6 x 5 and make a function to display it thank you – SRT Jul 10 '16 at 04:02
  • @user5941106 I could do it, but then you wouldn't learn anything :) And I am absolutely sure you can do it, based on the code you posted. There are just some minor mistakes, and I'm sure you can handle them :) [Here](http://stackoverflow.com/questions/2765999/what-is-a-reference-variable-in-c)'s a little tip to get you started for `setOneMatrix`. Good luck :) – Rakete1111 Jul 10 '16 at 04:04
  • 1
    [MSVC fills uninitialized memory with 0xCC to aid debugging](https://stackoverflow.com/q/370195/995714), and -858993460 = 0xCCCCCCCC – phuclv Aug 18 '18 at 11:12
  • @phuclv Interesting, I was only aware of 0xCDCDCDCD. Thanks :) – Rakete1111 Aug 18 '18 at 13:40
1

There are somethings wrong in your code.
1. The function "Display" must return a value. You just miss it. And instead of returning a value, just changing it into "void" function.
2. You redefine the matrix "setMat" in the function setOneMatrix(). It means the matrix "setMat" in the main() and the matrix "setMat" in the function setOneMatrix() are different about their address in the computer's memory. When you try to print out the matrix, you just print the matrix in the main(). And the value of its element are garbage.

lampv
  • 47
  • 4
0

Here, i corrected your code

#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;
    }
}
SloCompTech
  • 116
  • 1
  • 7