0

The question originally specified that a user inputs values for a three by three matrix. A challenge was then put forth to modify the program so that a user can specify the dimensions for the array. I attempted the question and my program works fine, but it only prints out the last values in the form of the array the user specified.

/*three by three matrix
 user enters values
 outputs these elements
 sums all the elements and outputs the result
*/

#include <iostream>

using namespace std;

int main(){
    int i, j;
    int r, c = 0;
    int matrix[r][c];
    int sum;

    cout << "A matrix, you want to make.Help I shall, \n";
    cout << "ROWS: ";
    cin >> r;

    cout << "COLUMNS: ";
    cin >> c;

    cout << "Give me the values of the " << r << " X " << c << " matrix in rows first: ";
    cout << "\n";

    //user inputs the values
    for(i = 0; i < r; i++){
        for(j = 0; j < c; j++){
            cin >> matrix[r][c];
            sum = matrix[r][c] + sum;
        }

        cout << "\n";
    }


    //outputs the values
    for(i = 0; i < r; i++){
        for(j = 0; j < c; j++){
            cout << matrix[r][c] << " ";
        }

        cout << "\n";
    }

    cout << "The sum of all these elements is: " << sum;

    return 0;
}
wazz
  • 4,953
  • 5
  • 20
  • 34
  • 1
    So the question is? – F. Leone May 14 '18 at 09:14
  • 3
    `int matrix[r][c];` will not change its size when you input new values for `r` and `c`. In fact, it is formally not even valid C++ to have non-constant dimensions, but [uses a compiler extension](https://stackoverflow.com/questions/5947661/in-c-books-array-bound-must-be-constant-expression-but-why-the-following-cod). – Bo Persson May 14 '18 at 09:18
  • @BoPersson what do you suggest? I've always just thrown in [ i ] or something – victor kuchiki May 14 '18 at 09:21
  • Using a compiler extension might be fine, if you are always using that compiler. Otherwise many solutions to dynamic arrays involve use of `std::vector`. Or `std::vector>` if you want more dimensions. – Bo Persson May 14 '18 at 09:24

2 Answers2

0

Let's consider

int main(){
    int i, j;
    int r, c = 0;
    int matrix[r][c];

Here r is uninitialized and c is zero. This means that matrix has an unknown number of rows and zero columns. Note that if you do want to initialize multiple variables of the same type it is generally better to do it one per line, like this

int r = 0;
int c = 0;

Probably the easiest approach will be to use a vector of vector as mentioned in the comments. Unfortunately C++ has relatively poor intrinsic support for multi-dimensional arrays.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
0

Please try this code:

include

using namespace std;

int main(){
    int i, j;
    int r, c = 0;
    int sum=0;

    cout << "A matrix, you want to make.Help I shall, \n";
    cout << "ROWS: ";
    cin >> r;

    cout << "COLUMNS: ";
    cin >> c;

    cout << "Give me the values of the " << r << " X " << c << " matrix in rows first: ";
    cout << "\n";

    int matrix[r][c];

    //user inputs the values
    for(i = 0; i < r; i++){
        for(j = 0; j < c; j++){
            cin >> matrix[i][j];
            sum = matrix[i][j] + sum;
        }

        cout << "\n";
    }


    //outputs the values
    for(i = 0; i < r; i++){
        for(j = 0; j < c; j++){
            cout << matrix[i][j] << " ";
        }

        cout << "\n";
    }

    cout << "The sum of all these elements is: " << sum;

    return 0;
}

There were two problems in your code:

  1. You were initializing matrix[r][c] at the start without initializing values of r and c.

  2. You were doing cin>> matrix[r][c] inside loop which gets values of matrix[3][3] every time loop runs since r=3 and c=3. Instead do cin>> matrix[i][j] inside loop.

Hope this helps!!!

Community
  • 1
  • 1
Abhishek Sharma
  • 153
  • 1
  • 2
  • 11
  • 2
    You should mention that VLA are not standard C++ but can be allowed as compiler extensions. Please never propose non standard code without an explicit warning! – Serge Ballesta May 14 '18 at 10:31