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;
}
Hibou
  • 196
  • 2
  • 3
  • 10
  • 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};` – user4581301 Jun 05 '20 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](https://stackoverflow.com/questions/4393779/initializing-a-dynamically-sized-variable-length-array-vla-to-0) – user4581301 Jun 05 '20 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. – user4581301 Jun 05 '20 at 17:15
  • Side note: [A simple, fast, and safe dynamically-sized matrix class](https://stackoverflow.com/a/2076668/4581301). As an added bonus it also automatically initializes to all zeroes. – user4581301 Jun 05 '20 at 17:40

2 Answers2

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

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • So there's no way to solve that "manually", without using the vector class ? – Hibou Jun 05 '20 at 17:13
  • 1
    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. – Paul Sanders Jun 05 '20 at 17:14
  • 1
    I have a couple reasons beyond non-standard: VLA's totally the compile-time behaviour of `sizeof`and 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. – user4581301 Jun 05 '20 at 17:22
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).