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

3 Answers 3

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.)

Sign up to request clarification or add additional context in comments.

4 Comments

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
@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's a little tip to get you started for setOneMatrix. Good luck :)
@phuclv Interesting, I was only aware of 0xCDCDCDCD. Thanks :)
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.