[英]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.