1

The user input a matrix, and the output must be a new matrix with an additional column of zeros. If we apply the script to a 2 square matrix like : {1,2,3,4} the new matrix output will be a 2 rows & 3 columns : {1,2,32,3,4,0}. I don't understand the number 32 output.

#include <iostream>

int main(){

    int m,n;

    std::cout << "Input the size of the square matrix :  ";
    std::cin >> m;
    n=m;

    int A[m][n]={};
    int M[m][n+1]={0};

    for (int i(0);i<m;i++){
        for(int j(0);j<n;j++){
            std::cout << "Input element A["<<i<<"]["<<j<<"] : ";
            std::cin >> A[i][j];
            M[i][j]=A[i][j];                    
        }
    }
    for (int i(0);i<m;i++){
        for(int j(0);j<=n;j++){
            std::cout << M[i][j] << " ";
        }
        std::cout << "\n";
    }

    return 0;
}
4
  • 1
    You know, I'm not sure how Variable Length Arrays handle initialization. It's kind of Wild West territory because Variable Length Arrays are not part of C++. Some compilers have added support to make C++ a bit more like C, but you'd have to check your compiler's documentation to see how it handles int M[m][n+1]={0}; Commented Jun 5, 2020 at 17:00
  • 2
    Since the goal of VLAs in C++ its to be the same as VLAs in C, this C question may be relevant: Initializing a dynamically-sized Variable-Length Array (VLA) to 0 Commented Jun 5, 2020 at 17:07
  • 1
    Poking around at g++, it looks like the request for initialization is mostly ignored. Only the first element is being set. Commented Jun 5, 2020 at 17:15
  • Side note: A simple, fast, and safe dynamically-sized matrix class. As an added bonus it also automatically initializes to all zeroes. Commented Jun 5, 2020 at 17:40

2 Answers 2

2

Variable length arrays (VLAs) are a non-portable gcc extension and evidently don't initialise as you would expect.

One solution is to use std::vector instead which is portable and will do what you want, something like this:

#include <iostream>
#include <vector>

int main(){
    int m,n;
    std::cout << "Input the size of the square matrix :  ";
    std::cin >> m;
    n=m;

    std::vector <std::vector <int>> A;
    std::vector <std::vector <int>> M;
    A.resize (m);
    M.resize (m);

    for (int i = 0; i < m; ++i)
    {
        A [i].resize (n);
        M [i].resize (n + 1);
    }

    for (int i(0);i<m;i++){
        for(int j(0);j<n;j++){
            std::cout << "Input element A["<<i<<"]["<<j<<"] : ";
            std::cin >> A[i][j];
            M[i][j]=A[i][j];    
            std::cout << "\n";
        }
    }

    for (int i(0);i<m;i++){
        for(int j(0);j<=n;j++){
            std::cout << M[i][j] << " ";
        }
        std::cout << "\n";
    }
}

Live demo

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

3 Comments

So there's no way to solve that "manually", without using the vector class ?
Yes there is, you can memset your VLAs as described in the link posted by user4581301 above. But if you're writing C++, you shouldn't be using VLAs - like I say, there non-standard.
I have a couple reasons beyond non-standard: VLA's totally <expletive deleted> the compile-time behaviour of sizeofand they are a really great way to allow the user to trigger a stack overflow. Input an m of 250 on many systems and sit back to watch the fun.
1

In the bad old days of C, you could realloc() the array bigger and memset() the new column (only good for the last dimension, where items are adjacent).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.