繁体   English   中英

c++ 中的多维数组和嵌套循环

[英]Multidimensional array and nested loop in c++

我正在尝试编写一个程序来使用递减数组显示数字,但我遇到了问题:循环不会停止。

输入样本:

5
5

Output:

12345
1234
123
12
1

我使用了二维数组,但我知道我的问题出在我的逻辑程序中。 也许有人想帮我修复这个程序?

我的程序:

#include<iostream>
using namespace std;

main(){
    
    int row, column, i, j;
    cout<<"masukkan baris: "<<endl;
    cin>>row;
    cout<<"masukkan column: "<<endl;
    cin>>kolom;
    
    int array[row][column];

    for (i = 0; i < row; i++) {
        for (j = 0; j < column; j--) {
            cout <<array[i][j]++<<" ";
        }
        cout<<endl;
    }

    return 0;
}

首先,我认为第二个循环的索引( j-- )有问题。

它只解决无限循环。

我只更改单行以开始编程。

#include<iostream>
using namespace std;

main(){
    
    int row, column, i, j;
    cout<<"masukkan baris: "<<endl;
    cin>>row;
    cout<<"masukkan column: "<<endl;
    cin>>kolom;
    
    int array[row][column]; // initialize

    for (i = 0; i < row; i++) {
        //for (j = 0; j < column; j--) {
        for (j = 0; j < column; j++) {
            cout <<array[i][j]++<<" ";
        }
        cout<<endl;
    }

    return 0;
}

ps)我认为array用于值的名称是不安全的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM