Tequila Lucille
Tequila Lucille

Reputation: 15

Creating matrix using array

I made an array matrix and used for loops, the problem is that it only displays my last input. Please refer to the sample result below.

#include<iostream>
using namespace std;
int main()
 {
   int x = 0, y = 0;
   int a[x][y], i, j;

cout<<"Enter number of Columns: ";
cin>>y;

cout<<"Enter number of Rows: ";
cin>>x;

       cout<<endl;

 for(i=0; i<x; i++)
{
        cout<<endl;
        cout<<"Enter values or row/s: ";
        cout<<endl;
                            
for(j=0; j<y; j++)
{
    cout<<endl;
    
    cout<<"Row "<< i+1 << ": ";
    cin >> a[i][j]; 
   }
   }

cout<<endl;

cout<<"Entered Matrix is: ";

cout<<endl;

for(i=0; i<x; i++)
{
    for(j=0; j<y; j++)
    
        cout<<a[i][j]<<"  ";
        
    cout<<endl;
    
     }


   }

SAMPLE RESULT:

Enter number of Columns: 3

Enter number of Rows: 2

Enter values or row/s:

Row 1: -1

Row 1: -2

Row 1: -3

Enter values or row/s:

Row 2: 4

Row 2: 5

Row 2: -6

Entered Matrix is:

4 5 -6

4 5 -6

Upvotes: 1

Views: 842

Answers (2)

A M
A M

Reputation: 15265

The problem here is that you want to use arrays with a variable size. So, input "x" and "y" and then use this as the array size. (array[x][y])

This does not work in C++. There are no dynamic, so called VLAs (Variable Length Arrays) in C++.

There are some dialects or compiler extensions, which can handle VLA, but C++ does not allow VLAs.

This means, you cannot write somthing like:

int arraySize{}; 
std::cin >> arraySize;
int array[arraySize];

This is invalid C++ code.

C-Style arrays must have a compile time constant value. You can define it with const or constexpr or even a number literal like 42. Magic constants liek 42 or the worst solution. Anyway, you could write

constepxr int arraySize = 42; 
int array[arraySize];

This is syntactically correct.

But, it will not help you. Because you need something "dynamic". The idiomatic correct approach in C++ is to use a std::vector. Please read here about that. And using a 2d std::vector the C++ solution would look like that:

#include <iostream>
#include <vector>

int main() {
    // Get user input. Size of matrix. Number of rows and columns
    std::cout << "\nEnter number of Rows:      ";
    if (size_t numberOfRows{}; (std::cin >> numberOfRows) and (numberOfRows > 0)) {

        std::cout << "\nEnter number of columns:   ";
        if (size_t numberOfColumns{}; (std::cin >> numberOfColumns) and (numberOfColumns > 0)) {

            // Define 2d matrix and initialze all values to 0
            std::vector<std::vector<int>> matrix(numberOfRows, std::vector<int>(numberOfColumns, 0));

            // Read all values of matrix
            std::cout << "\n\Please enter matrix values in row-column order:\n";
            for (std::vector<int>& row : matrix)
                for (int& column : row)
                    std::cin >> column;

            // Now show matrix
            std::cout << "\n\nThe entered Matrix is:\n\n";
            for (std::vector<int>& row : matrix) {
                for (int& column : row) std::cout << column << ' ';
                std::cout << '\n';
            }
        }
    }
}

But, next, we often hear the statement that you did not learn about std::vector yet. And then, you need to use the approach to use old style memory allocation using new and delete. This is a non-recommend approach, but for teaching purposes, it is still often used.

So, you first allocate memory that will hold pointers to array (for the rows) and then allocate memory for the columns in the rows.

This would then look like the below:

#include <iostream>

int main() {
    // Get user input. Size of matrix. Number of rows and columns
    std::cout << "\nEnter number of Rows:      ";
    if (size_t numberOfRows{}; (std::cin >> numberOfRows) and (numberOfRows > 0)) {

        std::cout << "\nEnter number of columns:   ";
        if (size_t numberOfColumns{}; (std::cin >> numberOfColumns) and (numberOfColumns > 0)) {

            // Define 2d matrix 
            int** matrix;
            matrix = new int* [numberOfRows];
            for (int row = 0; row < numberOfRows; ++row)
                matrix[row] = new int[numberOfColumns];
            

            // Read all values of matrix
            std::cout << "\n\Please enter matrix values in row-column order:\n";
            for (int row = 0; row < numberOfRows; ++row)
                for (int column=0; column < numberOfColumns; ++column)
                    std::cin >> matrix[row][column];

            // Now show matrix
            std::cout << "\n\nThe entered Matrix is:\n\n";
            for (int row = 0; row < numberOfRows; ++row) {
                for (int column = 0; column < numberOfColumns; ++column) std::cout << matrix[row][column] << ' ';
                std::cout << '\n';
            }

            // Release memory
            for (int row = 0; row < numberOfRows; ++row)
                delete [] matrix[row];
            delete matrix;
        }
    }
}

As said, often teached, but strongly discouraged.

Upvotes: 1

Sathi Aiswarya
Sathi Aiswarya

Reputation: 2950

Just change declaration of array like this:

int[x][y] to int[10][10]

Upvotes: 1

Related Questions