使用数组创建矩阵

发布于 2025-01-09 19:04:22 字数 1069 浏览 0 评论 0原文

我制作了一个数组矩阵并用于循环,问题是它只显示我的最后一个输入。 请参阅下面的示例结果。

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


   }

示例结果:

输入列数:3

输入行数:2

输入值或行数:

第 1 行:-1

行1: -2

第 1 行:-3

输入值或行:

第 2 行:4

第 2 行:5

第 2 行:-6

输入的矩阵为:

4 5 -6

4 5 -6

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带刺的爱情 2025-01-16 19:04:22

只需将数组的声明更改为这样:

int[x][y] 为 int[10][10]

Just change declaration of array like this:

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

旧城空念 2025-01-16 19:04:22

这里的问题是您想要使用大小可变的数组。因此,输入“x”和“y”,然后将其用作数组大小。 (array[x][y])

这在 C++ 中不起作用。 C++ 中不存在所谓的动态 VLA(可变长度数组)。

有一些方言或编译器扩展可以处理 VLA,但 C++ 不允许 VLA。

这意味着,您不能编写如下内容:

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

这是无效的 C++ 代码。

C 样式数组必须具有编译时常量值。您可以使用 constconstexpr 甚至像 42 这样的数字文字来定义它。魔法常数为 42 或最差的解决方案。无论如何,你可以写

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

This 在语法上是正确的。

但是,它不会帮助你。因为你需要一些“动态”的东西。 C++ 中惯用的正确方法是使用 std::vector。请阅读此处了解相关内容。使用 2d std::vector 的 C++ 解决方案将如下所示:

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

但是,接下来,我们经常听到这样的说法:您还没有了解 std::vector 。然后,您需要使用使用 newdelete 来使用旧式内存分配的方法。这是一种不推荐的方法,但出于教学目的,它仍然经常使用。

因此,您首先分配用于保存指向数组(行)的指针的内存,然后为行中的列分配内存。

这将如下所示:

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

正如所说,经常被教导,但强烈不鼓励。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文